当前位置:网站首页>Audio Focus Series: write a demo to understand audio focus and audiomananger
Audio Focus Series: write a demo to understand audio focus and audiomananger
2022-07-03 05:04:00 【zhangjin1120】
Catalog
Let's see the test results first :
- To open the first demo, Click on “start” wait a while ( Local audio loading takes time ), The sound of the car will be played in cycles .
- home Key back to desktop , Find cool dog music APP And open , Play a song , At this time logcat It will print
AUDIOFOCUS_LOSS,Demo App Will pause the play . - Manually pause the cool dog music player (log There will be no change , That is, there is no trigger
AUDIOFOCUS_GAIN). - set alarm , Alarm clock trigger ,log Show
AUDIOFOCUS_LOSS_TRANSIENT - Turn off the alarm clock ,log Show
AUDIOFOCUS_GAIN
![]() ![]() |
Demo Source code
The layout file is just a button , Write your own layout , complete Demo The code is as follows , The notes are very clear , No explanation .
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() {
// initialization AudioManager object
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// Application focus
mAudioManager.requestAudioFocus(mAudioFocusChange, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
AssetFileDescriptor fileDescriptor;
try {
// Get audio files
fileDescriptor = this.getAssets().openFd("11582.mp3");
// Instantiation MediaPlayer object
mMediaPlayer = new MediaPlayer();
// Set the playback stream type
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
// Set the playback source , There are multiple parameters to choose , Refer to relevant documents for details , This article aims to introduce audio focus
mMediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),
fileDescriptor.getStartOffset(),
fileDescriptor.getLength());
// Set up looping
mMediaPlayer.setLooping(true);
// Ready to monitor
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// Automatically play after preparation
mMediaPlayer.start();
}
});
// Asynchronous preparation
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();
}
/** * Focus change listener */
private AudioManager.OnAudioFocusChangeListener mAudioFocusChange = new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_LOSS:
// Lost focus for a long time
Log.d(TAG, "AUDIOFOCUS_LOSS");
stop();
// Release focus
mAudioManager.abandonAudioFocus(mAudioFocusChange);
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
// Temporary loss of focus
stop();
Log.d(TAG, "AUDIOFOCUS_LOSS_TRANSIENT");
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
// Temporarily lose focus and reduce sound
Log.d(TAG, "AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
break;
case AudioManager.AUDIOFOCUS_GAIN:
// Regain focus
Log.d(TAG, "AUDIOFOCUS_GAIN");
start();
break;
}
}
};
@Override
protected void onDestroy() {
super.onDestroy();
mMediaPlayer.release();
mAudioManager.abandonAudioFocus(mAudioFocusChange);
}
}
What are the practical scenarios of audio focus ?
- Turn on cool dog music and play a song , And on again B standing , Play the video , At this time, cool dog will automatically pause , Press the back key to exit the video playback , Cool dog will automatically resume playing music .
How does the official website describe the audio focus ?
边栏推荐
- Market status and development prospect prediction of global SoC Test Platform Industry in 2022
- Sprintf formatter abnormal exit problem
- sql语句模糊查询遇到的问题
- JS dynamic table creation
- Market status and development prospect prediction of global neutral silicone sealant industry in 2022
- MediaTek 2023 IC written examination approved in advance (topic)
- Market status and development prospect forecast of global heat curing adhesive industry in 2022
- "Pthread.h" not found problem encountered in compiling GCC
- The principle is simple, but I don't know how to use it? Understand "contemporaneous group model" in one article
- Market status and development prospect forecast of global button dropper industry in 2022
猜你喜欢

Keepalived热备与HAProxy

Valentine's day limited withdrawal guide: for one in 200 million of you

Uipath practice (08) - selector
![[research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached](/img/01/052928e7f20ca671cdc4c30ae55258.jpg)
[research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
![[Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology](/img/f1/d0dc4dc3fe49a2d2cd9e452a0ce31e.jpg)
[Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology

Promise

2022-02-12 daily clock in: problem fine brush

cookie session jwt

Do you know UVs in modeling?

Interface frequency limit access
随机推荐
Objects. Requirenonnull method description
Maximum continuous sub segment sum (dynamic programming, recursive, recursive)
1115 counting nodes in a BST (30 points)
Learning record of arouter principle
Distinguish between releases and snapshots in nexus private library
[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)
文献阅读_基于多模态数据语义融合的旅游在线评论有用性识别研究(中文文献)
The process of browser accessing the website
Based on RFC 3986 (unified resource descriptor (URI): general syntax)
1107 social clusters (30 points)
ZABBIX monitoring of lamp architecture (3): zabbix+mysql (to be continued)
论文阅读_ICD编码_MSMN
The principle is simple, but I don't know how to use it? Understand "contemporaneous group model" in one article
[set theory] relationship properties (symmetry | symmetry examples | symmetry related theorems | antisymmetry | antisymmetry examples | antisymmetry theorems)
Online VR model display - 3D visual display solution
1095 cars on campus (30 points)
Uipath practice (08) - selector
Silent authorization login and registration of wechat applet
[set theory] binary relation (example of binary relation operation | example of inverse operation | example of composite operation | example of limiting operation | example of image operation)
"Pthread.h" not found problem encountered in compiling GCC

