Introduction: If you want to make things perfect rather than complete them, you must pay attention to every small detail and do not think too simply about the problem.
Android audio technology is not complicated in application development. However, to improve the user experience, we should pay attention to the following important aspects:
1. The volume adjustment function is essential, and we should try to take into account the volume at the beginning.
2. Consider the currently played audio and the audio to be converted. This includes temporary conversions of permanent conversion boxes, where duck features can be added.
Code that permanently gets the focus:
AudioManager am = mContext. getSystemService (Context. AUDIO_SERVICE );
...
// Request audio focus for playback
Int result = am. requestAudioFocus (afChangeListener,
// Use the music stream.
AudioManager. STREAM_MUSIC,
// Request permanent focus.
AudioManager. AUDIOFOCUS_GAIN );
If (result = AudioManager. AUDIOFOCUS_REQUEST_GRANTED ){
Am. unregisterMediaButtonEventReceiver (remotecontrolpolicer );
// Start playback.
}
Code temporarily obtained with duck:
// Request audio focus for playback
Int result = am. requestAudioFocus (afChangeListener,
// Use the music stream.
AudioManager. STREAM_MUSIC,
// Request permanent focus.
AudioManager. AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK );
If (result = AudioManager. AUDIOFOCUS_REQUEST_GRANTED ){
// Start playback.
}
3. The impact of different output devices on audio playback. Including headphones, Bluetooth, and other output devices. The following code checks the hardware:
If (isw.tha2dpon ()){
// Adjust output for Bluetooth.
} Else if (isSpeakerphoneOn ()){
// Adjust output for Speakerphone.
} Else if (isWiredHeadsetOn ()){
// Adjust output for headsets
} Else {
// If audio plays and noone can hear it, is it still playing?
}