If your application intends to continue playing audio in the background when the Activity isn’t visible, a good approach is to keep your Media Player within a Service, controlling the media playback using Intents.Multiple applications might be installed on a given device, each configured to receive media key presses; therefore, you must also use the Audio Manager’s registerMediaButtonEventReceiver method to register your Receiver as the exclusive handler of media button presses, as shown in Listing 15-10, which both registers the media button event Receiver declared in your manifest and the local Broadcast Receiver that interprets the Intent when it’s passed through to the Activity. LISTING 15-10: Media button press Receiver manifest declaration // Register the Media Button Event Receiver to // listen for media button presses.AudioManager am =(AudioManager)getSystemService(Context.AUDIO_SERVICE); ComponentName component =new ComponentName(this, MediaControlReceiver.class); am.registerMediaButtonEventReceiver(component);// Register a local Intent Receiver that receives media button // presses from the Receiver registered in the manifest. activityMediaControlReceiver = new ActivityMediaControlReceiver(); IntentFilter filter =new IntentFilter(MediaControlReceiver.ACTION_MEDIA_BUTTON);registerReceiver(activityMediaControlReceiver, filter); code snippet PA4AD_Ch15_Media_Player/src/AudioPlayerActivity.java
đang được dịch, vui lòng đợi..
