從擴展文件播放聲音文件 (Playing sound files from expansion file)


問題描述

從擴展文件播放聲音文件 (Playing sound files from expansion file)

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.


參考解法

方法 1:

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

(by DeeVpauminku)

參考文件

  1. Playing sound files from expansion file (CC BY-SA 3.0/4.0)

#apk-expansion-files #android-mediaplayer #Android #android-resources






相關問題

從擴展文件播放聲音文件 (Playing sound files from expansion file)

如何掛載加密的APK擴展文件? (How to mount encrypted APK expansion files?)

如何使用jobb工具Android創建OBB文件 (How to create OBB files USING jobb tool Android)

發布不顯示谷歌地圖的 APK 文件 (Release APK file not showing google maps)

PHP - 上傳APK到自己的服務器 (PHP - upload APK to own server)

如果從 Google Play 商店下載 apk 和 obb,則無法在 android 10 設備上訪問 obb 擴展文件 (Cannot reach obb expansion file on android 10 device if apk and obb is downloaded from Google Play store)

APK 擴展文件下載顯示驗證的各種進度 (APK Expansion File Download shows various progress on validation)

APK 擴展文件下載不起作用 (APK Expansion file download doesn´t work)

掛載 .obb 時未調用 OnObbStateChangeListener (OnObbStateChangeListener not called when mounting .obb)

可以掛載未加密的obb但加密錯誤21 (can mount unencrypted obb but with encrypted error 21)

無法在魅族 m3 note 上掛載 .obb 文件(state = 21) (Can't mount .obb file (state = 21) on Meizu m3 note)







留言討論