歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android應用實例之調節播放器音量——AudioManager的應用

Android應用實例之調節播放器音量——AudioManager的應用

日期:2017/3/1 11:15:45   编辑:Linux編程

實現的功能:調節播放器音量。

實現的思路:1)用ProgressBar顯示當前音量大小;

2)在Button單擊事件中改變音量大小;

3)關鍵是用什麼控制音量,百度了一下可以用AudioManager調節各類型聲音的音量(比如:通話聲音、鈴聲聲音、音樂聲音等),本文調節的是音樂的聲音。

關鍵技術點:MediaPlayer播放MP3音樂、ProgressBar應用、AudioManager應用

參考及相關文章:http://www.linuxidc.com/Linux/2011-10/44660.htm

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

備注:有的文章中提到需要添加權限<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> ,我這裡並沒有添加,可以正常運行。

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

Copyright © Linux教程網 All Rights Reserved