我有这个活动,只要我在播放声音时按下主页按钮 声音一直在播放,当声音结束时它会带回应用程序。
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上关闭它。
答案 0 :(得分:1)
以下是MediaPlayer
的状态图,它可以帮助您了解Activity
生命周期中的哪一点,您应该调用MediaPlayer
对象上的各种方法。
就像Jesse J
所说的那样,尝试在stop()
onPause()