我目前正在开发一个 Android 音板,到目前为止一切正常。我用图像等制作了一个漂亮的 listView,但我就是无法正确加载声音。声音文件的位置存储在数组列表中,我正在尝试使用 getById().sound 加载它们;
这是我所做的
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
mp = MediaPlayer.create(MainActivity.this, Uri.parse("android.resource://" + getPackageName() + "/" +
Model.GetbyId(position + 1).Sound));
if(mp == null) {
Toast.makeText(getApplicationContext(),
"Mediaplayer is null. Link was: "+link , Toast.LENGTH_LONG)
.show();
} else {
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaplayer) {
mediaplayer.stop();
mediaplayer.release();
}
});
mp.start();
}
}
});
谁能告诉我为什么它不起作用?我仍然是初学者,经过数小时的尝试和研究,我无法弄清楚。
编辑:这是 logcat 中的错误。我很确定文件在那里...
07-28 12:56:53.998 124-593/? I/AwesomePlayer: setDataSource_l(URL suppressed)
07-28 12:56:53.998 124-7235/? E/: Failed to open file 'R.raw.so_schoenasummervielheu'. (No such file or directory)
07-28 12:56:53.998 6760-6771/? E/MediaPlayer: error (1, -2147483648)
07-28 12:56:54.008 6760-6760/? D/MediaPlayer: create failed:
java.io.IOException: Prepare failed.: status=0x1
at android.media.MediaPlayer.prepare(Native Method)
at android.media.MediaPlayer.create(MediaPlayer.java:792)
at android.media.MediaPlayer.create(MediaPlayer.java:769)
at com.spruechmeischtr2000.android.MainActivity$1.onItemClick(MainActivity.java:45)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
at android.widget.AbsListView$1.run(AbsListView.java:3463)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
编辑:只想在这里回答我自己的问题(StackOverflow 不会让我再等三个小时)。实际上代码很好,我做错的一件事是我将我的声音文件声明为字符串变量,而不是我的 ArrayList/Itemadapter 中的 int 资源 ID。感谢所有试图帮助我的人:)