我写这个是因为当我从sdCard中选择音频时遇到问题,我尝试用MediaPlayer播放它。
以下是我的代码的一部分:
Intent intent = new Intent();
intent.setType("audio/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Selecciona la música de Fondo "), PETICION_ESCOGER_AUDIO);
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
musicaDeFondo.reset();
if (requestCode == PETICION_ESCOGER_AUDIO)
{
Uri musicaFondoSeleccion = data.getData();
if (musicaFondoSeleccion != null)
{
System.out.println(musicaFondoSeleccion.toString());
// Here fails
musicaDeFondo = (AndroidMusica) sonidoManager.nuevaMusicaSDCARD(Piano.this, musicaFondoSeleccion); // Here fails
}
}
}
我的AndroidMusica类构造函数:
public AndroidMusica (Context ctx, Uri path){
try {
mediaPlayer.setDataSource(ctx, path); // Here files. line 37
mediaPlayer.prepareAsync();
isPrepared = true;
mediaPlayer.setOnCompletionListener(this);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
日志:
02-19 02:31:24.792: E/AndroidRuntime(19447): FATAL EXCEPTION: main
02-19 02:31:24.792: E/AndroidRuntime(19447): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=42, result=-1, data=Intent { dat=content://media/external/audio/media/434 }} to activity {com.psdmple.piano/com.psdmple.piano.Piano}: java.lang.NullPointerException
02-19 02:31:24.792: E/AndroidRuntime(19447): at android.app.ActivityThread.deliverResults(ActivityThread.java:2997)
02-19 02:31:24.792: E/AndroidRuntime(19447): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3040)
02-19 02:31:24.792: E/AndroidRuntime(19447): at android.app.ActivityThread.access$1100(ActivityThread.java:128)
02-19 02:31:24.792: E/AndroidRuntime(19447): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1191)
02-19 02:31:24.792: E/AndroidRuntime(19447): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 02:31:24.792: E/AndroidRuntime(19447): at android.os.Looper.loop(Looper.java:137)
02-19 02:31:24.792: E/AndroidRuntime(19447): at android.app.ActivityThread.main(ActivityThread.java:4517)
02-19 02:31:24.792: E/AndroidRuntime(19447): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 02:31:24.792: E/AndroidRuntime(19447): at java.lang.reflect.Method.invoke(Method.java:511)
02-19 02:31:24.792: E/AndroidRuntime(19447): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
02-19 02:31:24.792: E/AndroidRuntime(19447): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
02-19 02:31:24.792: E/AndroidRuntime(19447): at dalvik.system.NativeStart.main(Native Method)
02-19 02:31:24.792: E/AndroidRuntime(19447): Caused by: java.lang.NullPointerException
02-19 02:31:24.792: E/AndroidRuntime(19447): at logica.AndroidMusica.<init>(AndroidMusica.java:37)
02-19 02:31:24.792: E/AndroidRuntime(19447): at logica.SonidoManager.nuevaMusicaSDCARD(SonidoManager.java:60)
02-19 02:31:24.792: E/AndroidRuntime(19447): at com.psdmple.piano.Piano.onActivityResult(Piano.java:101)
02-19 02:31:24.792: E/AndroidRuntime(19447): at android.app.Activity.dispatchActivityResult(Activity.java:4654)
02-19 02:31:24.792: E/AndroidRuntime(19447): at android.app.ActivityThread.deliverResults(ActivityThread.java:2993)
02-19 02:31:24.792: E/AndroidRuntime(19447): ... 11 more
我真的很感谢你的帮助=)
答案 0 :(得分:0)
对象mediaPlayer
永远不会使用构造函数启动,因此为null。