Media Player Requirements in Android
I am developing a media player application in Android. Its a bit of a lengthy explanation, but please go through it & suggest a solution
The application has a first screen that displays a list of radio stations. When the user clicks on any of these radio stations, the application goes to the second screen that displays the play, pause & stop controls. The activity in the second screen also initiates a service that prepares the Media Player & starts playing the media. Now I have two requirements
- I want to show a progress status while the media has not started playing. It should be shown on top of the second screen & should disappear once the media has started playing. How do I do that? Is something is t开发者_StackOverflowo be done in the Activity that started the service or in the Service itself?
- Now when the media is playing, I want to go back to the first screen by pressing the back button(or may be exit the application). But the music should keep on playing unless I select some other radio station form the first screen(new music will start), or come back to the application from the notification panel & then stop the music.
Can someone suggest the best way to do this? Are there any sample codes available to achieve this?
Thanks
To show a status dialog, look into ProgressDialog called from your UI activity. To cause a service to continue playing, you need to use startService rather than bindService.
Service lifecycle reference
For your second Question , i have solution . There are two ways to do it .
As you are telling create one Activity ,which shows play/pause media controls. And Respective Service will continue to play in Background.
Second way is that u can initialize mediaplayer object in your playing activity.Don't worry your mediaplayer will continue to play in background untill and unless it has not been killed from task manager. (Mind it , mediaplayer will continue to play music even if you are closing the activity)
For second way , make your both selection and playing activity SingleInstance
static MediaPlayer mediaplayer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radio_play_layout);
//here check for one thing, **mediaplayer is null or not**. If mediaplayer is null than ur activity is **starting** and u need to have data from selection activity that i need to play this station & If your mediaplayer is not null than you are coming from **notification or widget** , put some IntExtra with pendingIntent to verify that you are actually starting activity from notification or widget.
if(mediaplayer!=null && (getIntent.getIntExtra("verifier",0)==786)){
//show ur currently playing stations details and control butttons.becaz activity is started from notification or widget.
}else{
//Initialize your mediaplayer , setlayout & startplaying
}
}
精彩评论