0

嗨,我在这里做一个应用程序,如果单击后退按钮然后我重新打开应用程序意味着我需要从头开始播放音乐..如果单击主页按钮意味着我需要再次暂停音乐我重新打开应用程序意味着我需要在按下主页按钮的地方播放从那时起歌曲中间..我尝试在暂停()中使用下面的代码我释放声音...对于后退按钮它的工作,但如果再次按下主页按钮重新打开应用程序意味着应用程序不播放任何声音,我想要两个功能..如何不做任何建议我..

    public class MainActivity extends Activity {

Button b1;
MediaPlayer level;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    level=MediaPlayer.create(MainActivity.this,R.raw.bgmusic);
    level.setLooping(true);
    level.start(); 

     b1=(Button)findViewById(R.id.button1);
     b1.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub


            Intent i=new Intent(MainActivity.this,Activityyy2.class);
            startActivity(i);

        }
    });
}

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

    if (level != null) {
        if (level.isPlaying()) {

            level.stop();

        }
        level.release();
        level = null;
    }
}
    }
public class Activityyy2 extends Activity {
MediaPlayer bgmusic;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.actvity2);

    bgmusic=MediaPlayer.create(Activityyy2.this,R.raw.bgmusic);
    bgmusic.setLooping(true);
    bgmusic.start(); 


      b1=(Button)findViewById(R.id.button1);
         b1.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub


                Intent i=new Intent(Activityyy2.this,MainActivity.class);
                startActivity(i);

            }
        });

}
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    if (bgmusic != null) {
        if (bgmusic.isPlaying()) {

            bgmusic.stop();

        }
        bgmusic.release();
        bgmusic = null;
    }
}
  }
4

4 回答 4

0

use onBackPressed and onPause something like this

  @Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

    if (level != null) {
        if (level.isPlaying()) {

            level.stop();

        }
        level.release();
        level = null;
    }
}

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

    if (level != null) {
        if (level.isPlaying()) {

            level.pause();

        }
    }
}
于 2013-08-19T04:52:39.517 回答
0

You can handle back button explicitly by following code:

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK))
    {
      }
}

For homeButton, you can use this api:

public void  onWindowFocusChanged (boolean hasFocus){
}

hasFocus is the boolean value and based on focus value, you can write the logic

于 2013-08-19T04:53:33.020 回答
0
  1. 暂停或停止播放并将当前位置存储在 onPause()

  2. 重新打开流并寻找到 onResume() 中的位置

于 2013-08-19T04:48:41.287 回答
0

将值存储Bundle savedInstanceStateonPause()其中并从中获取值onResume()

于 2013-08-19T04:49:41.690 回答