你好全部
我是Android新手
通过单击按钮停止声音后,我无法重新开始播放声音
我研究过这个问题
( Link )并找到了不同的申请方法
所以,我尝试了四种方法(不成功)
有四个按钮使用不同的方法
按钮1将用作控件
查看不同方法结果的评论
我将在下面的LogCat中发布一些条目。
谢谢你
Here is a link to a relevant Android State Diagram
package com.example.playsound;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
//Create Media Player
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Button Sound
final MediaPlayer mPlayer1 = MediaPlayer.create(MainActivity.this, R.raw.playsound0);
final MediaPlayer mPlayer2 = MediaPlayer.create(MainActivity.this, R.raw.playsound1);
final MediaPlayer mPlayer3 = MediaPlayer.create(MainActivity.this, R.raw.playsound2);
final MediaPlayer mPlayer4 = MediaPlayer.create(MainActivity.this, R.raw.playsound3);
//Button References
Button button1 = (Button)this.findViewById(R.id.button1);
Button button2 = (Button)this.findViewById(R.id.button2);
Button button3 = (Button)this.findViewById(R.id.button3);
Button button4 = (Button)this.findViewById(R.id.button4);
//Button 1 OnClickListener
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mPlayer1.isPlaying() ) {
try {
mPlayer1.stop();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
// BUTTON 1
else { // plays sound upon first click, stops on second click
try { // but does not play on third click
mPlayer1.start();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
}
});
//Button 2 Uses .prepareAsync()
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mPlayer2.isPlaying() ) { // BUTTON 2
try { // Plays sound upon first click, stops on second click
mPlayer2.stop(); // But will not play upon third click
mPlayer2.reset();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
else {
try {
mPlayer2.prepareAsync();
} catch (IllegalStateException e) {
e.printStackTrace();
}
mPlayer2.start();
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mPlayer3.isPlaying() ) {
try { // BUTTON 3
mPlayer3.stop(); // Does not play sound upon first button click
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
else {
mPlayer3.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mPlayer3) {
mPlayer3.start();
}
});
}
}
});
//Button 4 OnClickListener
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mPlayer4.isPlaying() ) {
try {
mPlayer4.stop();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
// BUTTON 4
else { // does not play sound on first click
try {
mPlayer4.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mPlayer4.start();
}
}
});
}
}
LogCat中的大多数错误类似于:
在状态8中调用的prepareAsync
开始在状态0中调用
开始在状态1中调用
错误主要是“呼入状态#”错误 我参考了上面链接中发布的图表 当我尝试在正确的位置调用方法时,我仍然有这些错误
答案 0 :(得分:0)
您正在调用处于不适当状态的方法。
PrepareAsync只能在{Initialized,Stopped}状态下调用。
只能在{Prepared,Started,Paused,PlaybackCompleted}状态下调用Start。
因此,如果您尝试调用其他状态的那些状态,那么它将抛出异常。
要清楚了解状态,请阅读此doc