প্লেয়ারকে নিয়ন্ত্রণ করতে, অডিও ফোকাস পরিচালনা করতে এবং মিডিয়া সেশন এবং মিডিয়া ব্রাউজার পরিষেবার সাথে যোগাযোগ করতে আপনার মিডিয়া সেশন কলব্যাক বিভিন্ন API-এ কল পদ্ধতিগুলিকে ব্যবহার করে৷ নোট করুন যে MediaSession
লজিক যা কলব্যাকের প্রতিক্রিয়া জানায় তা অবশ্যই সামঞ্জস্যপূর্ণ হতে হবে। কলব্যাকের আচরণ অবশ্যই কলকারীর পরিচয়ের উপর নির্ভর করবে না, যা MediaSession
বা MediaSession
এর সাথে সংযুক্ত একটি MediaController
সহ অন্য কোনো অ্যাপ চালানোর একই অ্যাপে একটি কার্যকলাপ হতে পারে।
নিচের সারণীটি সারসংক্ষেপ করে কিভাবে এই কাজগুলো কলব্যাক জুড়ে বিতরণ করা হয়।
অনপ্লে() | অনপজ() | অনস্টপ() | |
অডিও ফোকাস | requestFocus() আপনার OnAudioFocusChangeListener এ পাস হচ্ছে।সর্বদা প্রথমে 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);
}
};