mercredi 25 février 2015

Android JAVA Background Music Checkbox

I Been trying to add Background music to my APP.. i have add music player and the music actually works.. but i do have 2 problems.. 1 problem is i have no idea how to make the BG Music check box stop and start the music when checked & unchecked.. the second problem i have is.. when i move between pages in the app the music works fine but when press back to menu button and go back to the MainActivity the music just starts another time and then i have 2 Songs running in the back at the same time...


My checkbox id is checkBox1 as for now...


my code :



MediaPlayer mp;

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mp = MediaPlayer.create(MainActivity.this, R.raw.bgm);
mp.setLooping(true);

mp.start();

@Override

protected void onPause() {
if (this.isFinishing()){

mp.stop();
}

Context context = getApplicationContext();
ActivityManager am = (ActivityManager)


context.getSystemService(Context.ACTIVITY_SERVICE); List taskInfo = am.getRunningTasks(1);



if(!taskInfo.isEmpty()) {
ComponentName topActivity = taskInfo.get(0).topActivity;

if (!topActivity.getPackageName().equals(context.getPackageName())){
mp.stop();

}
else {

}
}

super.onPause();
}

}




Aucun commentaire:

Enregistrer un commentaire