当前位置:网站首页>音频焦点系列:手写一个demo理解音频焦点与AudioMananger
音频焦点系列:手写一个demo理解音频焦点与AudioMananger
2022-07-03 05:03:00 【zhangjin1120】
先看测试效果:
- 先打开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站,播放视频,这时酷狗会自动暂停,按返回键退出视频播放时,酷狗又会自动恢复播放音乐。
官网怎么描述音频焦点的?
边栏推荐
- RT thread flow notes I startup, schedule, thread
- Notes | numpy-07 Slice and index
- Realize file download through the tag of < a > and customize the file name
- 1086 tree traversals again (25 points)
- 1115 counting nodes in a BST (30 points)
- The reason why the entity class in the database is changed into hump naming
- Shell script -- condition judgment
- The 19th Zhejiang I. barbecue
- ZABBIX monitoring of lamp architecture (3): zabbix+mysql (to be continued)
- 编译GCC遇到的“pthread.h” not found问题
猜你喜欢

Oracle SQL table data loss

leetcode406. Rebuild the queue based on height
![[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)](/img/2a/362f3b0491f721d89336d4f468c9dd.jpg)
[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)
![[research materials] 2021 China's game industry brand report - Download attached](/img/b7/a377b0b7c742078e2feb28ebfbca62.jpg)
[research materials] 2021 China's game industry brand report - Download attached

2022-02-11 daily clock in: problem fine brush
![[batch dos-cmd command - summary and summary] - CMD window setting and operation command - close CMD window and exit CMD environment (exit, exit /b, goto: EOF)](/img/ce/d6f4fb30727e7436b6443537429ad4.png)
[batch dos-cmd command - summary and summary] - CMD window setting and operation command - close CMD window and exit CMD environment (exit, exit /b, goto: EOF)

First + only! Alibaba cloud's real-time computing version of Flink passed the stability test of big data products of the Institute of ICT

Esp32-c3 learning and testing WiFi (II. Wi Fi distribution - smart_config mode and BlueIf mode)

Cross platform plug-in flutter for displaying local notifications_ local_ notifications
![[set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)](/img/c2/87358af6b2b2892a6eceb751b3b60c.jpg)
[set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)
随机推荐
@RequestMapping
雇佣收银员(差分约束)
[set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)
The 19th Zhejiang I. barbecue
1110 complete binary tree (25 points)
leetcode406. Rebuild the queue based on height
Shuttle + Alluxio 加速内存Shuffle起飞
Interface frequency limit access
Market status and development prospect prediction of global neutral silicone sealant industry in 2022
First + only! Alibaba cloud's real-time computing version of Flink passed the stability test of big data products of the Institute of ICT
I've seen a piece of code in the past. I don't know what I'm doing. I can review it when I have time
Notes | numpy-10 Iterative array
Without 50W bride price, my girlfriend was forcibly dragged away. What should I do
并发操作-内存交互操作
Market status and development prospects of the global autonomous marine glider industry in 2022
MediaTek 2023 IC written examination approved in advance (topic)
112 stucked keyboard (20 points)
Silent authorization login and registration of wechat applet
leetcode860. Lemonade change
"Niuke brush Verilog" part II Verilog advanced challenge

