您好我正在建立在线广播流媒体应用。我按下播放按钮时收到java.lang.IllegalStateException错误。主要活动如下所示。
package com.radioawaz.simerpreetjassal.radioawaz;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import java.io.IOException;
public class MainActivity extends Activity implements OnClickListener {
private ProgressBar playSeekBar;
private Button buttonPlay;
private Button buttonStopPlay;
private MediaPlayer player;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeUIElements();
initializeMediaPlayer();
}
private void initializeUIElements() {
playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
playSeekBar.setMax(100);
playSeekBar.setVisibility(View.INVISIBLE);
buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
buttonStopPlay.setEnabled(false);
buttonStopPlay.setOnClickListener(this);
}
public void onClick(View v) {
if (v == buttonPlay) {
startPlaying();
} else if (v == buttonStopPlay) {
stopPlaying();
}
}
private void startPlaying() {
buttonStopPlay.setEnabled(true);
buttonPlay.setEnabled(false);
playSeekBar.setVisibility(View.VISIBLE);
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
}
private void stopPlaying() {
if (player.isPlaying()) {
player.stop();
player.release();
initializeMediaPlayer();
}
buttonPlay.setEnabled(true);
buttonStopPlay.setEnabled(false);
playSeekBar.setVisibility(View.INVISIBLE);
}
private void initializeMediaPlayer() {
player = new MediaPlayer();
try {
player.setDataSource("http://streaming.shoutcast.com/Radioawazca?lang=en-ca");
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
playSeekBar.setSecondaryProgress(percent);
Log.i("Buffering", "" + percent);
}
});
}
@Override
protected void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
}
}
}
Erorr Log cat
09-29 20:51:51.278 7852-7852/com.radioawaz.simerpreetjassal.radioawaz E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.radioawaz.simerpreetjassal.radioawaz, PID: 7852
java.lang.IllegalStateException
at android.media.MediaPlayer.prepareAsync(Native Method)
at com.radioawaz.simerpreetjassal.radioawaz.MainActivity.startPlaying(MainActivity.java:67)
at com.radioawaz.simerpreetjassal.radioawaz.MainActivity.onClick(MainActivity.java:55)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-29 20:51:52.978 7852-7852/com.radioawaz.simerpreetjassal.radioawaz I/Process: Sending signal. PID: 7852 SIG: 9
您好我正在建立在线广播流媒体应用。我按下播放按钮时收到java.lang.IllegalStateException错误。主要活动如下所示。
答案 0 :(得分:0)
请添加
execvp
在AndroidManifest.xml文件中。
答案 1 :(得分:0)
查看文档有两个可能的问题:
error
状态,这会使它抛出IllegalStateException
。在对MediaPlayer对象执行任何操作之前,通过https://developer.android.com/reference/android/media/MediaPlayer.html#setOnErrorListener(android.media.MediaPlayer.OnErrorListener)添加ErrorListener。文档中的重要部分:
通常,一些回放控制操作可能由于各种原因而失败,例如不支持的音频/视频格式,交错的音频/视频,分辨率太高,流超时等。因此,在这些情况下,错误报告和恢复是一个重要的问题。有时,由于编程错误,也可能发生在无效状态下调用重放控制操作。在所有这些错误条件下,如果事先通过setOnErrorListener(android.media.MediaPlayer.OnErrorListener)注册了OnErrorListener,则内部播放器引擎会调用用户提供的OnErrorListener.onError()方法。
答案 2 :(得分:0)
删除你的player.prepareAsync(),然后在player.prepareAsync()中添加player.prepare(),如下所示:希望可以帮到你
private void initializeMediaPlayer() {
player = new MediaPlayer();
try {
player.setDataSource("http://streaming.shoutcast.com/Radioawazca?lang=en-ca");
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
playSeekBar.setSecondaryProgress(percent);
Log.i("Buffering", "" + percent);
}
});
}