1

我正在 Android 中开发一个媒体播放器应用程序。它的解释有点冗长,但请仔细阅读并提出解决方案

该应用程序具有显示广播电台列表的第一个屏幕。当用户单击这些广播电台中的任何一个时,应用程序会转到显示播放、暂停和停止控件的第二个屏幕。第二个屏幕中的活动还启动了准备媒体播放器并开始播放媒体的服务。现在我有两个要求

  1. 我想在媒体尚未开始播放时显示进度状态。它应该显示在第二个屏幕的顶部,并且在媒体开始播放后应该消失。我怎么做?是在启动服务的 Activity 中还是在服务本身中做些什么?
  2. 现在当媒体播放时,我想通过按后退按钮返回第一个屏幕(或者可能退出应用程序)。但是音乐应该继续播放,除非我从第一个屏幕选择其他广播电台(新音乐将开始),或者从通知面板返回应用程序然后停止音乐。

有人可以建议最好的方法吗?是否有任何示例代码可用于实现此目的?

谢谢

4

2 回答 2

1

要显示状态对话框,请查看从您的 UI 活动调用的 ProgressDialog。要使服务继续播放,您需要使用 startService 而不是 bindService。

服务生命周期参考

于 2011-04-12T18:45:44.090 回答
0

对于您的第二个问题,我有解决方案。有两种方法可以做到这一点。

  1. 正如您所说,创建一个 Activity ,它显示播放/暂停媒体控件。相应的服务将继续在后台播放。

  2. 第二种方法是你可以在你的播放活动中初始化媒体播放器对象。不要担心你的媒体播放器会继续在后台播放,直到它没有被任务管理器杀死。(请注意,即使您关闭活动,媒体播放器也会继续播放音乐)

对于第二种方式,使您的选择和播放活动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 

  }
  }
于 2013-04-27T10:57:55.547 回答