-1

So I've been trying some stuff with the MediaPlayer, and it works if I do this:

mediaPlayer = MediaPlayer.create(this, R.raw.over);

However, obviously this only allows me to launch that one file. So, if I try something like this:

Uri myUri = Uri.parse("R.raw.over");
mediaPlayer = MediaPlayer.create(this, myUri);

then the mediaPlayer doesn't get created. Any method call

mediaPlayer.start()

throws a NullPointerException.

My idea is that, if I get this to work, then eventually I can playback a file from an ArayList containing URIs.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Yoey
  • 408
  • 1
  • 4
  • 16

2 Answers2

1

Please, try to apply this code:

Uri myUri = ....; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();
Adam Ostrožlík
  • 1,256
  • 1
  • 10
  • 16
1

If all the files that you want to play is in raw folder then you don't need to parse a Uri you can just store them in an array and play through that array:

int[] files = {R.raw.over, R.raw.over1, R.raw.over2}
mediaPlayer = MediaPlayer.create(this, files[0]);
alireza
  • 514
  • 3
  • 14