失去“的MediaPlayer'(放;其他变量)时,旋转设备 [英] Losing 'MediaPlayer' (& other Variables) when Device is Rotated

查看:156
本文介绍了失去“的MediaPlayer'(放;其他变量)时,旋转设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个音乐播放器的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

这篇关于失去“的MediaPlayer'(放;其他变量)时,旋转设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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