atbebtg
atbebtg

Reputation: 4083

Playing audio file with MediaPlayer

Hi there I'm an android and java newbie and is wondering if there is a better way of doing the following:

I currently have 250 short audio files (.3gp) that i put inside my res/raw folder and based on a certain condition, I want to play certain audio file. For example if the text of the button that is being clicked is the word "and", I want to play the and.3gp sound file.

The following is the code that I use to accomplish what I want to do:

if(word.equals("No"))
{   
    MediaPlayer mp = MediaPlayer.create(HomeActivity.this,R.raw.no);
    mp.start();
    mp.setOnCompletionListener(listener );
}
else if(word.equals("And"))
{
    MediaPlayer mp = MediaPlayer.create(HomeActivity.this,R.raw.and);
    mp.start();
    mp.setOnCompletionListener(listener );
}

.....

The above code is working fine so far but I'm wondering if I can do this without having a 250 else if condition. Is there a way to pass in the audio file to MedialPlayer.Create by file name via Uri? If yes, how do I do it? Thank you.

Upvotes: 2

Views: 1765

Answers (2)

Jason Robinson
Jason Robinson

Reputation: 31294

Loop through all your files in your raw folder and remove the file extension, then test against that.

Upvotes: 1

Hazem Farahat
Hazem Farahat

Reputation: 3790

You can make a hash function that maps a string key to the uri

Upvotes: 2

Related Questions