2

I have an expansion file with a series of sound files. The longer ones are .mp3 files while the shorter sound effects are .wav files. I pack them in a .zip file with no compression. I use the Zip File reader and downloader library provided by Google.

I'm opening the sound files like so:

MediaPlayer mPlayer;

...
AssetFileDescript asd = expansionFile.getAssFileDescriptor(fileName);
FileDescriptor soundFile = asd.getFileDescriptor();
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(soundFile);
mPlayer.prepare();
mPlayer.start();

The odd thing is that it plays the same sound file every single time. It seems to be whatever it finds first. The byte size for asd.getDeclaredLength() and asd.getLength() is exactly what it should be. This means the file descriptor is at least finding the file, but for some reason the MediaPlayer plays whatever random file it decides.

What exactly am I missing here? Any help would be greatly appreciated.

Thanks.

DeeV
  • 35,865
  • 9
  • 108
  • 95
  • Can you tell me the method of creating the expansion file, I also needs to upload mp3 files as expansion and read from it – Pramod J George Oct 05 '12 at 10:33
  • I'm assuming you've read these guidelines. http://developer.android.com/guide/google/play/expansion-files.html – DeeV Oct 07 '12 at 05:15
  • 3
    Aside from that, the way you do it is use a program like WinRar or WinZip. Archive the files you need and name them appropriately. **DO NOT USE COMPRESSION**. In the case of WinRar, you'd say "Archive for storage" which will do 0% compression. This is important for sound files as it screws with the data. Once it's done archiving, simply rename the file extension ".zip" to ".obb". – DeeV Oct 07 '12 at 05:16

1 Answers1

5

The first approximation is correct, except for when the zip file contains more than one file, then you should use:

mPlayer.setDataSource(asd.getFileDescriptor(), asd.getStartOffset(), asd.getLength());

You can get more information from this other question: Play audio file from the assets directory

Please, also remember to close the AssetFileDescriptor just after setDataSource to media player:

asd.close();

http://developer.android.com/reference/android/media/MediaPlayer.html#setDataSource%28java.io.FileDescriptor,%20long,%20long%29

Community
  • 1
  • 1
pauminku
  • 66
  • 2
  • Thank you! I kept thinking the fault was in the expansion file library, and caused a lot of frustration. – DeeV Jul 05 '12 at 12:22