音频焦点系列:手写一个demo理解音频焦点与AudioMananger
Posted zhangjin1120
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了音频焦点系列:手写一个demo理解音频焦点与AudioMananger相关的知识,希望对你有一定的参考价值。
目录
先看测试效果:
- 先打开demo,点击“start”等一会儿(本地音频加载需要时间),赛车声音会循环播放。
- home键回到桌面,找到酷狗音乐APP并打开,播放一首歌曲,这时logcat里面会打印
AUDIOFOCUS_LOSS
,Demo App会暂停播放。 - 手动暂停酷狗音乐播放器(log不会有变化,也就是并没有触发
AUDIOFOCUS_GAIN
)。 - 设置闹钟,闹钟触发,log显示
AUDIOFOCUS_LOSS_TRANSIENT
- 关闭闹钟,log显示
AUDIOFOCUS_GAIN
Demo源码
布局文件就一个按钮,自己写布局哈,完整Demo代码如下,注释写的很清楚了,就不解释了。
public class MainActivity extends AppCompatActivity
private final String TAG = "xxx";
private AudioManager mAudioManager;
private MediaPlayer mMediaPlayer;
private Button btmMusic;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
private void initData()
//初始化AudioManager对象
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//申请焦点
mAudioManager.requestAudioFocus(mAudioFocusChange, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
AssetFileDescriptor fileDescriptor;
try
//获取音频文件
fileDescriptor = this.getAssets().openFd("11582.mp3");
//实例化MediaPlayer对象
mMediaPlayer = new MediaPlayer();
//设置播放流类型
mMediaPlayer.setAudiostreamType(AudioManager.STREAM_MUSIC);
//设置播放源,有多个参数可以选择,具体参考相关文档,本文旨在介绍音频焦点
mMediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),
fileDescriptor.getStartOffset(),
fileDescriptor.getLength());
//设置循环播放
mMediaPlayer.setLooping(true);
//准备监听
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
@Override
public void onPrepared(MediaPlayer mp)
//准备完成后自动播放
mMediaPlayer.start();
);
//异步准备
mMediaPlayer.prepareAsync();
catch (IOException e)
e.printStackTrace();
private void initView()
btmMusic = (Button) findViewById(R.id.btm_music);
if (btmMusic != null)
btmMusic.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if (mMediaPlayer != null)
if (mMediaPlayer.isPlaying())
stop();
else
start();
);
private void start()
btmMusic.setText("Stop");
mAudioManager.requestAudioFocus(mAudioFocusChange, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
mMediaPlayer.start();
private void stop()
btmMusic.setText("Start");
mMediaPlayer.pause();
/**
* 焦点变化监听器
*/
private AudioManager.OnAudioFocusChangeListener mAudioFocusChange = new AudioManager.OnAudioFocusChangeListener()
@Override
public void onAudioFocusChange(int focusChange)
switch (focusChange)
case AudioManager.AUDIOFOCUS_LOSS:
//长时间丢失焦点
Log.d(TAG, "AUDIOFOCUS_LOSS");
stop();
//释放焦点
mAudioManager.abandonAudioFocus(mAudioFocusChange);
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
//短暂性丢失焦点
stop();
Log.d(TAG, "AUDIOFOCUS_LOSS_TRANSIENT");
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
//短暂性丢失焦点并作降音处理
Log.d(TAG, "AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
break;
case AudioManager.AUDIOFOCUS_GAIN:
//重新获得焦点
Log.d(TAG, "AUDIOFOCUS_GAIN");
start();
break;
;
@Override
protected void onDestroy()
super.onDestroy();
mMediaPlayer.release();
mAudioManager.abandonAudioFocus(mAudioFocusChange);
音频焦点的实用场景有哪些?
- 打开酷狗音乐播放一首歌曲,再打开B站,播放视频,这时酷狗会自动暂停,按返回键退出视频播放时,酷狗又会自动恢复播放音乐。
官网怎么描述音频焦点的?
音频焦点改进
AudioManager.requestAudioFocus()
以上是关于音频焦点系列:手写一个demo理解音频焦点与AudioMananger的主要内容,如果未能解决你的问题,请参考以下文章