Android应用实例之调节播放器音量——AudioManager的应用


实现的功能:调节播放器音量。

实现的思路:1)用ProgressBar显示当前音量大小;

                          2)在Button单击事件中改变音量大小;

                          3)关键是用什么控制音量,百度了一下可以用AudioManager调节各类型声音的音量(比如:通话声音、铃声声音、音乐声音等),本文调节的是音乐的声音。

关键技术点:MediaPlayer播放MP3音乐、ProgressBar应用、AudioManager应用

参考及相关文章:

第一步:新建一个工程,命名为AudioManagerVolume,Activity命名为AdjustVolumeActivity。

修改布局文件main.xml,代码后如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" android:background="#FFFFFF">  
  5.     <Button android:id="@+id/play" android:layout_width="wrap_content"  
  6.         android:layout_height="wrap_content" android:text="播放MP3音乐" />  
  7.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  8.         android:orientation="horizontal" android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent">  
  10.         <Button android:id="@+id/down" android:layout_width="wrap_content"  
  11.             android:layout_height="wrap_content"  android:text="减小" />  
  12.         <ProgressBar android:id="@+id/progress"  
  13.             style="?android:attr/progressBarStyleHorizontal"  
  14.             android:layout_width="150dip" android:layout_height="wrap_content"  
  15.             />  
  16.         <Button android:id="@+id/up" android:layout_width="wrap_content"  
  17.             android:layout_height="wrap_content" android:text="增大" />  
  18.     </LinearLayout>  
  19. </LinearLayout>  

第二步:修改AdjustVolumeActivity类,修改后代码如下:

  1. package com.zyg.demo.adjustvolume;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Context;  
  7. import android.content.res.AssetFileDescriptor;  
  8. import android.content.res.AssetManager;  
  9. import android.media.AudioManager;  
  10. import android.media.MediaPlayer;  
  11. import android.os.Bundle;  
  12. import android.view.View;  
  13. import android.view.View.OnClickListener;  
  14. import android.widget.Button;  
  15. import android.widget.ProgressBar;  
  16.   
  17. import com.zyg.demo.progressbar.R;  
  18.   
  19. public class AdjustVolumeActivity extends Activity implements OnClickListener {  
  20.     private Button play = null;  
  21.     private Button down = null;  
  22.     private Button up = null;  
  23.   
  24.     private ProgressBar pb = null;  
  25.     private int maxVolume = 50// 最大音量值   
  26.     private int curVolume = 20// 当前音量值   
  27.     private int stepVolume = 0// 每次调整的音量幅度   
  28.   
  29.     private MediaPlayer mediaPlayer = null;// 播放器   
  30.     private AudioManager audioMgr = null// Audio管理器,用了控制音量   
  31.     private AssetManager assetMgr = null// 资源管理器   
  32.   
  33.     private final String musicName = "hehe.mp3";  
  34.   
  35.     @Override  
  36.     public void onCreate(Bundle savedInstanceState) {  
  37.         super.onCreate(savedInstanceState);  
  38.         setContentView(R.layout.main);  
  39.   
  40.         // 初始化播放器、音量数据等相关工作   
  41.         initPlayWork();  
  42.         // 初始化视图   
  43.         initUI();  
  44.     }  
  45.   
  46.     /** 
  47.      * 初始化UI 
  48.      */  
  49.     private void initUI() {  
  50.         play = (Button) findViewById(R.id.play);  
  51.         down = (Button) findViewById(R.id.down);  
  52.         up = (Button) findViewById(R.id.up);  
  53.   
  54.         play.setOnClickListener(this);  
  55.         down.setOnClickListener(this);  
  56.         up.setOnClickListener(this);  
  57.         // 设置进度条   
  58.         pb = (ProgressBar) findViewById(R.id.progress);  
  59.         pb.setMax(maxVolume);  
  60.         pb.setProgress(curVolume);  
  61.     }  
  62.   
  63.     /** 
  64.      * 初始化播放器、音量数据等相关工作 
  65.      */  
  66.     private void initPlayWork() {  
  67.         audioMgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);  
  68.         // 获取最大音乐音量   
  69.         maxVolume = audioMgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);  
  70.         // 初始化音量大概为最大音量的1/2   
  71.         curVolume = maxVolume / 2;  
  72.         // 每次调整的音量大概为最大音量的1/6   
  73.         stepVolume = maxVolume / 6;  
  74.   
  75.         mediaPlayer = new MediaPlayer();  
  76.         assetMgr = this.getAssets();  
  77.     }  
  78.   
  79.     /** 
  80.      * 准备播放音乐 
  81.      *  
  82.      * @param music 
  83.      */  
  84.     private void prepareAndPlay() {  
  85.         try {  
  86.             // 打开指定音乐文件   
  87.             AssetFileDescriptor afd = assetMgr.openFd(musicName);  
  88.             mediaPlayer.reset();  
  89.             // 使用MediaPlayer加载指定的声音文件。   
  90.             mediaPlayer.setDataSource(afd.getFileDescriptor(),  
  91.                     afd.getStartOffset(), afd.getLength());  
  92.             // 准备声音   
  93.             mediaPlayer.prepare();  
  94.             // 播放   
  95.             mediaPlayer.start();  
  96.         } catch (IOException e) {  
  97.             e.printStackTrace();  
  98.         }  
  99.     }  
  100.   
  101.     /** 
  102.      * 调整音量 
  103.      */  
  104.     private void adjustVolume() {  
  105.         audioMgr.setStreamVolume(AudioManager.STREAM_MUSIC, curVolume,  
  106.                 AudioManager.FLAG_PLAY_SOUND);  
  107.     }  
  108.   
  109.     @Override  
  110.     public void onClick(View v) {  
  111.         int id = v.getId();  
  112.         switch (id) {  
  113.         case R.id.play://按下播放按钮   
  114.             prepareAndPlay();  
  115.             break;  
  116.         case R.id.up://按下增大音量按钮   
  117.             curVolume += stepVolume;  
  118.             if (curVolume >= maxVolume) {  
  119.                 curVolume = maxVolume;  
  120.             }  
  121.             pb.setProgress(curVolume);  
  122.             break;  
  123.         case R.id.down://按下减小音量按钮   
  124.             curVolume -= stepVolume;  
  125.             if (curVolume <= 0) {  
  126.                 curVolume = 0;  
  127.             }  
  128.             pb.setProgress(curVolume);  
  129.             break;  
  130.         default:  
  131.             break;  
  132.         }  
  133.         // 调整音量   
  134.         adjustVolume();  
  135.     }  
  136. }  

备注:有的文章中提到需要添加权限<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> ,我这里并没有添加,可以正常运行。

第三步:运行程序,效果如下:

相关内容