MediaPlayer在onCreate方法中播放了两次声音
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(mediaPlayer.isPlaying())
{
mediaPlayer.stop();
}
mediaPlayer.reset();
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.v1);
mediaPlayer.start();
}
当onCreate方法stard声音播放两次。如何解决?
答案 0 :(得分:0)
启动声音onCreate的正确方法:
MediaPlayer mPlayer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.v1);
mPlayer.start();
}
然后在销毁时停止它:
@Override
public void onDestroy() {
mPlayer.stop();
super.onDestroy();
}