Android 中的媒体播放器要求

发布于 2024-11-01 08:43:56 字数 463 浏览 5 评论 0原文

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

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

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

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

谢谢

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

  1. I want to show a progress status while the media has not started playing. It should be shown on top of the second screen & should disappear once the media has started playing. How do I do that? Is something is to be done in the Activity that started the service or in the Service itself?
  2. Now when the media is playing, I want to go back to the first screen by pressing the back button(or may be exit the application). But the music should keep on playing unless I select some other radio station form the first screen(new music will start), or come back to the application from the notification panel & then stop the music.

Can someone suggest the best way to do this? Are there any sample codes available to achieve this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笨笨の傻瓜 2024-11-08 08:43:56

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

服务生命周期参考

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.

Service lifecycle reference

罪#恶を代价 2024-11-08 08:43:56

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

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

  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 

  }
  }

For your second Question , i have solution .
There are two ways to do it .

  1. As you are telling create one Activity ,which shows play/pause media controls. And Respective Service will continue to play in Background.

  2. 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 

  }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文