如果我可以这样称呼自己,我就是新手,而且我的程序中有一个错误,我无法弄清楚如何修复它。我有一个使用流媒体源在线播放广播的小应用程序。
日志告诉我:
> 12-09 21:32:35.207: W/WindowManager(60): Pointer down received while already down in: Window{450930b8 com.android.launcher/com.android.launcher2.Launcher paused=false}
> 12-09 21:32:35.347: I/ActivityManager(60): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 .MainRadioActivity bnds=[174,147][230,206] }
> 21:32:35.777: I/Prefetcher(34): max cache size reached 12-09
> 21:32:36.457: I/StagefrightPlayer(34):
> setDataSource('http://64.71.133.122:8000') 12-09 21:32:36.717:
> I/ActivityManager(60): Displayed activity MainRadioActivity: 1580 ms
> (total 1580 ms) 12-09 21:32:36.821: I/Prefetcher(34): max cache size
> reached 12-09 21:32:37.862: I/Prefetcher(34): max cache size reached
> 12-09 21:32:38.902: I/Prefetcher(34): max cache size reached 12-09
> 21:32:39.896: I/Prefetcher(34): max cache size reached
> 12-09 21:32:40.736: E/MediaPlayer(1122): prepareAsync called in state 4
> 12-09 21:32:40.736: D/AndroidRuntime(1122): Shutting down VM
> 12-09 21:32:40.736: W/dalvikvm(1122): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
> 12-09 21:32:40.756: E/AndroidRuntime(1122): FATAL EXCEPTION: main
> 12-09 21:32:40.756: E/AndroidRuntime(1122): java.lang.IllegalStateException
> 12-09 21:32:40.756: E/AndroidRuntime(1122): at android.media.MediaPlayer.prepareAsync(Native Method)
> 12-09 21:32:40.756: E/AndroidRuntime(1122): at com.example.***.MainRadioActivity.startPlaying(MainRadioActivity.java:65)
> 12-09 21:32:40.756: E/AndroidRuntime(1122): at com.example.***.MainRadioActivity.onClick(MainRadioActivity.java:55)
> 12-09 21:32:40.756: E/AndroidRuntime(1122): at android.view.View.performClick(View.java:2408)
> 12-09 21:32:40.756: E/AndroidRuntime(1122): at android.view.View$PerformClick.run(View.java:8816)
> 12-09 21:32:40.756: E/AndroidRuntime(1122): at android.os.Handler.handleCallback(Handler.java:587)
> 12-09 21:32:40.756: E/AndroidRuntime(1122): at android.os.Handler.dispatchMessage(Handler.java:92)
> 12-09 21:32:40.756: E/AndroidRuntime(1122): at android.os.Looper.loop(Looper.java:123)
> 12-09 21:32:40.756: E/AndroidRuntime(1122): at android.app.ActivityThread.main(ActivityThread.java:4627)
> 12-09 21:32:40.756: E/AndroidRuntime(1122): at java.lang.reflect.Method.invokeNative(Native Method)
> 12-09 21:32:40.756: E/AndroidRuntime(1122): at java.lang.reflect.Method.invoke(Method.java:521)
> 12-09 21:32:40.756: E/AndroidRuntime(1122): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
> 12-09 21:32:40.756: E/AndroidRuntime(1122): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
> 12-09 21:32:40.756: E/AndroidRuntime(1122): at dalvik.system.NativeStart.main(Native Method)
> 12-09 21:32:40.776: W/ActivityManager(60): Force finishing activity .......
这是我的代码:
public class MainRadioActivity extends Activity implements OnClickListener {
private static String RADIO_STATION_URL = "http://64.71.133.122:8000";
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_radio);
initializeUIElements();
initializeMediaPlayer();
}
private void initializeUIElements() {
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);
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
player.prepareAsync();
}
private void stopPlaying() {
if (player.isPlaying()) {
player.stop();
player.release();
initializeMediaPlayer();
}
buttonPlay.setEnabled(true);
buttonStopPlay.setEnabled(false);
}
private void initializeMediaPlayer() {
player = new MediaPlayer();
try {
player.setDataSource(RADIO_STATION_URL);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
}
}
}
任何帮助是极大的赞赏 :) !