android media player中的非法状态异常
我正在开发一个简单的实时音频流应用程序。应用程序运行,但在mediaplayer.preapre()之后出现非法状态异常。谁能帮我一下我做错了什么android media player中的非法状态异常,android,android-mediaplayer,Android,Android Mediaplayer,我正在开发一个简单的实时音频流应用程序。应用程序运行,但在mediaplayer.preapre()之后出现非法状态异常。谁能帮我一下我做错了什么 public class MainActivity extends Activity { MediaPlayer mPlayer; Button buttonPlay; Button buttonStop; String url = "http://programmerguru.com/android-tutorial/wp-content/upl
public class MainActivity extends Activity {
MediaPlayer mPlayer;
Button buttonPlay;
Button buttonStop;
String url = "http://programmerguru.com/android-tutorial/wp-content/uploads/2013/04/hosannatelugu.mp3";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonPlay = (Button) findViewById(R.id.play);
buttonPlay.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mPlayer.setDataSource(url);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly! IllegalArgumentException", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!SecurityException", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!IllegalStateException", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
mPlayer.start();
}
});
buttonStop = (Button) findViewById(R.id.stop);
buttonStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(mPlayer!=null && mPlayer.isPlaying()){
mPlayer.stop();
}
}
});
}
public void onPrepared(MediaPlayer player) {
player.start();
}
以下消息出现在logcat中:
E/MediaPlayer-JNI: QCMediaPlayer mediaplayer NOT present
E/MediaPlayer: prepareAsync called in state 1 E/MediaPlayer: Unable to create media player
E/MediaPlayer: prepareAsync called in state 1
E/MediaPlayer: start called in state 1
E/MediaPlayer: error (-38, 0)
可能的重复可能的重复