私はこのアクティビティを持っており、サウンドの再生中にホームボタンを押すとすぐにサウンドが再生され続け、サウンドが終了するとアプリが元に戻ります。
public class View5 extends MasterView implements OnCompletionListener {
int[] tracks = new int[4];
int[] times = new int[4];
int currentTrack = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
InputStream stream = null;
try {
stream = getAssets().open("herzdruck_animiert2.gif");
} catch (IOException e) {
e.printStackTrace();
}
SharedPreferences settings = getSharedPreferences("settings", 0);
String lang = settings.getString("lang", "");
if (lang.equals("de")) {
tracks[0] = getResources().getIdentifier("raw/view5_1_de",
null, this.getPackageName());
tracks[1] = getResources().getIdentifier("raw/view5_2_de", null,
this.getPackageName());
tracks[2] = getResources().getIdentifier("raw/view5_3_de", null,
this.getPackageName());
tracks[3] = getResources().getIdentifier("raw/view5_4_de", null,
this.getPackageName());
} else if (lang.equals("tr")) {
tracks[0] = getResources().getIdentifier("raw/view5_1_tr",
null, this.getPackageName());
tracks[1] = getResources().getIdentifier("raw/view5_2_tr", null,
this.getPackageName());
tracks[2] = getResources().getIdentifier("raw/view5_3_tr", null,
this.getPackageName());
tracks[3] = getResources().getIdentifier("raw/view5_4_tr", null,
this.getPackageName());
} else if (lang.equals("en")) {
tracks[0] = getResources().getIdentifier("raw/view5_1_en",
null, this.getPackageName());
tracks[1] = getResources().getIdentifier("raw/view5_2_en", null,
this.getPackageName());
tracks[2] = getResources().getIdentifier("raw/view5_3_en", null,
this.getPackageName());
tracks[3] = getResources().getIdentifier("raw/view5_4_en", null,
this.getPackageName());
}
times[0] = 5000;
times[1] = 2000;
times[2] = 2000;
times[3] = 1000;
GifMovieView view = new GifMovieView(this, stream);
mPlayer = MediaPlayer.create(getApplicationContext(), tracks[currentTrack]);
mPlayer.setOnCompletionListener(this);
mPlayer.start();
setContentView(view);
}
@Override
public void onCompletion(MediaPlayer arg0) {
arg0.release();
arg0 = MediaPlayer.create(getApplicationContext(),
tracks[currentTrack]);
if (currentTrack < tracks.length-1) {
currentTrack++;
arg0 = MediaPlayer.create(getApplicationContext(),
tracks[currentTrack]);
arg0.setOnCompletionListener(this);
try {
Thread.sleep(times[currentTrack]);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
arg0.start();
}else if(currentTrack==tracks.length-1) {
Intent i = new Intent(getApplicationContext(), ViewBeat1.class);
startActivity(i);
}
}
}
どうすればこの動作を取り除くことができますか?
MasterView は mPlayer を保持し、onRestart、onDestroy、および onPause も閉じます。