Android 播放相同的音乐时重新启动音乐

Android 播放相同的音乐时重新启动音乐,android,android-mediaplayer,Android,Android Mediaplayer,我有一个按钮和按钮按下声音“你好”播放 这是工作正常,但当我再次按下按钮,当前的声音应该停止,并再次播放从开始,目前它不会播放任何声音在第二次点击相同的按钮 我试过这种方法,但不起作用 MediaPlayer hello = MediaPlayer.create(this, R.raw.hello); if (v.getId() == R.id.buttonKey) { //i.e on buttonpress hello.stop(); hello.start()

我有一个按钮和按钮按下声音“你好”播放

这是工作正常,但当我再次按下按钮,当前的声音应该停止,并再次播放从开始,目前它不会播放任何声音在第二次点击相同的按钮

我试过这种方法,但不起作用

 MediaPlayer hello = MediaPlayer.create(this, R.raw.hello);
if (v.getId() == R.id.buttonKey) {    //i.e on buttonpress
hello.stop();           
hello.start();
}
寻找类似的问题,但运气不好。我怎样才能做到呢? 提前感谢

请尝试下面的代码

  MediaPlayer hello = MediaPlayer.create(this, R.raw.hello);

 if (v.getId() == R.id.buttonKey) {    //i.e on buttonpress

       hello.reset();
       hello = MediaPlayer.create(this, R.raw.hello);
       hello.prepare();
       hello.start();

 }
试试下面的代码

  MediaPlayer hello = MediaPlayer.create(this, R.raw.hello);

 if (v.getId() == R.id.buttonKey) {    //i.e on buttonpress

       hello.reset();
       hello = MediaPlayer.create(this, R.raw.hello);
       hello.prepare();
       hello.start();

 }

使用stop()和start()选项时会发生什么情况?我不确定,但我认为您也必须使用prepare()方法。看看这个流程:你是怎么做到的?您的按钮有两个动作,但没有明显的分隔。请发布按钮onClick方法的完整代码。通常,您必须检查mediaPlayer是否正在播放,如果是,请停止播放,如果不是,请启动播放…..如果(v.getId()==R.id.buttonKey){if(hello.isplay()){hello.stop();}hello.start();}否,则声音也不会播放,请发布完整的代码…通过上面的示例,我们看不到足够的上下文…在您的问题中发布它…当您使用stop()和start()选项时会发生什么?我不确定,但我认为您也必须使用prepare()方法。看看这个流程:你是怎么做到的?您的按钮有两个动作,但没有明显的分隔。请发布按钮onClick方法的完整代码。通常,您必须检查mediaPlayer是否正在播放,如果是,请停止播放,如果不是,请启动播放…..如果(v.getId()==R.id.buttonKey){if(hello.isplay()){hello.stop();}hello.start();}否,则声音也不会播放,请发布完整的代码…通过上面的示例,我们看不到足够的上下文…在您的问题中发布它…错误出现在hello=MediaPlayer.create(this,R.raw.hello)无法解析方法create'(com.myurl.appname.MainActivity.ButtonclickHandler,int)’现在可以工作了,谢谢MediaPlayer hello=MediaPlayer.create(this,R.raw.hello); if(v.getId()==R.id.buttonKey){//i.on按钮按if(hello.isplaying){hello.reset();hello=MediaPlayer.create(getApplicationContext(),R.raw.hello);}hello.start();}hello=MediaPlayer.create(this,R.raw.hello)错误无法解析方法create'(com.myurl.appname.MainActivity.buttonickhandler,int)'现在这可以工作了,谢谢MediaPlayer hello=MediaPlayer.create(this,R.raw.hello);if(v.getId()==R.id.buttonKey){//即在按钮上按if(hello.isplaying){hello.reset();hello=MediaPlayer.create(getApplicationContext(),R.raw.hello);}hello.start();}