-1

I've been working on creating an alarm app to run a music track that the user selects. So, after the user selects the track, it is converted from Uri to String by ".toString" method to be able to receive this Uri from a broadcast receiver so that it can be used in MediaPlayer.create(); after converting it back to Uri

Uri.parse(String track);

The problem comes when i try to play this it doesn't work although when I tested this in another activity it worked fine and I checked the uri.getPath() it tells me the path of the track so the problem is not because Uri object is empty.

public class AlarmTrigger extends Activity {
    String trackPath;
    Uri trackUri;
    MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_trigger);

        trackPath = getIntent().getExtras().getString("Track path"); 
        //getIntent from BroadcastReceiver class
        trackUri = Uri.parse(trackPath);
        Toast.makeText(this, trackUri.getPath(),Toast.LENGTH_SHORT).show(); // it works fine
        AlarmGoes();
    }

    public void AlarmGoes(){
        MediaPlayer mediaPlayer = MediaPlayer.create(this,trackUri); 
        mediaPlayer.start();
    }
}

The stack-trace:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.amr.mineapps.myapplication/com.amr.mineapps.awarm.AlarmTrigger}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2747)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1004)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:983)
at android.media.MediaPlayer.create(MediaPlayer.java:890)
at android.media.MediaPlayer.create(MediaPlayer.java:867)
at android.media.MediaPlayer.create(MediaPlayer.java:846)
at com.amr.mineapps.awarm.AlarmTrigger.onCreate(AlarmTrigger.java:40)
at android.app.Activity.performCreate(Activity.java:6845)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2700)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:165) 
at android.app.ActivityThread.main(ActivityThread.java:6375) 
at java.lang.reflect.Method.invoke(Native Method)
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
Amr Salah
  • 85
  • 9
  • This is not '`MediaPlayer.start()` on a null object reference'. It is `Uri.getScheme()` on a null object reference. Read the error message. – user207421 Apr 02 '19 at 04:31

1 Answers1

0

Try this code

MediaPlayer mediaPlayer = new MediaPlayer();

 // mediaPlayer.setDataSource(String.valueOf(myUri));
  mediaPlayer.setDataSource(AlarmTrigger.this,trackUri);
  mediaPlayer.prepare();   
  mediaPlayer.start();

In the previous activity, you should include FLAG_GRANT_READ_URI_PERMISSION in the intent before sending data https://commonsware.com/blog/2016/08/10/uri-access-lifetime-shorter-than-you-might-think.html

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Amine
  • 2,241
  • 2
  • 19
  • 41
  • Try it why? What's good about using two try/catch blocks where one would do? And what's good about ignoring two successive exceptions? – user207421 Apr 02 '19 at 04:30
  • you are right, there is no need for two try catch blocks. – Amine Apr 02 '19 at 06:56
  • i get this error 04-02 17:34:00.616 20900-20900/com.amr.mineapps.myapplication W/MediaPlayer: Couldn't open content://com.mi.android.globalFileexplorer.myprovider/external_files/Music/Avicii%20-%20The%20Nights.mp3: java.lang.SecurityException: Permission Denial: opening provider com.android.fileexplorer.provider.FileExplorerFileProvider from ProcessRecord{7360cb5 20900:com.amr.mineapps.myapplication/u0a402} (pid=20900, uid=10402) that is not exported from uid 10107 04-02 17:34:00.620 20900-20900/com.amr.mineapps.myapplication E/MediaPlayer: Unable to create media player – Amr Salah Apr 02 '19 at 15:39
  • it's a permission error; I will update the answer, take a look – Amine Apr 02 '19 at 15:56