问题描述
|
我正在用Android开发媒体播放器应用程序。这是一个冗长的解释,但请仔细阅读并提出解决方案
该应用程序具有显示广播电台列表的第一个屏幕。当用户单击这些广播电台中的任何一个时,应用程序将转到显示播放,暂停和停止控件的第二个屏幕。第二个屏幕中的活动还会启动一项服务,该服务准备媒体播放器并开始播放媒体。现在我有两个要求
我想在媒体尚未开始播放时显示进度状态。它应该显示在第二个屏幕的顶部,并在媒体开始播放后消失。我怎么做?在启动服务的活动或服务本身中要做什么?
现在,当媒体正在播放时,我想通过按“后退”按钮返回到第一个屏幕(或者可能退出应用程序)。但是,除非我从第一个屏幕中选择其他广播电台(新音乐将开始播放),或者从通知面板返回到应用程序然后停止音乐,否则音乐应继续播放。
有人可以建议最好的方法吗?有没有可用的示例代码来实现这一目标?
谢谢
解决方法
要显示状态对话框,请查看UI活动中调用的ProgressDialog。要使服务继续播放,您需要使用startService而不是bindService。
服务生命周期参考
,关于第二个问题,我有解决办法。
有两种方法可以做到这一点。
正如您所讲的,创建一个Activity,它显示播放/暂停媒体控件。相应的服务将继续在后台运行。
第二种方式是您可以在您的播放活动中初始化mediaplayer对象。不要担心您的mediaplayer会继续在后台播放,直到被任务管理器杀死为止。 (请记住,即使您正在关闭活动,mediaplayer也将继续播放音乐)
对于第二种方法,请同时进行选择和播放活动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
}
}