简体   繁体   中英

Android Mediaplayer Null point Exception (Java)

I am trying to create Mediaplayer session with given uri. but it causes NullpointerException .

    Uri uri = Uri.parse(path);

    // Creating MediaPlayer with given song's URI
    if (mediaPlayer != null) {
        mediaPlayer.stop();
        mediaPlayer.release();
    }
    mediaPlayer = MediaPlayer.create(this, uri);

    try {
        // Setting the MediaPlayer Listener
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                seekBar.setMax(mp.getDuration());
                mediaPlayer.start();
                changeSeekbar();
            }
        });
    } catch (Exception e) {
        Log.e("ERROR", e.toString());
    }

Given Logcat:

2020-04-07 22:21:05.289 12237-12237/com.example.musicappresearch2 E/Path: /storage/emulated/0/Music/Alone - Viren.mp3
2020-04-07 22:21:05.289 12237-12237/com.example.musicappresearch2 E/ERROR: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setOnPreparedListener(android.media.MediaPlayer$OnPreparedListener)' on a null object reference

Could you Tell me What i am doing wrong? Thanks.

There are two ways to write this code, both tested on the device

First of all, make sure you handle android.permission.READ_EXTERNAL_STORAGE correctly and you really have correct Uri.
MediaPlayer.create(this, uri); will fail if either context or uri are invalid.

MediaPlayer.create(this, uri); which itself already prepares player so you don't need .prepareAsync() in this situation. and your code is good to go.

another way:


    mediaPlayer = new MediaPlayer(); // hence, we don't use .create, manually instantiate
    try {
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setDataSource(this, uri);
        mediaPlayer.setOnPreparedListener(mp -> {
            mediaPlayer.start();
        });

        /* use async, if you don't want to block UI thread
         keep in mind, this should be called after setting listener  
       because it might prepare even until the listener has been set */
        mediaPlayer.prepareAsync(); 
    } catch (Exception e) {
        Log.e("ERROR", e.toString());
    }

Try this:

            Uri uri = Uri.parse(path);

        mediaPlayer = new MediaPlayer();
        try {
           // mediaPlayer.setDataSource(String.valueOf(uri));
           mediaPlayer.setDataSource(MainActivity.this,uri);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mediaPlayer.start();

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