我是Android的新手,需要播放声音并在声音结束后显示图像。
下面提到的代码告诉你声音完成后显示下一个图像并播放与之相关的声音
代码:
mymediaplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
//mymediaplayer=null;
//mymediaplayer.reset();
mseekbar.setProgress(0);
count++;
Toast.makeText(getApplicationContext(), "SONG PLAYING OVER", 0).show();
changeImage(count);
playsound(count);
}
});
所以对于某些图片来说还可以,但是在某些图片之后,它会抛出异常,称为无法创建媒体播放器,然后强行关闭该应用。
下面是playound方法的代码。
private void playsound(int soundindex) {
// TODO Auto-generated method stub
if(mymediaplayer!=null)
mymediaplayer.release();
Toast.makeText(getApplicationContext(), "SOUND"+soundindex, 0).show();
if(souncollect.getlengthofsoundfile_array()>=count)
{
try
{
mymediaplayer=MediaPlayer.create(MainActivity.this, souncollect.capture_sound.get(soundindex));
mymediaplayer.seekTo(0);
mymediaplayer.start();
new Thread(this).start();
}
catch(Exception ex)
{
Toast.makeText(getApplicationContext(), "ERROR WHILE PLAYING SOUND", Toast.LENGTH_LONG).show();
}
}
else
{
Toast.makeText(getApplicationContext(), "NO SOUND TO PLAY", Toast.LENGTH_SHORT).show();
}
所有声音和图像的东西都在一些arraylist里面的其他类中定义。在arraylist中我有18个声音和18个图像。
下面提到异常的日志文件
02-27 13:06:00.908: D/dalvikvm(1503): GC_EXTERNAL_ALLOC freed 87K, 52% free 2615K/5379K, external 2112K/2137K, paused 53ms
02-27 13:06:10.928: D/dalvikvm(1503): GC_EXTERNAL_ALLOC freed 23K, 52% free 2621K/5379K, external 2289K/2613K, paused 42ms
02-27 13:06:15.488: D/dalvikvm(1503): GC_EXTERNAL_ALLOC freed 14K, 52% free 2631K/5379K, external 7212K/8760K, paused 264ms
02-27 13:06:21.039: D/dalvikvm(1503): GC_EXTERNAL_ALLOC freed 10K, 52% free 2634K/5379K, external 7015K/8761K, paused 42ms
****02-27 13:06:27.098: E/MediaPlayer(1503): Unable to to create media player
02-27 13:06:27.098: D/MediaPlayer(1503): create failed:
02-27 13:06:27.098: D/MediaPlayer(1503): java.io.IOException: setDataSourceFD failed.: status=0x80000000****
02-27 13:06:27.098: D/MediaPlayer(1503): at android.media.MediaPlayer.setDataSource(Native Method)
02-27 13:06:27.098: D/MediaPlayer(1503): at android.media.MediaPlayer.create(MediaPlayer.java:666)
02-27 13:06:27.098: D/MediaPlayer(1503): at com.goutam.imageandplaysound.MainActivity.playsound(MainActivity.java:85)
02-27 13:06:27.098: D/MediaPlayer(1503): at com.goutam.imageandplaysound.MainActivity.access$3(MainActivity.java:76)
02-27 13:06:27.098: D/MediaPlayer(1503): at com.goutam.imageandplaysound.MainActivity$1.onCompletion(MainActivity.java:117)
02-27 13:06:27.098: D/MediaPlayer(1503): at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:1316)
02-27 13:06:27.098: D/MediaPlayer(1503): at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 13:06:27.098: D/MediaPlayer(1503): at android.os.Looper.loop(Looper.java:123)
02-27 13:06:27.098: D/MediaPlayer(1503): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-27 13:06:27.098: D/MediaPlayer(1503): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 13:06:27.098: D/MediaPlayer(1503): at java.lang.reflect.Method.invoke(Method.java:507)
02-27 13:06:27.098: D/MediaPlayer(1503): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-27 13:06:27.098: D/MediaPlayer(1503): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-27 13:06:27.098: D/MediaPlayer(1503): at dalvik.system.NativeStart.main(Native Method)
02-27 13:06:27.218: D/AndroidRuntime(1503): Shutting down VM
02-27 13:06:27.218: W/dalvikvm(1503): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-27 13:06:27.228: E/AndroidRuntime(1503): FATAL EXCEPTION: main
02-27 13:06:27.228: E/AndroidRuntime(1503): java.lang.NullPointerException
02-27 13:06:27.228: E/AndroidRuntime(1503): at com.goutam.imageandplaysound.MainActivity.playsound(MainActivity.java:106)
02-27 13:06:27.228: E/AndroidRuntime(1503): at com.goutam.imageandplaysound.MainActivity.access$3(MainActivity.java:76)
02-27 13:06:27.228: E/AndroidRuntime(1503): at com.goutam.imageandplaysound.MainActivity$1.onCompletion(MainActivity.java:117)
02-27 13:06:27.228: E/AndroidRuntime(1503): at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:1316)
02-27 13:06:27.228: E/AndroidRuntime(1503): at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 13:06:27.228: E/AndroidRuntime(1503): at android.os.Looper.loop(Looper.java:123)
02-27 13:06:27.228: E/AndroidRuntime(1503): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-27 13:06:27.228: E/AndroidRuntime(1503): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 13:06:27.228: E/AndroidRuntime(1503): at java.lang.reflect.Method.invoke(Method.java:507)
02-27 13:06:27.228: E/AndroidRuntime(1503): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-27 13:06:27.228: E/AndroidRuntime(1503): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-27 13:06:27.228: E/AndroidRuntime(1503): at dalvik.system.NativeStart.main(Native Method)
02-27 13:11:27.292: I/Process(1503): Sending signal. PID: 1503 SIG: 9
正如你可以看到一些图像和声音它可以正常但是经过一段时间后它无法创建mediaplayer.so困惑并且不知道如何实现媒体播放器逐个播放声音。在图像编号7之后失败
还提到了代码中的onpause状态
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if(!mymediaplayer.isPlaying())
{
mymediaplayer=null;
mymediaplayer.release();
}
}
所以请建议什么是更好的方法。感谢任何回复