From d70ad7a12871df3af73220cad19f5ecf45ef36d0 Mon Sep 17 00:00:00 2001 From: Jean-Michel Trivi Date: Tue, 15 Jul 2014 16:34:17 -0700 Subject: [PATCH] MediaPlayer factory method with audio attributes and session ID Change-Id: I4e7f633c84c90581e849af2865f5be84a026f32f --- api/current.txt | 2 ++ media/java/android/media/MediaPlayer.java | 44 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/api/current.txt b/api/current.txt index a6b679304c5f..cbb4e7b82197 100644 --- a/api/current.txt +++ b/api/current.txt @@ -14954,7 +14954,9 @@ package android.media { method public void attachAuxEffect(int); method public static android.media.MediaPlayer create(android.content.Context, android.net.Uri); method public static android.media.MediaPlayer create(android.content.Context, android.net.Uri, android.view.SurfaceHolder); + method public static android.media.MediaPlayer create(android.content.Context, android.net.Uri, android.view.SurfaceHolder, android.media.AudioAttributes, int); method public static android.media.MediaPlayer create(android.content.Context, int); + method public static android.media.MediaPlayer create(android.content.Context, int, android.media.AudioAttributes, int); method public void deselectTrack(int) throws java.lang.IllegalStateException; method public int getAudioSessionId(); method public int getCurrentPosition(); diff --git a/media/java/android/media/MediaPlayer.java b/media/java/android/media/MediaPlayer.java index 5e398c6429e8..b15bd69eb489 100644 --- a/media/java/android/media/MediaPlayer.java +++ b/media/java/android/media/MediaPlayer.java @@ -824,9 +824,30 @@ public class MediaPlayer implements SubtitleController.Listener * @return a MediaPlayer object, or null if creation failed */ public static MediaPlayer create(Context context, Uri uri, SurfaceHolder holder) { + int s = AudioSystem.newAudioSessionId(); + return create(context, uri, holder, null, s > 0 ? s : 0); + } + + /** + * Same factory method as {@link #create(Context, Uri, SurfaceHolder)} but that lets you specify + * the audio attributes and session ID to be used by the new MediaPlayer instance. + * @param context the Context to use + * @param uri the Uri from which to get the datasource + * @param holder the SurfaceHolder to use for displaying the video, may be null. + * @param audioAttributes the {@link AudioAttributes} to be used by the media player. + * @param audioSessionId the audio session ID to be used by the media player, + * see {@link AudioManager#allocateAudioSessionId()} to obtain a new session. + * @return a MediaPlayer object, or null if creation failed + */ + public static MediaPlayer create(Context context, Uri uri, SurfaceHolder holder, + AudioAttributes audioAttributes, int audioSessionId) { try { MediaPlayer mp = new MediaPlayer(); + final AudioAttributes aa = audioAttributes != null ? audioAttributes : + new AudioAttributes.Builder().build(); + mp.setAudioAttributes(aa); + mp.setAudioSessionId(audioSessionId); mp.setDataSource(context, uri); if (holder != null) { mp.setDisplay(holder); @@ -866,11 +887,34 @@ public class MediaPlayer implements SubtitleController.Listener * @return a MediaPlayer object, or null if creation failed */ public static MediaPlayer create(Context context, int resid) { + int s = AudioSystem.newAudioSessionId(); + return create(context, resid, null, s > 0 ? s : 0); + } + + /** + * Same factory method as {@link #create(Context, int)} but that lets you specify the audio + * attributes and session ID to be used by the new MediaPlayer instance. + * @param context the Context to use + * @param resid the raw resource id (R.raw.<something>) for + * the resource to use as the datasource + * @param audioAttributes the {@link AudioAttributes} to be used by the media player. + * @param audioSessionId the audio session ID to be used by the media player, + * see {@link AudioManager#allocateAudioSessionId()} to obtain a new session. + * @return a MediaPlayer object, or null if creation failed + */ + public static MediaPlayer create(Context context, int resid, + AudioAttributes audioAttributes, int audioSessionId) { try { AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid); if (afd == null) return null; MediaPlayer mp = new MediaPlayer(); + + final AudioAttributes aa = audioAttributes != null ? audioAttributes : + new AudioAttributes.Builder().build(); + mp.setAudioAttributes(aa); + mp.setAudioSessionId(audioSessionId); + mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); afd.close(); mp.prepare(); -- 2.11.0