Android之音量调节详解编程语言

本文章主要介绍了Android之音量调节,具有不错的的参考价值,希望对您有所帮助,如解说有误或未考虑完全的地方,请您留言指出,谢谢!

main.xml布局文件

<? xml version = " 1.0 " encoding = " utf-8 " ?>
< LinearLayout xmlns:android = " http://schemas.android.com/apk/res/android "
android:orientation
= " vertical " android:layout_width = " fill_parent "
android:layout_height
= " fill_parent " >
< Button android:id = " @+id/btnPlay "
android:layout_width
= " fill_parent "
android:layout_height
= " wrap_content "
android:text
= " 播放音乐 " />
< LinearLayout android:orientation = " horizontal "
android:layout_width
= " wrap_content "
android:layout_height
= " wrap_content "
android:layout_gravity
= " center_horizontal " >
< ToggleButton android:id = " @+id/tbMute "
android:layout_width
= " fill_parent "
android:layout_height
= " wrap_content "
android:textOn
= " 静音 "
android:textOff
= " 正常 "
android:checked
= " true "
android:layout_gravity
= " center_vertical " />
< Button android:id = " @+id/btnUpper "
android:text
= " 增大音量 "
android:layout_width
= " wrap_content "
android:layout_height
= " wrap_content " />
< Button android:id = " @+id/btnLower "
android:text
= " 减小音量 "
android:layout_width
= " wrap_content "
android:layout_height
= " wrap_content " />
</ LinearLayout >
</ LinearLayout >

AudioActivity类

package com.ljq.activity;

import android.app.Activity;
import android.app.Service;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class AudioActivity extends Activity {
private Button btnPlay = null , btnUpper = null , btnLower = null ;
private ToggleButton tbMute = null ;
private MediaPlayer mediaPlayer = null ; // 声频
private AudioManager audioManager = null ; // 音频

@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);

audioManager
= (AudioManager)getSystemService(Service.AUDIO_SERVICE);
btnPlay
= (Button)findViewById(R.id.btnPlay);
btnUpper
= (Button)findViewById(R.id.btnUpper);
btnLower
= (Button)findViewById(R.id.btnLower);
btnPlay.setOnClickListener(listener);
btnUpper.setOnClickListener(listener);
btnLower.setOnClickListener(listener);
tbMute
= (ToggleButton)findViewById(R.id.tbMute);
tbMute.setOnCheckedChangeListener(
new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
audioManager.setStreamMute(AudioManager.STREAM_MUSIC,
! isChecked); // 设置是否静音
}
});
}

View.OnClickListener listener
= new View.OnClickListener(){
public void onClick(View v) {
@SuppressWarnings(
" unused " )
Button btn
= (Button)v;
switch (v.getId()) {
case R.id.btnPlay: // 播放音乐
mediaPlayer = MediaPlayer.create(AudioActivity. this , R.raw.music);
mediaPlayer.setLooping(
true ); // 设置循环播放
mediaPlayer.start(); // 播放声音
break ;
case R.id.btnUpper: // 增多音量
// adjustStreamVolume: 调整指定声音类型的音量
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE,
AudioManager.FLAG_SHOW_UI);
// 调高声音
break ;
case R.id.btnLower: // 减少音量
// 第一个参数:声音类型
// 第二个参数:调整音量的方向
// 第三个参数:可选的标志位
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER,
AudioManager.FLAG_SHOW_UI);
// 调低声音
break ;
}
}
};

}

运行结果

Android之音量调节详解编程语言

原创文章,作者:506227337,如若转载,请注明出处:https://blog.ytso.com/228166.html

(0)
上一篇 2022年1月11日
下一篇 2022年1月11日

相关推荐

发表回复

登录后才能评论