退出申请后如何让媒体播放器播放

时间:2015-04-04 14:04:25

标签: android android-mediaplayer

每当我点击后退或点击回家时,声音就会停止..即使我点击主页或后退按钮,如何让声音仍在播放..对不起我的英文..谢谢

这是我的main_activity代码

public class Child extends Activity{

private static final String isPlaying = "Media is Playing";

private MediaPlayer player;
private Button jakartaButton;
private Button acehButton;
private Button jogjaButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.child);

 // Get the button from the view
        jakartaButton = (Button) this.findViewById(R.id.jakarta);
        jakartaButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                playSound(1);

            }
         });
        acehButton = (Button) this.findViewById(R.id.aceh);
         acehButton.setOnClickListener(new OnClickListener() {

             @Override
             public void onClick(View arg0) {
                 // TODO Auto-generated method stub
                 playSound(2);

             }
         });
         jogjaButton = (Button) this.findViewById(R.id.jogja);
         jogjaButton.setOnClickListener(new OnClickListener() {

             @Override
             public void onClick(View arg0) {
                 // TODO Auto-generated method stub
                 playSound(3);

             }
         });
 }

 @Override
 public void onPause() {
    try{
    super.onPause();
    player.pause();
    }catch (Exception e){

    }
 }

 private void playSound(int arg){
    try{
        if (player.isPlaying()) {
            player.stop();
            player.release();
        }
    }catch(Exception e){
        Toast.makeText(this, " Masuk Exception", Toast.LENGTH_LONG).show();
    }
    if (arg == 1){

        Toast.makeText(this, isPlaying+" jakarta", Toast.LENGTH_LONG).show();
        player = MediaPlayer.create(this, R.raw.jakarta);

    }else if (arg == 2){
         Toast.makeText(this, isPlaying+" aceh", Toast.LENGTH_LONG).show();
         player = MediaPlayer.create(this, R.raw.aceh);

     }else if (arg == 3){
         Toast.makeText(this, isPlaying+" jogja", Toast.LENGTH_LONG).show();
         player = MediaPlayer.create(this, R.raw.yogya);
     }
    player.setLooping(true); // Set looping
    player.start();  
}

每当我点击后退或点击回家时声音就会停止..即使我点击主页或后退按钮,如何让声音仍在播放..对不起我的英语..

1 个答案:

答案 0 :(得分:0)

有一个关于在不同Android平台上播放音乐的Google示例项目,如电话,手机,手表,自动...它可以保持音乐播放甚至应用程序退出。深入了解here,我想你会受到很多启发。