简体   繁体   中英

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 play 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

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