简体   繁体   中英

Play dynamic MP3 files in MediaPlayer

I need to play local mp3 from the RAW folder. When i am calling it with direct name, like this way it works fine:

final MediaPlayer mP = MediaPlayer.create(MainActivity.this, R.raw.sound);
mP.start();

But i cannot figure it out how to play mp3 according to a variable containing the name of mp3, for example:

String filemane = "my.mp3";
final MediaPlayer mP = MediaPlayer.create(MainActivity.this, R.raw.filemane);
mP.start();

I understand this doesn't work since in the raw folder there is no file "filemane.mp3"

Please point me how to play "my.mp3"

使用Uri作为第二个参数。

MediaPlayer.create(MainActivity.this, Uri.fromFile(new File(path)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM