こんにちは、私は 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)
完璧な仕事を理解できないときもあれば、そうでないときもあります。iこのメソッドをアクティビティの静的な方法で使用しました。