diff options
Diffstat (limited to 'packages/SystemUI/src/com/android/systemui/media/NotificationPlayer.java')
-rw-r--r-- | packages/SystemUI/src/com/android/systemui/media/NotificationPlayer.java | 44 |
1 files changed, 38 insertions, 6 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/media/NotificationPlayer.java b/packages/SystemUI/src/com/android/systemui/media/NotificationPlayer.java index f8b347c..803a014 100644 --- a/packages/SystemUI/src/com/android/systemui/media/NotificationPlayer.java +++ b/packages/SystemUI/src/com/android/systemui/media/NotificationPlayer.java @@ -17,6 +17,7 @@ package com.android.systemui.media; import android.content.Context; +import android.media.AudioAttributes; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; @@ -45,11 +46,11 @@ public class NotificationPlayer implements OnCompletionListener { Context context; Uri uri; boolean looping; - int stream; + AudioAttributes attributes; long requestTime; public String toString() { - return "{ code=" + code + " looping=" + looping + " stream=" + stream + return "{ code=" + code + " looping=" + looping + " attributes=" + attributes + " uri=" + uri + " }"; } } @@ -79,7 +80,7 @@ public class NotificationPlayer implements OnCompletionListener { (AudioManager) mCmd.context.getSystemService(Context.AUDIO_SERVICE); try { MediaPlayer player = new MediaPlayer(); - player.setAudioStreamType(mCmd.stream); + player.setAudioAttributes(mCmd.attributes); player.setDataSource(mCmd.context, mCmd.uri); player.setLooping(mCmd.looping); player.prepare(); @@ -90,10 +91,12 @@ public class NotificationPlayer implements OnCompletionListener { if (mAudioManagerWithAudioFocus == null) { if (mDebug) Log.d(mTag, "requesting AudioFocus"); if (mCmd.looping) { - audioManager.requestAudioFocus(null, mCmd.stream, + audioManager.requestAudioFocus(null, + AudioAttributes.toLegacyStreamType(mCmd.attributes), AudioManager.AUDIOFOCUS_GAIN); } else { - audioManager.requestAudioFocus(null, mCmd.stream, + audioManager.requestAudioFocus(null, + AudioAttributes.toLegacyStreamType(mCmd.attributes), AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); } mAudioManagerWithAudioFocus = audioManager; @@ -280,7 +283,9 @@ public class NotificationPlayer implements OnCompletionListener { * (see {@link MediaPlayer#setLooping(boolean)}) * @param stream the AudioStream to use. * (see {@link MediaPlayer#setAudioStreamType(int)}) + * @deprecated use {@link #play(Context, Uri, boolean, AudioAttributes)} instead. */ + @Deprecated public void play(Context context, Uri uri, boolean looping, int stream) { Command cmd = new Command(); cmd.requestTime = SystemClock.uptimeMillis(); @@ -288,7 +293,34 @@ public class NotificationPlayer implements OnCompletionListener { cmd.context = context; cmd.uri = uri; cmd.looping = looping; - cmd.stream = stream; + cmd.attributes = new AudioAttributes.Builder().setInternalLegacyStreamType(stream).build(); + synchronized (mCmdQueue) { + enqueueLocked(cmd); + mState = PLAY; + } + } + + /** + * Start playing the sound. It will actually start playing at some + * point in the future. There are no guarantees about latency here. + * Calling this before another audio file is done playing will stop + * that one and start the new one. + * + * @param context Your application's context. + * @param uri The URI to play. (see {@link MediaPlayer#setDataSource(Context, Uri)}) + * @param looping Whether the audio should loop forever. + * (see {@link MediaPlayer#setLooping(boolean)}) + * @param attributes the AudioAttributes to use. + * (see {@link MediaPlayer#setAudioAttributes(AudioAttributes)}) + */ + public void play(Context context, Uri uri, boolean looping, AudioAttributes attributes) { + Command cmd = new Command(); + cmd.requestTime = SystemClock.uptimeMillis(); + cmd.code = PLAY; + cmd.context = context; + cmd.uri = uri; + cmd.looping = looping; + cmd.attributes = attributes; synchronized (mCmdQueue) { enqueueLocked(cmd); mState = PLAY; |