Search code examples
androiduriandroid-mediaplayer

How do I load an mp3 from raw using Uri.parse in android?


How do I load an mp3 from raw using Uri.parse? The variable name is a string (such as fun (if the file was fun.mp3)). When I try to play an audio file from raw, but receive a null object reference error. I'm probably missing something uber simple. Thanks.

    player = MediaPlayer.create(this, Uri.parse("R.raw." + name));
    player.start();

Solution

  • How do I load an mp3 from raw using Uri.parse?

    You don't, usually.

    When I try to play an audio file from raw, but receive a null object reference error.

    Among other things, that is not a valid string to parse as a Uri.

    Use the create() method that takes the raw resource ID as an int parameter. If your problem is that you do not have that int, use getResources().getIdentifier() to look it up by name.