简体   繁体   中英

Android - Problem playing sounds from assets folder

i have 5 mp3 files stored on the assets folder. The files are all 25 KB.
I load the files using:

manager = context.getAssets();
this.inputStream = manager.openFd(fileName).createInputStream();

Whenever i try to play the files, the sounds are all messed up like they were mixed or something. I've zipaligned the app already but with no results.
anny help about this issue? Thanks in advance

After some research i've found the awnser myself. the problem was i was using the following method to set the MediaPlayer's datasource:

inputStream = manager.openFd(fileName).createInputStream();    
player.setDataSource(inputStream.getFD());

Wich is just a call to setDataSource(fd, 0, 0x7ffffffffffffffL); , passing the min offset and this arbitrary length, causing the sounds to be played all mixed.
When using the following code everything worked fine:

AssetFileDescriptor descriptor = manager.openFd(fileName);
long start = descriptor.getStartOffset();
long end = descriptor.getLength();
player.setDataSource(descriptor.getFileDescriptor(), start,end);

You can also try playing them from the res/raw folder:

MediaPlayer p=MediaPlayer.create(this, R.raw.soundid);
p.start();
  1. For start try to eliminate one potential problem: compare inputStream with the original file.

  2. Try opening and playing files directly.

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