在聽筒模式下
- am.setSpeakerphoneOn(false);
- setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
- am.setMode(AudioManager.MODE_IN_CALL);
我用Mediaplayer AudioTrack調節音量總是失敗
- at.setStereoVolume(vol, vol);
- player.setVolume(vol,vol);
後來 決定用AudioManager來調節音量
AudioManager可以修改系統Android系統的音量
下面介紹幾個AudioManager的幾個音量調整方面的方法.
首先是得到AudioManager實例:
AudioManager am=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
調整音量方法有兩種,一種是漸進式,即像手動按音量鍵一樣,一步一步增加或減少,另一種是直接設置音量值.
1、漸進式
- public void adjustStreamVolume (int streamType, int direction, int flags)
-
- am.adjustStreamVolume (AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
解釋一下三個參數
- 第一個streamType是需要調整音量的類型,這裡設的是媒體音量,可以是:
- STREAM_ALARM 警報
- STREAM_MUSIC 音樂回放即媒體音量
- STREAM_NOTIFICATION 窗口頂部狀態欄Notification,
- STREAM_RING 鈴聲
- STREAM_SYSTEM 系統
- STREAM_VOICE_CALL 通話
- STREAM_DTMF 雙音多頻,不是很明白什麼東西
-
- 第二個direction,是調整的方向,增加或減少,可以是:
- ADJUST_LOWER 降低音量
- ADJUST_RAISE 升高音量
- ADJUST_SAME 保持不變,這個主要用於向用戶展示當前的音量
-
- 第三個flags是一些附加參數,只介紹兩個常用的
- FLAG_PLAY_SOUND 調整音量時播放聲音
- FLAG_SHOW_UI 調整時顯示音量條,就是按音量鍵出現的那個
2、直接設置音量值的方法:
- public void setStreamVolume (int streamType, int index, int flags)
-
- am.setStreamVolume(AudioManager.STREAM_MUSIC, am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), AudioManager.FLAG_PLAY_SOUND);
- am.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);//得到聽筒模式的最大值
- am.getStreamVolume(AudioManager.STREAM_VOICE_CALL);//得到聽筒模式的當前值
第一個和第三個參數與上面的相同
第二個參數是一個音量的int值,getStreamMaxVolume(int streamType)得到的是該類型音量的最大值,可以根據這個值計算你需要的音量,我這裡直接調到最大.
最後我的代碼:
- package com.lp;
-
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
-
- import android.app.Activity;
- import android.content.Context;
- import android.media.AudioFormat;
- import android.media.AudioManager;
- import android.media.AudioTrack;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.SeekBar;
- /**
- * AudioTrack 播放音頻 如wav格式
- * 並允許調節音量
- * @author Administrator
- *
- */
- public class MainActivity5 extends Activity {
- private Button play;
- private Button stop;
- private SeekBar soundValue;
-
- private AudioTrack at;
- private AudioManager am;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main_sk);
- am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
- play = (Button)findViewById(R.id.main_sk_play);
- stop = (Button)findViewById(R.id.main_sk_stop);
- soundValue = (SeekBar)findViewById(R.id.skbVolume);
- setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
- play.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
-
- if(am.isSpeakerphoneOn()){
- am.setSpeakerphoneOn(false);
- }
- //setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
- am.setMode(AudioManager.MODE_IN_CALL);
- System.out.println(am.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL));
- System.out.println("&&&&&&&&&&&&&");
- System.out.println(am.getStreamVolume(AudioManager.STREAM_VOICE_CALL));
- //am.setStreamVolume(streamType, index, flags)
-
- int bufferSizeInBytes = AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
-
- if(at==null){
- at = new AudioTrack(AudioManager.STREAM_VOICE_CALL, 44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSizeInBytes, AudioTrack.MODE_STREAM);
- System.out.println("22222");
- //at.setStereoVolume(100f, 100f);
- at.setStereoVolume(0.7f, 0.7f);//設置當前音量大小
- new AudioTrackThread().start();
- }else{
- if(at.getPlayState()==AudioTrack.PLAYSTATE_PLAYING){
- System.out.println("111111111");
- }else{
- System.out.println("33333");
- at = new AudioTrack(AudioManager.STREAM_VOICE_CALL, 44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSizeInBytes, AudioTrack.MODE_STREAM);
- new AudioTrackThread().start();
- }
- }
-
- }
- });
-
- stop.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- if(at.getPlayState()==AudioTrack.PLAYSTATE_PLAYING){
- try{
- at.stop();
- }catch (IllegalStateException e)
- {
- e.printStackTrace();
- }
- at.release();
- am.setMode(AudioManager.MODE_NORMAL);
- }
- }
- });
-
- // soundValue.setMax(100);//音量調節的極限
- // soundValue.setProgress(70);//設置seekbar的位置值
- soundValue.setMax(am.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL));
- soundValue.setProgress(am.getStreamVolume(AudioManager.STREAM_VOICE_CALL));
-
- soundValue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
-
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- // float vol=(float)(seekBar.getProgress())/(float)(seekBar.getMax());
- // System.out.println(vol);
- // at.setStereoVolume(vol, vol);//設置音量
- am.setStreamVolume(AudioManager.STREAM_VOICE_CALL, seekBar.getProgress(), AudioManager.FLAG_PLAY_SOUND);
- }
-
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- // TODO Auto-generated method stub
- }
-
- @Override
- public void onProgressChanged(SeekBar seekBar, int progress,
- boolean fromUser) {
- // TODO Auto-generated method stub
- }
- });
- }
-
- class AudioTrackThread extends Thread{
-
- @Override
- public void run() {
- byte[] out_bytes = new byte[44100];
-
- InputStream is = getResources().openRawResource(R.raw.start);
- int length ;
- try{
- at.play();
- }catch (IllegalStateException e)
- {
- e.printStackTrace();
- }
- try {
- while((length = is.read(out_bytes))!=-1){
- //System.out.println(length);
- at.write(out_bytes, 0, length);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- if(at.getPlayState()==AudioTrack.PLAYSTATE_PLAYING){
- try{
- at.stop();
- }catch (IllegalStateException e)
- {
- e.printStackTrace();
- }
- at.release();
- am.setMode(AudioManager.MODE_NORMAL);
- }
- }
-
- }
-
- }
當然 權限
- <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.RECORD_AUDIO" />