MediaPlayer在onCreate方法中播放了两次声音

时间:2016-06-27 19:48:21

标签: android android-mediaplayer

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声音播放两次。如何解决?

1 个答案:

答案 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();
}