简体   繁体   中英

MediaPlayer, onItemClickListener and a NullPointerException. What's wrong?

I'm working on an Android soundboard at the moment and so far it's going fine. I've made a beautiful listView with images etc., but i just can't get the sounds to load properly. The locations of the soundfiles are stored within the arraylist and I'm trying to load them with getById().sound;

Here's what I've done

    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();

            }


        }
    });

Can anyone tell my why it doesn't work? I'm still a beginner and I couldn't figure it out after hours of trying and researching.

EDIT: Here's the error in the logcat. I'm pretty sure the file is there...

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)

EDIT: Just want to answer my own question here (StackOverflow won't let me do so for three more hours). Actually the code was fine, the one thing i did wrong was that i declared my soundfiles as string variables instead of int resource ids in my ArrayList/Itemadapter. Thanks to all the people who tried to help me :)

不要忘记添加mediaplayer.prepare();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM