0

I am trying to develop a language learning app where I play dialog lines on a button click (5-10 seconds each). So, I created arrays of dialog lines for each dialog, i.e:
currentAudioArray {"line0", "line2", "line3"}
They are names of the audio files placed in res/raw folder

But I can't get to play them the way I need. I am using MediaPlayer.create() method, but it seems that it only takes direct links to specific files (i.e. R.raw.mySoundfile).

How can I feed an array usingsomething like looping [i] into MediaPlayer.create()? Or is there any other class or method to play a next audio file on a NEXT button click?

So far I tried using: mediaPlayer= MediaPlayer.create(Listen.this, currentAudioArray[0])); mediaPlayer.start();

But it gives me "unresolved method" error because it does not like currentAudioArray[0] being a string and not URL.

I also tried: mediaPlayer.setDataSource(currentAudioArray[0])

but this also gives arrows once it gets to mediaPlayer.prepare().

Any suggestions or ideas on how to play those files in array are very very welcome!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
anne
  • 29
  • 5

2 Answers2

0

There is one thing you need to keep in mind:

You are using the same instance for all the tracks. So whenever you click 'next', you have to stop and dispose the MediaPlayer which is currently playing.

If you're getting an error, try to re-initialize the MediaPlayer instance and Create() it again: mp = MediaPlayer.create(this, R.raw.mySoundfile); instead of the setDataSource method

Check this question if you need to loop through the "raw" folder: How to read many file from raw resource in loop?

Alan Deep
  • 2,037
  • 1
  • 14
  • 22
0

Alright, I found my problem! Originally I set my array of audio files as strings:

String[] currentAudioArray {"line0", "line2", "line3"}. But instead I should have set them as int and also add R.raw path like this:

int[] currentAudioArray {R.raw.line0, R.rawline2, R.raw.line3}

In this case: mediaPlayer= MediaPlayer.create(Listen.this, currentAudioArray[i])) did not have any problems with integer values.

So it worked! Hope it helps someone else!

anne
  • 29
  • 5