首页 文章

Android中的媒体播放器要求

提问于
浏览
1

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

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

  • 我想在媒体尚未开始播放时显示进度状态 . 它应该显示在第二个屏幕的顶部,并且一旦媒体开始播放就应该消失 . 我怎么做?是否要在启动服务的Activity或服务本身中完成某些操作?

  • 现在播放媒体时,我想通过按后退按钮返回第一个屏幕(或者可以退出应用程序) . 但音乐应继续播放,除非我从第一个屏幕选择其他电台(新音乐将开始),或从通知面板返回应用程序然后停止音乐 .

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

谢谢

2 回答

  • 0

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

    Service lifecycle reference

  • 1

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

    • 正如您所说的创建一个活动,其中显示了播放/暂停媒体控件 . 各个服务将继续在后台发挥作用 .

    • 第二种方式是你可以 initialize mediaplayer object 在你的游戏活动中 . 不要担心你的媒体播放器将继续在后台播放,直到除非它还没有 killed from task manager . ( Mind it ,即使您正在关闭活动,媒体播放器也将继续播放音乐)

    第二种方式,让你的选择和游戏活动 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 
    
      }
      }
    

相关问题