转载请注明出处,原文网址:http://blog.csdn.net/m_changgong/article/details/6846000 作者:张燕广
实现的功能:调节播放器音量。
实现的思路:1)用ProgressBar显示当前音量大小;
2)在Button单击事件中改变音量大小;
3)关键是用什么控制音量,百度了一下可以用AudioManager调节各类型声音的音量(比如:通话声音、铃声声音、音乐声音等),本文调节的是音乐的声音。
关键技术点:MediaPlayer播放MP3音乐、ProgressBar应用、AudioManager应用
参考及相关文章:http://blog.csdn.net/m_changgong/article/details/6846000、http://lipeng88213.iteye.com/blog/1149352
第一步:新建一个工程,命名为AudioManagerVolume,Activity命名为AdjustVolumeActivity。
修改布局文件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" android:background="#FFFFFF"> <Button android:id="@+id/play" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="播放MP3音乐" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="减小" /> <ProgressBar android:id="@+id/progress" style="?android:attr/progressBarStyleHorizontal" android:layout_width="150dip" android:layout_height="wrap_content" /> <Button android:id="@+id/up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="增大" /> </LinearLayout> </LinearLayout>
第二步:修改AdjustVolumeActivity类,修改后代码如下:
package com.zyg.demo.adjustvolume; import java.io.IOException; import android.app.Activity; import android.content.Context; import android.content.res.AssetFileDescriptor; import android.content.res.AssetManager; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; import com.zyg.demo.progressbar.R; public class AdjustVolumeActivity extends Activity implements OnClickListener { private Button play = null; private Button down = null; private Button up = null; private ProgressBar pb = null; private int maxVolume = 50; // 最大音量值 private int curVolume = 20; // 当前音量值 private int stepVolume = 0; // 每次调整的音量幅度 private MediaPlayer mediaPlayer = null;// 播放器 private AudioManager audioMgr = null; // Audio管理器,用了控制音量 private AssetManager assetMgr = null; // 资源管理器 private final String musicName = "hehe.mp3"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 初始化播放器、音量数据等相关工作 initPlayWork(); // 初始化视图 initUI(); } /** * 初始化UI */ private void initUI() { play = (Button) findViewById(R.id.play); down = (Button) findViewById(R.id.down); up = (Button) findViewById(R.id.up); play.setOnClickListener(this); down.setOnClickListener(this); up.setOnClickListener(this); // 设置进度条 pb = (ProgressBar) findViewById(R.id.progress); pb.setMax(maxVolume); pb.setProgress(curVolume); } /** * 初始化播放器、音量数据等相关工作 */ private void initPlayWork() { audioMgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE); // 获取最大音乐音量 maxVolume = audioMgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC); // 初始化音量大概为最大音量的1/2 curVolume = maxVolume / 2; // 每次调整的音量大概为最大音量的1/6 stepVolume = maxVolume / 6; mediaPlayer = new MediaPlayer(); assetMgr = this.getAssets(); } /** * 准备播放音乐 * * @param music */ private void prepareAndPlay() { try { // 打开指定音乐文件 AssetFileDescriptor afd = assetMgr.openFd(musicName); mediaPlayer.reset(); // 使用MediaPlayer加载指定的声音文件。 mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); // 准备声音 mediaPlayer.prepare(); // 播放 mediaPlayer.start(); } catch (IOException e) { e.printStackTrace(); } } /** * 调整音量 */ private void adjustVolume() { audioMgr.setStreamVolume(AudioManager.STREAM_MUSIC, curVolume, AudioManager.FLAG_PLAY_SOUND); } @Override public void onClick(View v) { int id = v.getId(); switch (id) { case R.id.play://按下播放按钮 prepareAndPlay(); break; case R.id.up://按下增大音量按钮 curVolume += stepVolume; if (curVolume >= maxVolume) { curVolume = maxVolume; } pb.setProgress(curVolume); break; case R.id.down://按下减小音量按钮 curVolume -= stepVolume; if (curVolume <= 0) { curVolume = 0; } pb.setProgress(curVolume); break; default: break; } // 调整音量 adjustVolume(); } }
备注:有的文章中提到需要添加权限<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> ,我这里并没有添加,可以正常运行。
第三步:运行程序,效果如下: