我和我的朋友正在創建一個android應用程序。我們幾乎做到了這一點...應用程序將在第一次運行時同時播放兩個聲音,但在第二次運行時它會拋出一些錯誤並達到ANR狀態。如何解決這個問題?檢查下面的logcat和代碼。如何使用MediaPlayer同時播放兩個或多個音頻剪輯(android)
代碼
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)
感謝您的支持:) – Niko