-1

I've been working on a Processing Sketch in Android Mode. I had problems with Minim library so, I decided to use MediaPlayer. What I want to do is very simple, to play an mp3 file.

Code:

import android.media.MediaPlayer;
import android.media.AudioManager;
import android.net.Uri;
MediaPlayer p;

void setup()
{
  //Uri needed for the create Method
  Uri u = Uri.parse("m.mp3");//The file is in the data folder
  p = MediaPlayer.create(this, u);
  p.start();
}

I know that in Android you make an Uri like "R.raw.m"... Now, the app doesn't work when I run it and I think is due to the Uri. I just don't know how to make it work. Thanks a lot.

1 Answers1

0

Not sure what are you trying to do, but here is how you are going to play from assets dir in Android.

 MediaPlayer player;

 void play(String trackName) {
    try {
        AssetFileDescriptor desc = getAssets().openFd(trackName);
        player = new MediaPlayer();
        player.setDataSource(desc.getFileDescriptor(),desc.getStartOffset(),desc.getLength());
        player.prepare();
        player.start();
    }
    catch (Exception e) {
        Log.d("TMS", "Error playing sound: " + trackName);
    }
}

Just tested this code and it works. Just create assets dir if it doesn`t exist in your project and put there your .mp3 file. Then call this function with the mp3 file name as argument. If this is not the right answer, please be more specific what are you trying to do.

Also check this link Play notification audio by URI

Community
  • 1
  • 1
Adrian Ivasku
  • 1,118
  • 3
  • 16
  • 31