如何Android的mediaplayers继续播放歌曲时,应用程序被关闭? [英] How do Android mediaplayers continuing playing songs when app is closed?

查看:414
本文介绍了如何Android的mediaplayers继续播放歌曲时,应用程序被关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道下一首歌曲如何播放一遍,就好像播放整张CD或播放列表应用程序被关闭...

Wondering how next songs are played once app is closed as if playing an entire CD or playlist...

推荐答案

媒体播放器只能播放一个音轨。什么的媒体播放器的事,就是监听的onCompletion事件和播放下一首曲目。

The media player only plays one audio track. What media players do, is listen on the onCompletion event and play the next track.

MediaPlayer正在必然的过程中,不活动,所以它一直播放,只要进程运行。活性可能暂停或销毁,但是这将不会影响内部线程的MediaPlayer使用

The MediaPlayer is bound to the process, not the activity, so it keeps playing as long as the process runs. The activity might be paused or destroyed, but that won't affect the internal thread that MediaPlayer uses.

我要建一个音频播放器学习Android的,你可以看到,播放音频文件服务<一href="https://github.com/augusto/android-sandbox/blob/master/src/com/augusto/mymediaplayer/services/AudioPlayer.java">here

I'm building an audio player to learn Android, you can see the service that plays audio files here

对于第一个注释:这个服务会在后台运行,并保持在您运行退出的应用程序,因为服务和活动的生命周期是不同的。

regarding the first comment: The service keeps running on the background and keeps running after you "exit" the application, because the lifecycle of the service and Activities are different.

为了玩的的下轨的,在服务上注册的MediaPlayer的回调,所以当一个音频流完成服务通知。当音频完成时,服务清理所用的MediaPlayer的资源,通过调用MediaPlayer.release(),然后创建一个全新的媒体播放器与下一个音轨播放而注册其自身被通知再次当音频轨道完成后,循环往复。)

In order to play the next track, the service registers a callback on the MediaPlayer so the service is informed when an audio stream completed. When the audio completes, the service cleans up the resources used by the MediaPlayer, by calling MediaPlayer.release(), and then creates a fresh new media player with the next audio track to play and registers itself to be notified again when that audio track completes, ad infinitum :).

在MediaPlayer类不明白的播放列表,所以服务负责previous跟踪完成后播放曲目。

The MediaPlayer class doesn't understand playlists, so the service is responsible for playing a track after the previous track completes.

在<一个href="https://github.com/augusto/android-sandbox/blob/master/src/com/augusto/mymediaplayer/services/AudioPlayer.java">AudioPlayer我创建的服务,活动队列中的AudioPlayer轨迹和AudioPlayer负责播放它们的顺序。

In the AudioPlayer service I've created, an activity queues tracks in the AudioPlayer and the AudioPlayer is responsible for playing them in order.

我希望这是明确的,并再次,如果你有一段时间了,请检查AudioPlayer服务的code我上面放。这不是纯粹的美,但它的工作。

I hope it's clear and again, if you have some time, please check the code of AudioPlayer service I've put above. It's not pure beauty, but it does its job.

这篇关于如何Android的mediaplayers继续播放歌曲时,应用程序被关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆