android音频开发技术分析

引言:要想把事情做到完美而不是完成,你就要注意每一个小的细节,不要把问题想的太简单。

 

对于Android的音频技术,在应用开发中并不复杂,但为了提高用户的体验,我们要注意几点重要的方面:

1.音量的调节功能必不可少,而且我们要尽量考虑到刚开始音量的大小情况。

2.对于当前播放的音频和将要转换的音频的考虑。其中包括永久的转换盒临时的转换,可以在其中加入duck特性。

永久获得焦点的代码:

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(RemoteControlReceiver);
   
// Start playback.
}

临时获得并带有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.不同的输出设备对音频播放的影响。包括耳机,蓝牙等输出设备。下面是检测硬件的代码:

if (isBluetoothA2dpOn()) {
   
// 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?
}

你可能感兴趣的:(android软件开发)