Reputation: 1369
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
Can someone suggest the best way to do this? Are there any sample codes available to achieve this?
Thanks
Upvotes: 1
Views: 1005
Reputation: 773
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
}
}
Upvotes: 0
Reputation: 2376
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.
Upvotes: 1