Share
Embed
Implementing Audio on Android
Caren
November 05, 2017
Transcript
92 slides
-
How is Audio Processed? Latency - Time delay as a
signal passes through a system -
Latency - Time delay as a signal passes through a
system How is Audio Processed? -
Latency - Time delay as a signal passes through a
system How is Audio Processed? -
Latency - Time delay as a signal passes through a
system How is Audio Processed? -
How is Audio Processed? Application Audio Framework App audio source
Resample Effects Mixer -
How is Audio Processed? Application Audio Framework Device Hardware App
audio source Resample Effects Mixer DAC -
How is Audio Processed? Audio hardware consumes data at constant
rate When we pass audio files to be played, we have to make sure it gets processed in time to meet the deadlines -
How is Audio Processed? Audio hardware consumes data at constant
rate When we pass audio files to be played, we have to make sure it gets processed in time to meet the deadlines Otherwise, when deadlines are missed, you can get silent sounds or weird ‘static’ sounds -
MediaPlayer Easily fetch, decode and play audio with minimal setup
Supports : Local resources Internal URIs (from content resolver) External URLs (http streaming) -
MediaPlayer void playAudioFile(String audioFilePath) { MediaPlayer mediaPlayer = new MediaPlayer();
try { mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource(audioFilePath); mediaPlayer.prepare(); mediaPlayer.start(); } catch (IOException e) { Log.e(TAG, "Error playing audio file: " + audioFilePath); mediaPlayer.release(); } } -
MediaPlayer void playAudioFile(String audioFilePath) { MediaPlayer mediaPlayer = new MediaPlayer();
try { mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource(audioFilePath); mediaPlayer.prepare(); mediaPlayer.start(); } catch (IOException e) { Log.e(TAG, "Error playing audio file: " + audioFilePath); mediaPlayer.release(); } } -
MediaPlayer void playAudioFile(String audioFilePath) { MediaPlayer mediaPlayer = new MediaPlayer();
try { mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource(audioFilePath); mediaPlayer.prepare(); mediaPlayer.start(); } catch (IOException e) { Log.e(TAG, "Error playing audio file: " + audioFilePath); mediaPlayer.release(); } } -
MediaPlayer void playAudioFile(String audioFilePath) { MediaPlayer mediaPlayer = new MediaPlayer();
try { mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource(audioFilePath); mediaPlayer.prepare(); mediaPlayer.start(); } catch (IOException e) { Log.e(TAG, "Error playing audio file: " + audioFilePath); mediaPlayer.release(); } } -
MediaPlayer void playAudioFile(String audioFilePath) { MediaPlayer mediaPlayer = new MediaPlayer();
try { mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource(audioFilePath); mediaPlayer.prepare(); mediaPlayer.start(); } catch (IOException e) { Log.e(TAG, "Error playing audio file: " + audioFilePath); mediaPlayer.release(); } } -
MediaPlayer void playAudioFile(String audioFilePath) { MediaPlayer mediaPlayer = new MediaPlayer();
try { mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource(audioFilePath); mediaPlayer.prepare(); mediaPlayer.start(); } catch (IOException e) { Log.e(TAG, "Error playing audio file: " + audioFilePath); mediaPlayer.release(); } } -
MediaPlayer void playAudioFile(String audioFilePath) { MediaPlayer mediaPlayer = new MediaPlayer();
try { mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource(audioFilePath); mediaPlayer.prepare(); mediaPlayer.start(); } catch (IOException e) { Log.e(TAG, "Error playing audio file: " + audioFilePath); mediaPlayer.release(); } } -
MediaPlayer MediaPlayer is state based, can easily caused undesired behaviors
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource(audioFilePath); mediaPlayer.prepare(); mediaPlayer.start(); or mediaPlayer.pause() or mediaPlayer.seekTo(); IDLE -
MediaPlayer MediaPlayer is state based, can easily caused undesired behaviors
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource(audioFilePath); mediaPlayer.prepare(); mediaPlayer.start(); or mediaPlayer.pause() or mediaPlayer.seekTo(); INITIALIZED -
MediaPlayer MediaPlayer is state based, can easily caused undesired behaviors
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource(audioFilePath); mediaPlayer.prepare(); mediaPlayer.start(); or mediaPlayer.pause() or mediaPlayer.seekTo(); PREPARED -
MediaPlayer MediaPlayer is state based, can easily caused undesired behaviors
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource(audioFilePath); mediaPlayer.prepare(); mediaPlayer.start(); or mediaPlayer.pause() or mediaPlayer.seekTo(); -
MediaPlayer : Common Errors Not handling onError Performing actions in
wrong state Unsupported file types -
MediaPlayer : Common Errors Not handling onError Performing actions in
wrong state Unsupported file types Not releasing after use -
SoundPool Uses the MediaPlayer service to decode the audio Collection
of samples from the APK that can be loaded into memory -
SoundPool Uses the MediaPlayer service to decode the audio Collection
of samples from the APK that can be loaded into memory Able to manage number of audio streams IE : able to do sound mixing -
SoundPool Uses the MediaPlayer service to decode the audio Collection
of samples from the APK that can be loaded into memory Able to manage number of audio streams IE : able to do sound mixing Can set priority when playing sound -
SoundPool SoundPool mSoundPool = new SoundPool.Builder() .setAudioAttributes(mAudioAttributes) .setMaxStreams(MAX_SOUND_STREAMS) .build();
int loadedSoundId = mSoundPool.load(audioFilePath, 1); mSoundPool.play(loadedSoundId, volumeToPlaySound, volumeToPlaySound, REGULAR_PRIORITY_SOUND, NO_LOOP, DEFAULT_PLAYBACK_RATE); -
SoundPool SoundPool mSoundPool = new SoundPool.Builder() .setAudioAttributes(mAudioAttributes) .setMaxStreams(MAX_SOUND_STREAMS) .build();
int loadedSoundId = mSoundPool.load(audioFilePath, 1); mSoundPool.play(loadedSoundId, volumeToPlaySound, volumeToPlaySound, REGULAR_PRIORITY_SOUND, NO_LOOP, DEFAULT_PLAYBACK_RATE); -
SoundPool SoundPool mSoundPool = new SoundPool.Builder() .setAudioAttributes(mAudioAttributes) .setMaxStreams(MAX_SOUND_STREAMS) .build();
int loadedSoundId = mSoundPool.load(audioFilePath, 1); mSoundPool.play(loadedSoundId, volumeToPlaySound, volumeToPlaySound, REGULAR_PRIORITY_SOUND, NO_LOOP, DEFAULT_PLAYBACK_RATE); -
ExoPlayer Open source project (not part of Android framework) Pros:
Ability to update player along with application -
ExoPlayer Open source project (not part of Android framework) Pros:
Ability to update player along with application Fewer device specific issues across different versions of Android -
ExoPlayer Open source project (not part of Android framework) Pros:
Ability to update player along with application Fewer device specific issues across different versions of Android Ability to customize and extend the player -
ExoPlayer : Talks ExoPlayer : Flexible Media Playback for Android
Google IO ‘17 Advanced Exoplayer Effie Barak -
AudioFocus Audio from different apps can play simultaneously (system does
not have control over which streams play) -
AudioFocus Audio from different apps can play simultaneously (system does
not have control over which streams play) If individual apps don’t properly handle audio focus it will result in a bad user experience -
AudioFocus : Scenario User is currently in your app and
playing audio -
AudioFocus : Scenario User is currently in your app and
playing audio User goes into another media app that plays audio -
AudioFocus : Scenario User is currently in your app and
playing audio User goes into another media app that plays audio If not properly handled, both apps will now play their audio stream and nothing will be heard -
AudioFocus : Proper Flow Request audio focus Start playback after
audio focus is successfully granted -
AudioFocus : Proper Flow Request audio focus Start playback after
audio focus is successfully granted Handle audio focus changes through onAudioFocusChangedListener() -
AudioFocus : Intentions AUDIOFOCUS_GAIN : Indefinite AUDIOFOCUS_TRANSIENT : temporary (phone
call) AUDIOFOCUS_TRANSIENT_MAY_DUCK : temporary (notification) -
AudioFocus : Implementation AudioFocusRequest mAudioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) .setAudioAttributes(mAudioAttributes) .setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(...) .build(); int focusRequest = mAudioManager.requestAudioFocus(mAudioFocusRequest); -
AudioFocus : Implementation AudioFocusRequest mAudioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) .setAudioAttributes(mAudioAttributes) .setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(...) .build(); int focusRequest = mAudioManager.requestAudioFocus(mAudioFocusRequest); -
AudioFocus : Implementation AudioFocusRequest mAudioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) .setAudioAttributes(mAudioAttributes) .setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(...) .build(); int focusRequest = mAudioManager.requestAudioFocus(mAudioFocusRequest); -
AudioFocus : Implementation AudioFocusRequest mAudioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) .setAudioAttributes(mAudioAttributes) .setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(...) .build(); int focusRequest = mAudioManager.requestAudioFocus(mAudioFocusRequest); -
Sounds on the June Oven Common sounds vs Special sounds
Themes Emergency sounds -
Sounds on the June Oven Common sounds (tap, cancel, accept,
back) are eagerly loaded into a SoundPool for low latency playback. -
Sounds on the June Oven Common sounds (tap, cancel, accept,
back) are eagerly loaded into a SoundPool for low latency playback. All other sounds are played with the MediaPlayer -
Sounds on the June Oven Common sounds (tap, cancel, accept,
back) are eagerly loaded into a SoundPool for low latency playback. All other sounds are played with the MediaPlayer Sounds can request to be played immediately -
Problems Encountered Creating pleasant experience on sliders ‘Wake’ sound Playing
sound on theme changed