0

Addig a file to MediaPlayer is easy:

mp = MediaPlayer.create(Myctivity.this, R.raw.sound);

But what if I have the filename as an argument coming from a function like this?

  public void CreateSound(String filename, float volume)
    {
        mp = MediaPlayer.create(Myctivity.this, R.raw.???);
             mp.setVolume(volume, volume);
           try {
            mp.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
           mp.start();
    }
erdomester
  • 11,789
  • 32
  • 132
  • 234

1 Answers1

0

This thread mentions

 int resID=getResources().getIdentifier(fname, "raw", getPackageName());

which takes a file name and fetches the ID. (OBS: Before using the file name it's converted to lower case) This can then be passed to the MediaPlayer:

MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
Community
  • 1
  • 1
keyser
  • 18,829
  • 16
  • 59
  • 101