你好,我是 android 新手,请帮助我,我的音频文件格式是正确的 .mp4 和 mp3,然后有时它也会给我这个错误 java.io.IOException: setDataSourceFD failed.: status=0x80000000 或有时 java.lang.NullPointerException .
public class Music implements OnCompletionListener {
public static boolean isPlaying=false;
public static MediaPlayer mp=null;
public static void playGeneric(int name, final ImageButton button,final ImageButton pervious,Context context) {
button.setEnabled(false);
button.setClickable(false);
pervious.setEnabled(false);
pervious.setClickable(false);
try{
if(isPlaying==true)
{
stopplaying();
}
if(isPlaying==false)
{}
mp = MediaPlayer.create(context, name);
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer arg0) {
mp.start();
isPlaying=true;
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
isPlaying=false;
System.out.println("Object released");
button.setEnabled(true);
button.setClickable(true);
pervious.setEnabled(true);
pervious.setClickable(true);
}
});
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
e.getMessage();
} catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
e.getMessage();
}
}
public static void stopplaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
看我的 LogCat
10-15 15:36:19.061: E/MediaPlayer(5018): Unable to to create media player
10-15 15:36:19.061: D/MediaPlayer(5018): create failed:
10-15 15:36:19.061: D/MediaPlayer(5018): java.io.IOException: setDataSourceFD failed.: status=0x80000000
10-15 15:36:19.061: D/MediaPlayer(5018): at android.media.MediaPlayer.setDataSource(Native Method)
10-15 15:36:19.061: D/MediaPlayer(5018): at android.media.MediaPlayer.create(MediaPlayer.java:666)
10-15 15:36:19.061: D/MediaPlayer(5018): at com.rogerscenter.LearnReadWriteSpell.Utility.Music.playGeneric(Music.java:102)
10-15 15:36:19.061: D/MediaPlayer(5018): at com.rogerscenter.LearnReadWriteSpell.LetterCategory.Letter_Lesson1_activity.onClick(Letter_Lesson1_activity.java:702)
10-15 15:36:19.061: D/MediaPlayer(5018): at android.view.View.performClick(View.java:2485)
10-15 15:36:19.061: D/MediaPlayer(5018): at android.view.View$PerformClick.run(View.java:9153)
10-15 15:36:19.061: D/MediaPlayer(5018): at android.os.Handler.handleCallback(Handler.java:587)
10-15 15:36:19.061: D/MediaPlayer(5018): at android.os.Handler.dispatchMessage(Handler.java:92)
10-15 15:36:19.061: D/MediaPlayer(5018): at android.os.Looper.loop(Looper.java:123)
10-15 15:36:19.061: D/MediaPlayer(5018): at android.app.ActivityThread.main(ActivityThread.java:3647)
10-15 15:36:19.061: D/MediaPlayer(5018): at java.lang.reflect.Method.invokeNative(Native Method)
10-15 15:36:19.061: D/MediaPlayer(5018): at java.lang.reflect.Method.invoke(Method.java:507)
10-15 15:36:19.061: D/MediaPlayer(5018): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-15 15:36:19.061: D/MediaPlayer(5018): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-15 15:36:19.061: D/MediaPlayer(5018): at dalvik.system.NativeStart.main(Native Method)
10-15 15:36:19.061: W/System.err(5018): java.lang.NullPointerException
10-15 15:36:19.061: W/System.err(5018): at com.rogerscenter.LearnReadWriteSpell.Utility.Music.playGeneric(Music.java:103)
10-15 15:36:19.061: W/System.err(5018): at com.rogerscenter.LearnReadWriteSpell.LetterCategory.Letter_Lesson1_activity.onClick(Letter_Lesson1_activity.java:702)
10-15 15:36:19.061: W/System.err(5018): at android.view.View.performClick(View.java:2485)
10-15 15:36:19.061: W/System.err(5018): at android.view.View$PerformClick.run(View.java:9153)
10-15 15:36:19.061: W/System.err(5018): at android.os.Handler.handleCallback(Handler.java:587)
10-15 15:36:19.061: W/System.err(5018): at android.os.Handler.dispatchMessage(Handler.java:92)
10-15 15:36:19.061: W/System.err(5018): at android.os.Looper.loop(Looper.java:123)
10-15 15:36:19.061: W/System.err(5018): at android.app.ActivityThread.main(ActivityThread.java:3647)
10-15 15:36:19.061: W/System.err(5018): at java.lang.reflect.Method.invokeNative(Native Method)
10-15 15:36:19.061: W/System.err(5018): at java.lang.reflect.Method.invoke(Method.java:507)
10-15 15:36:19.061: W/System.err(5018): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-15 15:36:19.061: W/System.err(5018): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-15 15:36:19.061: W/System.err(5018): at dalvik.system.NativeStart.main(Native Method)
我无法理解有时它的工作完美,有时不是。我在活动静态方式中使用了这种方法。