OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.media.AudioManager; | 8 import android.media.AudioManager; |
9 | 9 |
10 import org.chromium.base.Log; | 10 import org.chromium.base.Log; |
(...skipping 16 matching lines...) Expand all Loading... |
27 private static final String TAG = "MediaSession"; | 27 private static final String TAG = "MediaSession"; |
28 | 28 |
29 // These need to match the values in native apps. | 29 // These need to match the values in native apps. |
30 public static final double DUCKING_VOLUME_MULTIPLIER = 0.2f; | 30 public static final double DUCKING_VOLUME_MULTIPLIER = 0.2f; |
31 public static final double DEFAULT_VOLUME_MULTIPLIER = 1.0f; | 31 public static final double DEFAULT_VOLUME_MULTIPLIER = 1.0f; |
32 | 32 |
33 private Context mContext; | 33 private Context mContext; |
34 private int mFocusType; | 34 private int mFocusType; |
35 private boolean mIsDucking = false; | 35 private boolean mIsDucking = false; |
36 | 36 |
37 // Native pointer to C++ content::MediaSessionAndroid. | 37 // Native pointer to C++ content::MediaSessionDelegateAndroid. |
38 private final long mNativeMediaSessionDelegateAndroid; | 38 // It will be set to 0 when the native MediaSessionDelegateAndroid object is
destroyed. |
| 39 private long mNativeMediaSessionDelegateAndroid; |
39 | 40 |
40 private MediaSessionDelegate(final Context context, long nativeMediaSessionD
elegateAndroid) { | 41 private MediaSessionDelegate(final Context context, long nativeMediaSessionD
elegateAndroid) { |
41 mContext = context; | 42 mContext = context; |
42 mNativeMediaSessionDelegateAndroid = nativeMediaSessionDelegateAndroid; | 43 mNativeMediaSessionDelegateAndroid = nativeMediaSessionDelegateAndroid; |
43 } | 44 } |
44 | 45 |
45 @CalledByNative | 46 @CalledByNative |
46 private static MediaSessionDelegate create(Context context, | 47 private static MediaSessionDelegate create(Context context, |
47 long nativeMediaSessionDelegateAn
droid) { | 48 long nativeMediaSessionDelegateAn
droid) { |
48 return new MediaSessionDelegate(context, nativeMediaSessionDelegateAndro
id); | 49 return new MediaSessionDelegate(context, nativeMediaSessionDelegateAndro
id); |
49 } | 50 } |
50 | 51 |
51 @CalledByNative | 52 @CalledByNative |
52 private void tearDown() { | 53 private void tearDown() { |
53 abandonAudioFocus(); | 54 abandonAudioFocus(); |
| 55 mNativeMediaSessionDelegateAndroid = 0; |
54 } | 56 } |
55 | 57 |
56 @CalledByNative | 58 @CalledByNative |
57 private boolean requestAudioFocus(boolean transientFocus) { | 59 private boolean requestAudioFocus(boolean transientFocus) { |
58 mFocusType = transientFocus ? AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY
_DUCK | 60 mFocusType = transientFocus ? AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY
_DUCK |
59 : AudioManager.AUDIOFOCUS_GAIN; | 61 : AudioManager.AUDIOFOCUS_GAIN; |
60 return requestAudioFocusInternal(); | 62 return requestAudioFocusInternal(); |
61 } | 63 } |
62 | 64 |
63 @CalledByNative | 65 @CalledByNative |
64 private void abandonAudioFocus() { | 66 private void abandonAudioFocus() { |
65 AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO
_SERVICE); | 67 AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO
_SERVICE); |
66 am.abandonAudioFocus(this); | 68 am.abandonAudioFocus(this); |
67 } | 69 } |
68 | 70 |
69 private boolean requestAudioFocusInternal() { | 71 private boolean requestAudioFocusInternal() { |
70 AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO
_SERVICE); | 72 AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO
_SERVICE); |
71 | 73 |
72 int result = am.requestAudioFocus(this, AudioManager.STREAM_MUSIC, mFocu
sType); | 74 int result = am.requestAudioFocus(this, AudioManager.STREAM_MUSIC, mFocu
sType); |
73 return result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED; | 75 return result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED; |
74 } | 76 } |
75 | 77 |
76 @Override | 78 @Override |
77 public void onAudioFocusChange(int focusChange) { | 79 public void onAudioFocusChange(int focusChange) { |
| 80 if (mNativeMediaSessionDelegateAndroid == 0) return; |
| 81 |
78 switch (focusChange) { | 82 switch (focusChange) { |
79 case AudioManager.AUDIOFOCUS_GAIN: | 83 case AudioManager.AUDIOFOCUS_GAIN: |
80 if (mIsDucking) { | 84 if (mIsDucking) { |
81 nativeOnSetVolumeMultiplier(mNativeMediaSessionDelegateAndro
id, | 85 nativeOnSetVolumeMultiplier(mNativeMediaSessionDelegateAndro
id, |
82 DEFAULT_VOLUME_MULTIPLIER); | 86 DEFAULT_VOLUME_MULTIPLIER); |
83 mIsDucking = false; | 87 mIsDucking = false; |
84 } else { | 88 } else { |
85 nativeOnResume(mNativeMediaSessionDelegateAndroid); | 89 nativeOnResume(mNativeMediaSessionDelegateAndroid); |
86 } | 90 } |
87 break; | 91 break; |
(...skipping 15 matching lines...) Expand all Loading... |
103 break; | 107 break; |
104 } | 108 } |
105 } | 109 } |
106 | 110 |
107 private native void nativeOnSuspend(long nativeMediaSessionDelegateAndroid,
boolean temporary); | 111 private native void nativeOnSuspend(long nativeMediaSessionDelegateAndroid,
boolean temporary); |
108 private native void nativeOnResume(long nativeMediaSessionDelegateAndroid); | 112 private native void nativeOnResume(long nativeMediaSessionDelegateAndroid); |
109 private native void nativeOnSetVolumeMultiplier(long nativeMediaSessionDeleg
ateAndroid, | 113 private native void nativeOnSetVolumeMultiplier(long nativeMediaSessionDeleg
ateAndroid, |
110 double volumeMultiplier); | 114 double volumeMultiplier); |
111 private native void nativeRecordSessionDuck(long nativeMediaSessionDelegateA
ndroid); | 115 private native void nativeRecordSessionDuck(long nativeMediaSessionDelegateA
ndroid); |
112 } | 116 } |
OLD | NEW |