gpt4 book ai didi

java - 当您在没有点击播放的那一刻按暂停或停止时,Android 广播流媒体应用程序崩溃

转载 作者:行者123 更新时间:2023-11-30 04:21:05 25 4
gpt4 key购买 nike

当我启动我的应用程序并按下停止或暂停时,Android 应用程序将崩溃。如果您先按播放然后停止或暂停,效果会很好。我在 google 和 stackoverflow 上搜索过,但找不到太多相关信息。我认为问题是因为 NullPointerException,但由于我也是 java 新手,所以它并没有告诉我很多关于这个问题的信息

代码:

     import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class myMain extends Activity implements
MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener,
MediaPlayer.OnErrorListener, MediaPlayer.OnBufferingUpdateListener, OnClickListener {


private String TAG = getClass().getSimpleName();
private MediaPlayer mp= null;

private Button play;
private Button pause;
private Button stop;




@Override
public void onCreate(Bundle icicle) {

super.onCreate(icicle);
setContentView(R.layout.main);

play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
stop = (Button) findViewById(R.id.stop);




play.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
play();
}
});

pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
pause();
}
});

stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
stop();

}
});
}


private void play() {
Uri myUri = Uri.parse("url");
try {
if (mp == null) {
this.mp = new MediaPlayer();
} else {
mp.stop();
mp.reset();
}
mp.setDataSource(this, myUri); // Go to Initialized state
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnPreparedListener(this);
mp.setOnBufferingUpdateListener(this);

mp.setOnErrorListener(this);
mp.prepareAsync();

Log.d(TAG, "LoadClip Done");
} catch (Throwable t) {
Log.d(TAG, t.toString());
}
}

public void onPrepared(MediaPlayer mp) {
Log.d(TAG, "Stream is prepared");
mp.start();
}

private void pause() {
mp.pause();
}
private void stop() {
mp.stop();

}

@Override
public void onDestroy() {
super.onDestroy();
stop();

}

public void onCompletion(MediaPlayer mp) {
stop();
}

public boolean onError(MediaPlayer mp, int what, int extra) {
StringBuilder sb = new StringBuilder();
sb.append("Media Player Error: ");
switch (what) {
case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
sb.append("Not Valid for Progressive Playback");
break;
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
sb.append("Server Died");
break;
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
sb.append("Unknown");
break;
default:
sb.append(" Non standard (");
sb.append(what);
sb.append(")");
}
sb.append(" (" + what + ") ");
sb.append(extra);
Log.e(TAG, sb.toString());
return true;
}

public void onBufferingUpdate(MediaPlayer mp, int percent) {
Log.d(TAG, "PlayerService onBufferingUpdate : " + percent + "%");
}

public void onClick(View v) {
// TODO Auto-generated method stub

}

}

错误:

      02-11 20:45:43.837: D/AndroidRuntime(338): Shutting down VM
02-11 20:45:43.837: W/dalvikvm(338): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-11 20:45:43.857: E/AndroidRuntime(338): FATAL EXCEPTION: main
02-11 20:45:43.857: E/AndroidRuntime(338): java.lang.NullPointerException
02-11 20:45:43.857: E/AndroidRuntime(338): at wadio.media.internetradio.myMain.stop(myMain.java:95)
02-11 20:45:43.857: E/AndroidRuntime(338): at wadio.media.internetradio.myMain.access$2(myMain.java:94)
02-11 20:45:43.857: E/AndroidRuntime(338): at wadio.media.internetradio.myMain$3.onClick(myMain.java:55)
02-11 20:45:43.857: E/AndroidRuntime(338): at android.view.View.performClick(View.java:2485)
02-11 20:45:43.857: E/AndroidRuntime(338): at android.view.View$PerformClick.run(View.java:9080)
02-11 20:45:43.857: E/AndroidRuntime(338): at android.os.Handler.handleCallback(Handler.java:587)
02-11 20:45:43.857: E/AndroidRuntime(338): at android.os.Handler.dispatchMessage(Handler.java:92)
02-11 20:45:43.857: E/AndroidRuntime(338): at android.os.Looper.loop(Looper.java:123)
02-11 20:45:43.857: E/AndroidRuntime(338): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-11 20:45:43.857: E/AndroidRuntime(338): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 20:45:43.857: E/AndroidRuntime(338): at java.lang.reflect.Method.invoke(Method.java:507)
02-11 20:45:43.857: E/AndroidRuntime(338): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-11 20:45:43.857: E/AndroidRuntime(338): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-11 20:45:43.857: E/AndroidRuntime(338): at dalvik.system.NativeStart.main(Native Method)

最佳答案

在您的stop() 方法中,您访问了mp 变量。但是,mp 变量为空,直到您按下播放键(并且调用了 play() 方法)。因此,当您尝试访问 null 变量时,将抛出 NullPointerException。

停止 NullPointerException 的一种非常简单的方法是执行如下操作:

private void pause() {
if(mp!=null) mp.pause();
}

private void stop() {
if(mp!=null) mp.stop();
}

当然,此解决方案不考虑两次调用 pause 或 stop 的情况。看看 MediaPlayer documentation有关状态管理的更多信息。

关于java - 当您在没有点击播放的那一刻按暂停或停止时,Android 广播流媒体应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9244361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com