失去“的MediaPlayer'(放;其他变量)时,旋转设备变量、设备、MediaPlayer

2023-09-12 04:41:08 作者:黑翼天使

我要创建一个音乐播放器的Andr​​oid和它的主要工作。问题是,当我打开设备水平我失去所有的变量从活动(这是有道理的,因为它被破坏并重新创建)。

I'm creating a music player for Android and it's mostly working. The problem is that when I turn the device horizontally I lose all the variables from the Activity (which makes sense because it is destroyed and re-created).

我用包来存储播放器的onSaveInstanceState和放大器的状态尝试; onRestoreInstanceState但我仍然无法访问的媒体播放器。有没有办法通过像束在MediaPlayer的对象?我应该使用一个数据库,而不是?

I've tried using bundles to store the state of the player with onSaveInstanceState & onRestoreInstanceState but I still can't access the media player. Is there a way to pass objects like the MediaPlayer in bundles? Should I be using a database instead?

感谢

推荐答案

您应该使用服务来提供后台的音频播放功能,使 用户在不停止播放活动或旋转设备之间切换。

You should use a Service to Provides "background" audio playback capabilities, allowing the user to switch between activities or Rotate device without stopping playback.

查看 android_packages_apps_Music 它是开源的由CM在github上,它使用MediaPlaybackService延伸服务要做到这一点,结帐MediaPlaybackService.java

Check out android_packages_apps_Music which is opensource by CM on github , It use MediaPlaybackService extends Service to do this , checkout MediaPlaybackService.java

 
精彩推荐
图片推荐