Android中的媒体播放器要求

时间:2011-04-12 18:37:35

标签: android streaming android-mediaplayer

我正在Android中开发媒体播放器应用程序。这是一个冗长的解释,但请通过它&提出解决方案

应用程序有第一个显示电台列表的屏幕。当用户点击任何这些无线电台时,应用程序进入显示播放,暂停和播放的第二个屏幕。停止控制。第二个屏幕中的活动还启动了准备媒体播放器和播放器的服务。开始播放媒体。现在我有两个要求

  1. 我想在媒体尚未开始播放时显示进度状态。它应该显示在第二个屏幕和媒体开始播放后应该消失。我怎么做?是否要在启动服务的Activity或服务本身中完成某些操作?
  2. 现在播放媒体时,我想通过按后退按钮返回第一个屏幕(或者可以退出应用程序)。但是音乐应该继续播放,除非我从第一个屏幕选择一些其他电台(新音乐将开始),或者从通知面板&回到应用程序。然后停止音乐。
  3. 有人可以建议最好的方法吗?是否有可用于实现此目的的示例代码?

    由于

2 个答案:

答案 0 :(得分:1)

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

Service lifecycle reference

答案 1 :(得分:0)

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

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

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

  3. 第二种方式,进行选择和播放活动 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 
    
      }
      }