android MediaPlayer is playing the wrong mp3 file in assets directory
I'm trying to play an mp3 file from the assets directory, but when I start it up with the MediaPlayer, something completely different plays. Here's the code:
String mp3File = "dir/a/music.mp3"; //the path here is file:///android_asset/dir/a/music.mp3;
AssetManager assetMan = getAssets();
MediaPlayer media = new MediaPlayer();
FileInputStream mp3Stream = assetMan.openFd(mp3File).createInputStream();
media.setDataSource(mp3Stream.getFD());
media.prepare();
media.start();
Instead of playing mp3File, it seems to pl开发者_JAVA技巧ay a bunch of other files that reside in the assets directory. Any ideas?
Use this way it is very useful function :)
public void playBeep() {
try {
if (m.isPlaying()) {
m.stop();
m.release();
m = new MediaPlayer();
}
AssetFileDescriptor descriptor = getAssets().openFd("mp3 name.mp3");
m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
m.prepare();
m.setVolume(1f, 1f);
m.setLooping(true);
m.start();
} catch (Exception e) {
}
}
MediaPlayer mp = new MediaPlayer();
AssetFileDescriptor descriptor;
descriptor = getAssets().openFd( "filename.mp3" );
mp.setDataSource( descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength() );
descriptor.close();
mp.prepare();
mp.start();
put your mp3 in the assets folder.
you can refer this link also play-audio-file-from-the-assets-directory
and this also android-problem-playing-sounds-from-assets-folder
精彩评论