आपका मीडिया सेशन कॉलबैक, प्लेयर को कंट्रोल करने, ऑडियो फ़ोकस मैनेज करने,
और मीडिया सत्र और मीडिया ब्राउज़र सेवा के साथ संवाद कर सकते हैं. ध्यान दें कि
कॉलबैक के जवाब देने वाला MediaSession
लॉजिक, एक जैसा होना चाहिए. व्यवहार
कॉलबैक का नाम, कॉलर की पहचान पर निर्भर नहीं होना चाहिए. इसका मतलब यह हो सकता है कि
उसी ऐप्लिकेशन में कोई गतिविधि जिस पर MediaSession
चल रहा हो या कोई दूसरा ऐप्लिकेशन
MediaController
को MediaSession
से कनेक्ट किया गया.
यहां दी गई टेबल में इस बारे में खास जानकारी दी गई है कि इन टास्क को, कॉलबैक में किस तरह बांटा जाता है.
onPlay() | onPause() | onStop() | |
ऑडियो फ़ोकस | आपके OnAudioFocusChangeListener में requestFocus() पास हो रहा है.हमेशा पहले requestFocus() को कॉल करें, फ़ोकस सही होने पर ही आगे बढ़ें.
|
abandonAudioFocus()
|
|
सेवा | startService()
|
stopSelf()
|
|
मीडिया सेशन | setActive(true) अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है
- मेटाडेटा और स्थिति अपडेट करें |
- मेटाडेटा और स्थिति अपडेट करें | setActive(false) अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है
- मेटाडेटा और स्थिति अपडेट करें |
प्लेयर लागू करना | प्लेयर शुरू करें | प्लेयर को रोकें | प्लेयर को बंद करें |
आवाज़ आ रही है | अपना BroadcastReceiver रजिस्टर करें
|
अपने BroadcastReceiver का रजिस्ट्रेशन रद्द करें
|
|
सूचनाएं | startForeground(notification)
|
stopForeground(false)
|
stopForeground(false)
|
यहां कॉलबैक के लिए एक सैंपल फ़्रेमवर्क दिया गया है:
private val intentFilter = IntentFilter(ACTION_AUDIO_BECOMING_NOISY)
// Defined elsewhere...
private lateinit var afChangeListener: AudioManager.OnAudioFocusChangeListener
private val myNoisyAudioStreamReceiver = BecomingNoisyReceiver()
private lateinit var myPlayerNotification: MediaStyleNotification
private lateinit var mediaSession: MediaSessionCompat
private lateinit var service: MediaBrowserService
private lateinit var player: SomeKindOfPlayer
private lateinit var audioFocusRequest: AudioFocusRequest
private val callback = object: MediaSessionCompat.Callback() {
override fun onPlay() {
val am = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
// Request audio focus for playback, this registers the afChangeListener
audioFocusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN).run {
setOnAudioFocusChangeListener(afChangeListener)
setAudioAttributes(AudioAttributes.Builder().run {
setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
build()
})
build()
}
val result = am.requestAudioFocus(audioFocusRequest)
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// Start the service
startService(Intent(context, MediaBrowserService::class.java))
// Set the session active (and update metadata and state)
mediaSession.isActive = true
// start the player (custom call)
player.start()
// Register BECOME_NOISY BroadcastReceiver
registerReceiver(myNoisyAudioStreamReceiver, intentFilter)
// Put the service in the foreground, post notification
service.startForeground(id, myPlayerNotification)
}
}
public override fun onStop() {
val am = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
// Abandon audio focus
am.abandonAudioFocusRequest(audioFocusRequest)
unregisterReceiver(myNoisyAudioStreamReceiver)
// Stop the service
service.stopSelf()
// Set the session inactive (and update metadata and state)
mediaSession.isActive = false
// stop the player (custom call)
player.stop()
// Take the service out of the foreground
service.stopForeground(false)
}
public override fun onPause() {
val am = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
// Update metadata and state
// pause the player (custom call)
player.pause()
// unregister BECOME_NOISY BroadcastReceiver
unregisterReceiver(myNoisyAudioStreamReceiver)
// Take the service out of the foreground, retain the notification
service.stopForeground(false)
}
}
private IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
// Defined elsewhere...
private AudioManager.OnAudioFocusChangeListener afChangeListener;
private BecomingNoisyReceiver myNoisyAudioStreamReceiver = new BecomingNoisyReceiver();
private MediaStyleNotification myPlayerNotification;
private MediaSessionCompat mediaSession;
private MediaBrowserService service;
private SomeKindOfPlayer player;
private AudioFocusRequest audioFocusRequest;
MediaSessionCompat.Callback callback = new
MediaSessionCompat.Callback() {
@Override
public void onPlay() {
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
// Request audio focus for playback, this registers the afChangeListener
AudioAttributes attrs = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build();
audioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setOnAudioFocusChangeListener(afChangeListener)
.setAudioAttributes(attrs)
.build();
int result = am.requestAudioFocus(audioFocusRequest);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// Start the service
startService(new Intent(context, MediaBrowserService.class));
// Set the session active (and update metadata and state)
mediaSession.setActive(true);
// start the player (custom call)
player.start();
// Register BECOME_NOISY BroadcastReceiver
registerReceiver(myNoisyAudioStreamReceiver, intentFilter);
// Put the service in the foreground, post notification
service.startForeground(id, myPlayerNotification);
}
}
@Override
public void onStop() {
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
// Abandon audio focus
am.abandonAudioFocusRequest(audioFocusRequest);
unregisterReceiver(myNoisyAudioStreamReceiver);
// Stop the service
service.stopSelf();
// Set the session inactive (and update metadata and state)
mediaSession.setActive(false);
// stop the player (custom call)
player.stop();
// Take the service out of the foreground
service.stopForeground(false);
}
@Override
public void onPause() {
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
// Update metadata and state
// pause the player (custom call)
player.pause();
// unregister BECOME_NOISY BroadcastReceiver
unregisterReceiver(myNoisyAudioStreamReceiver);
// Take the service out of the foreground, retain the notification
service.stopForeground(false);
}
};