首页 文章

背景音乐自动停止

提问于
浏览
0

我用Splash Music制作了一个应用程序 . 但每当我进入应用程序的偏好时,音乐会自动停止,然后永远不会播放,直到我重新启动应用程序 . 当我打开一个活动时,情况也是如此,该活动告诉手机是处于“正常”还是处于“静音”模式 .

  • 这种奇怪行为的原因是什么?这是Splash音乐代码,我检查是否播放音乐..

公共类SplashScreen扩展Activity {

MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splashscreen);

    mp= MediaPlayer.create(SplashScreen.this, R.raw.got);
    SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
;
if(pref.getBoolean("music", true))
{
    mp.start();
}
if(pref.getBoolean("loop", true))
{
    mp.setLooping(true);
}
Thread timer= new Thread()
{
    public void run()
    {
    try
    {
        sleep(5000);
        Class ourclass = Class.forName("com.umer.practice2.Menu");
        Intent myintent= new Intent(SplashScreen.this,ourclass);
        startActivity(myintent);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    }
      };
    timer.start();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}

}

谢谢

2 回答

  • 0

    在服务中运行应该可以帮助您 .

  • 0

    这是因为您正在主线程上创建和播放媒体播放器对象 . 当您的活动暂停时,您正在完成停止媒体播放器的活动 . 如果您想让音乐独立于活动,请将其转移到服务中 .

相关问题