本实例通过MediaPlayer播放一首音乐并通过AudioManager控制手机音频,关于AudioManager的详解可参照:Android开发之AudioManager(音频管理器)详解
程序运行效果图:
实例代码:
package com.jph.audiomanagerdemo; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.app.Activity; import android.app.Service; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.ToggleButton; /** * Describe:</br> * 控制数据音频 * 本实例通过MediaPlayer播放一首音乐并 * 通过AudioManager控制手机音频 * */ public class AudioManagerDemo extends Activity { Button btnPlay,btnUp,btnDown; ToggleButton btnMute; AudioManager aManager; MediaPlayer mPlayer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnDown=(Button)findViewById(R.id.btnDown); btnPlay=(Button)findViewById(R.id.btnPlay); btnUp=(Button)findViewById(R.id.btnUp); btnMute=(ToggleButton)findViewById(R.id.btnMute); //初始化AudioManager aManager=(AudioManager) getSystemService(Service.AUDIO_SERVICE); OnClickListener listener=new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btnPlay: //初始化MediaPlayer为播放音乐做准备 mPlayer=MediaPlayer.create( AudioManagerDemo.this, R.raw.music); //设置音乐循环播放 mPlayer.setLooping(true); //播放 mPlayer.start(); break; case R.id.btnUp: //指定调节音乐的音频为增大,并显示音量图形 aManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI); break; case R.id.btnDown: //指定调节音乐的音频为减小,并显示音量图形 aManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI); break; default: break; } } }; //为按钮添加监听器 btnDown.setOnClickListener(listener); btnPlay.setOnClickListener(listener); btnUp.setOnClickListener(listener); btnMute.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub //设置音乐静音 aManager.setStreamMute(AudioManager.STREAM_MUSIC, isChecked); } }); } }main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/btnPlay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="播放"/> <Button android:id="@+id/btnUp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_below="@+id/btnPlay" android:text="增大音量"/> <Button android:id="@+id/btnDown" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_below="@+id/btnUp" android:text="减小音量"/> <ToggleButton android:id="@+id/btnMute" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_below="@+id/btnDown" android:textOn="关闭静音" android:textOff="开启静音"/> </RelativeLayout>