我和我的朋友正在创建一个Android应用程序。我们几乎做到了......应用程序将在第一次运行时同时播放两个声音,但在第二次运行时它会抛出一些错误并达到ANR状态。如何解决这个问题?检查下面的logcat和代码。
代码
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener,Runnable {
boolean mBound = false;
Button btn1,btn2;
Button btn3;
Button btn4;
MediaPlayer yourStereo;
MediaPlayer myStereo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.a_button);
btn2=(Button)findViewById(R.id.b_button);
btn3=(Button)findViewById(R.id.c_button);
btn4=(Button)findViewById(R.id.d_button);
btn1.setOnClickListener(this);
myStereo = MediaPlayer.create(this, R.raw.brb);
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn3.setVisibility(Button.GONE);
btn4.setVisibility(Button.VISIBLE);
// TODO Auto-generated method stub
try {
myStereo.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myStereo.setLooping(true);
myStereo.start();
myStereo.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
myStereo.setLooping(true);
myStereo.release();
try {
myStereo.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myStereo.start();
}
});
// pause music here
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myStereo.stop();
btn4.setVisibility(Button.GONE);
btn3.setVisibility(Button.VISIBLE);
}
});
}
});
}
@Override
public void onClick(View arg0) {
// play music here
btn1.setVisibility(Button.GONE);
btn2.setVisibility(Button.VISIBLE);
// TODO Auto-generated method stub
yourStereo = MediaPlayer.create(this, R.raw.pl);
yourStereo.setLooping(true);
try {
yourStereo.prepare();
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
yourStereo.start();
yourStereo.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
yourStereo.setLooping(true);
yourStereo.release();
try {
yourStereo.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
yourStereo.start();
}
});
// pause music here
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
yourStereo.stop();
btn2.setVisibility(Button.GONE);
btn1.setVisibility(Button.VISIBLE);
}
});
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}
logcat的
07-08 11:44:17.245: W/System.err(2659): java.lang.IllegalStateException
07-08 11:44:17.261: W/System.err(2659): at android.media.MediaPlayer.prepare(Native Method)
07-08 11:44:17.261: W/System.err(2659): at com.example.sleepsimulator.MainActivity$1.onClick(MainActivity.java:49)
07-08 11:44:17.269: W/System.err(2659): at android.view.View.performClick(View.java:2485)
07-08 11:44:17.269: W/System.err(2659): at android.view.View$PerformClick.run(View.java:9080)
07-08 11:44:17.269: W/System.err(2659): at android.os.Handler.handleCallback(Handler.java:587)
07-08 11:44:17.269: W/System.err(2659): at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 11:44:17.269: W/System.err(2659): at android.os.Looper.loop(Looper.java:130)
07-08 11:44:17.269: W/System.err(2659): at android.app.ActivityThread.main(ActivityThread.java:3687)
07-08 11:44:17.269: W/System.err(2659): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 11:44:17.269: W/System.err(2659): at java.lang.reflect.Method.invoke(Method.java:507)
07-08 11:44:17.269: W/System.err(2659): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-08 11:44:17.269: W/System.err(2659): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-08 11:44:17.269: W/System.err(2659): at dalvik.system.NativeStart.main(Native Method)
07-08 11:44:20.534: W/KeyCharacterMap(2659): Can't open keycharmap file
07-08 11:44:20.534: W/KeyCharacterMap(2659): Error loading keycharmap file
07-08 11:44:20.534: W/KeyCharacterMap(2659): Using default keymap
07-08 11:44:26.636: W/System.err(2659): java.io.IOException: Prepare failed.: status=0xFFFFFF8E
07-08 11:44:26.636: W/System.err(2659): at android.media.MediaPlayer.prepare(Native Method)
07-08 11:44:26.636: W/System.err(2659): at com.example.sleepsimulator.MainActivity$1.onClick(MainActivity.java:49)
07-08 11:44:26.636: W/System.err(2659): at android.view.View.performClick(View.java:2485)
07-08 11:44:26.636: W/System.err(2659): at android.view.View$PerformClick.run(View.java:9080)
07-08 11:44:26.636: W/System.err(2659): at android.os.Handler.handleCallback(Handler.java:587)
07-08 11:44:26.636: W/System.err(2659): at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 11:44:26.636: W/System.err(2659): at android.os.Looper.loop(Looper.java:130)
07-08 11:44:26.636: W/System.err(2659): at android.app.ActivityThread.main(ActivityThread.java:3687)
07-08 11:44:26.636: W/System.err(2659): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 11:44:26.636: W/System.err(2659): at java.lang.reflect.Method.invoke(Method.java:507)
07-08 11:44:26.636: W/System.err(2659): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-08 11:44:26.636: W/System.err(2659): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-08 11:44:26.636: W/System.err(2659): at dalvik.system.NativeStart.main(Native Method)
07-08 11:44:26.636: E/MediaPlayer(2659): start called in state 64
07-08 11:44:26.636: E/MediaPlayer(2659): error (-38, 0)
07-08 11:44:34.643: E/MediaPlayer(2659): stop called in state 0
07-08 11:44:34.643: E/MediaPlayer(2659): error (-38, 0)
07-08 11:44:34.643: E/MediaPlayer(2659): Error (-38,0)
07-08 11:44:34.651: W/System.err(2659): java.lang.IllegalStateException
07-08 11:44:34.675: W/System.err(2659): at android.media.MediaPlayer.prepare(Native Method)
07-08 11:44:34.675: W/System.err(2659): at com.example.sleepsimulator.MainActivity$1$1.onCompletion(MainActivity.java:67)
07-08 11:44:34.675: W/System.err(2659): at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:1475)
07-08 11:44:34.675: W/System.err(2659): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 11:44:34.675: W/System.err(2659): at android.os.Looper.loop(Looper.java:130)
07-08 11:44:34.675: W/System.err(2659): at android.app.ActivityThread.main(ActivityThread.java:3687)
07-08 11:44:34.675: W/System.err(2659): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 11:44:34.675: W/System.err(2659): at java.lang.reflect.Method.invoke(Method.java:507)
07-08 11:44:34.675: W/System.err(2659): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-08 11:44:34.675: W/System.err(2659): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-08 11:44:34.675: W/System.err(2659): at dalvik.system.NativeStart.main(Native Method)
07-08 11:44:34.675: W/dalvikvm(2659): threadid=1: thread exiting with uncaught exception (group=0x40018578)
07-08 11:44:34.683: E/AndroidRuntime(2659): FATAL EXCEPTION: main
07-08 11:44:34.683: E/AndroidRuntime(2659): java.lang.IllegalStateException
07-08 11:44:34.683: E/AndroidRuntime(2659): at android.media.MediaPlayer._start(Native Method)
07-08 11:44:34.683: E/AndroidRuntime(2659): at android.media.MediaPlayer.start(MediaPlayer.java:950)
07-08 11:44:34.683: E/AndroidRuntime(2659): at com.example.sleepsimulator.MainActivity$1$1.onCompletion(MainActivity.java:75)
07-08 11:44:34.683: E/AndroidRuntime(2659): at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:1475)
07-08 11:44:34.683: E/AndroidRuntime(2659): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 11:44:34.683: E/AndroidRuntime(2659): at android.os.Looper.loop(Looper.java:130)
07-08 11:44:34.683: E/AndroidRuntime(2659): at android.app.ActivityThread.main(ActivityThread.java:3687)
07-08 11:44:34.683: E/AndroidRuntime(2659): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 11:44:34.683: E/AndroidRuntime(2659): at java.lang.reflect.Method.invoke(Method.java:507)
07-08 11:44:34.683: E/AndroidRuntime(2659): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-08 11:44:34.683: E/AndroidRuntime(2659): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-08 11:44:34.683: E/AndroidRuntime(2659): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
您收到的错误是由于.release()
.prepare()
后跟onCompletionListener
.release()
会释放所有资源,只有在您不想再使用MediaPlayer
时才会被调用,例如退出应用时。
http://developer.android.com/reference/android/media/MediaPlayer.html#release()
因此,.prepare()
之后对.release()
的调用失败,因为它不知道要准备什么。
如果您只是再次播放同一个文件,则可以在完成时再次调用.start()
。我相信它会回到起点。无需再次发布和准备。
此外,如果您使用.create()
,则根本不需要致电.prepare()
,因为它已经处于准备状态。