Android Assets 文件夹中的 Mp3 音频无法从已签名且与 zip 对齐的 APK 播放 [英] Mp3 audio in the Android Assets folder fails to play from a signed and zip-aligned APK

查看:23
本文介绍了Android Assets 文件夹中的 Mp3 音频无法从已签名且与 zip 对齐的 APK 播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加载 mp3 文件以在基于 AndEngine 的 Android 游戏中播放.当我将游戏打包为 APK 时,assets 文件夹中的 mp3 音频会抛出错误无法作为文件描述符打开;它可能已被压缩."

I am loading mp3 files to play in an Android game based on AndEngine. When I package my game as an APK, mp3 audio in the assets folder throws the error "can not be opened as a file descriptor; it is probably compressed."

但是,当我使用 Eclipse (MOTODEV Studio 3.0.2) 中的运行 > 按钮运行游戏时,应用程序被打包、部署到设备并且游戏有声音.在设备上完美运行.

However, when I run the game using the run > button in Eclipse (MOTODEV Studio 3.0.2), the app is packaged, deployed to a device and the game has sound. Works perfectly on a device.

如果我使用导出 Android 应用程序"打包应用程序,这会创建一个准备在 Android 市场中部署的签名 APK,然后将该 APK 安装在设备上,游戏运行完美除了声音不播放并抛出以下错误:

If I package the app using "Export Android Application", which creates a signed APK ready to deploy in the Android Market and then install that APK on a device, the game functions perfectly except the sound doesn't play and throws the following error:

> 02-02 20:42:31.433: E/AndEngine(1925): AndEngine 02-02 20:42:31.433:
> E/AndEngine(1925): java.io.FileNotFoundException: This file can not be
> opened as a file descriptor; it is probably compressed 02-02
> 20:42:31.433: E/AndEngine(1925):  at
> android.content.res.AssetManager.openAssetFd(Native Method) 02-02
> 20:42:31.433: E/AndEngine(1925):  at
> android.content.res.AssetManager.openFd(AssetManager.java:330) 02-02
> 20:42:31.433: E/AndEngine(1925):  at
> org.anddev.andengine.audio.music.MusicFactory.createMusicFromAsset(MusicFactory.java:75)
> 02-02 20:42:31.433: E/AndEngine(1925):    at
> com.snoffleware.android.roshambomb.PlayLevelActivity.onLoadResources(PlayLevelActivity.java:255)
> 02-02 20:42:31.433: E/AndEngine(1925):    at
> org.anddev.andengine.ui.activity.BaseGameActivity.doResume(BaseGameActivity.java:168)
> 02-02 20:42:31.433: E/AndEngine(1925):    at
> org.anddev.andengine.ui.activity.BaseGameActivity.onWindowFocusChanged(BaseGameActivity.java:85)
> 02-02 20:42:31.433: E/AndEngine(1925):    at
> com.android.internal.policy.impl.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:2012)
> 02-02 20:42:31.433: E/AndEngine(1925):    at
> android.view.View.dispatchWindowFocusChanged(View.java:3924) 02-02
> 20:42:31.433: E/AndEngine(1925):  at
> android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:659)
> 02-02 20:42:31.433: E/AndEngine(1925):    at
> android.view.ViewRoot.handleMessage(ViewRoot.java:1968) 02-02
> 20:42:31.433: E/AndEngine(1925):  at
> android.os.Handler.dispatchMessage(Handler.java:99) 02-02
> 20:42:31.433: E/AndEngine(1925):  at
> android.os.Looper.loop(Looper.java:130) 02-02 20:42:31.433:
> E/AndEngine(1925):    at
> android.app.ActivityThread.main(ActivityThread.java:3683) 02-02
> 20:42:31.433: E/AndEngine(1925):  at
> java.lang.reflect.Method.invokeNative(Native Method) 02-02
> 20:42:31.433: E/AndEngine(1925):  at
> java.lang.reflect.Method.invoke(Method.java:507) 02-02 20:42:31.433:
> E/AndEngine(1925):    at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:850)
> 02-02 20:42:31.433: E/AndEngine(1925):    at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608) 02-02
> 20:42:31.433: E/AndEngine(1925):  at
> dalvik.system.NativeStart.main(Native Method)

我尝试将 APK 部署到 Kindle Fire、Motorolo Droid 和 Motorolo Droid 2 Global.该游戏适用于所有这些设备,但没有声音.

I have tried deploying the APK to a Kindle Fire, Motorolo Droid and Motorolo Droid 2 Global. The game works on all of these devices but without sound.

音频以 mp3 文件的形式存储在 Assets 下名为 Sound 的文件夹中.示例文件名是music-a-nebulas-promise.mp3".我曾尝试重命名其中一个文件simple.mp3",担心命名中不允许使用破折号,但没有任何区别.

The audio is stored as mp3 files in a folder under Assets called Sound. An example filename is "music-a-nebulas-promise.mp3". I have tried renaming one of the files, "simple.mp3", worried that dashes are not allowed in the naming but it didn't make any difference.

加载音频的代码如下,请注意,当我使用 MOTODEV Studio 运行应用程序时,声音可以完美播放.只有当我尝试将应用程序导出为 APK 并安装它时,才会出现上述有关压缩的错误:

The code that loads the audio is as follows and please note, when I run the application using MOTODEV Studio the sound is played back perfectly. It is only when I try to export the app as an APK and install it that I get the aforementioned error regarding compression:

SoundFactory.setAssetBasePath("sound/");
        MusicFactory.setAssetBasePath("sound/");
        try {
            if (!level.getMusic().equals("")) {
                backgroundMusic = MusicFactory.createMusicFromAsset(this.mEngine.getMusicManager(), this,   level.getMusic());
                backgroundMusic.setLooping(true);

            } else {
                // music is turned on but there is no music file -- protect
                // against crash
                isMusic = false;
            }
        } catch (final IOException e) {
            Debug.e(e);
            // music is turned on but the music file was not found -- protect
            // against crash
            isMusic = false;
        }

        try {
            collisionSound = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "sfx-collision.mp3");
            portalSound = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "sfx-portal-activated.mp3");
            winningSound = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "sfx-winning.mp3");
            losingSound = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "sfx-losing.mp3");
        } catch (final IOException e) {
            Debug.e(e);
            isSound = false;
        }

初始化音频的AndEngine方法:

The AndEngine method that initializes the audio:

public static Music createMusicFromAsset(final MusicManager pMusicManager, final Context pContext, final String pAssetPath) throws IOException {
    final MediaPlayer mediaPlayer = new MediaPlayer();

    final AssetFileDescriptor assetFileDescritor = pContext.getAssets().openFd(MusicFactory.sAssetBasePath + pAssetPath);
    mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(), assetFileDescritor.getStartOffset(), assetFileDescritor.getLength());
    mediaPlayer.prepare();

    final Music music = new Music(pMusicManager, mediaPlayer);
    pMusicManager.add(music);

    return music;
}

我尝试将 mp3 文件转换为 ogg 和 wav.ogg 和 wav 都抛出与 mp3 文件相同的错误.

I have tried converting the mp3 files to both ogg and wav. Both ogg and wav throw the same error as mp3 files.

因为当我使用 Eclipse 运行或调试时游戏声音有效,这似乎将最终导出步骤作为罪魁祸首.我看过一些帖子,建议控制构建过程以防止压缩 Assets 文件夹中的文件,但我不确定如何从 MOTODEV Studio 中实现.

Since the game sound works when I run or debug it using Eclipse that seems to leave the final export step as the culprit. I have seen posts that suggest controlling the build process to prevent compression of files in the Assets folder but I'm not sure how to accomplish that from MOTODEV Studio.

我还看到一些帖子建议将 mp3 音频存储在 res/raw 文件夹中,但由于 Assets 文件夹中的音频在从 Eclipse 运行时播放,因此在打包为 APK 时它似乎也应该可以工作.

I have also seen posts that suggest storing the mp3 audio in the res/raw folder but since audio in the Assets folder plays when run from Eclipse, it seems that it should also work when packaged as an APK.

我目前使用 Android SDK Tools v.15 和 Android SDK Platform-tools v.9 定位 Android SDK 级别 13(Android 3.2)——所以认为它可能与 SDK 工具有关,我更新了 SDK到 v.16,平台工具到 v.10,但这并没有解决问题.

I am current targeting Android SDK level 13 (Android 3.2) using Android SDK Tools v.15 and Android SDK Platform-tools v.9 -- so thinking it might have something to do with the SDK tools, I updated the SDK to v.16 and the platform tools to v.10 but that did not fix the problem.

推荐答案

问题似乎与AndEngine的MusicFactory.java中的mediaPlayer.prepare()方法有关.我刚刚注释掉了第 91 行(如以下代码片段中所示),并且 MP3 资源已无任何异常地加载.我认为资产加载方法也可以做到这一点.

The problem seems to be related to the mediaPlayer.prepare() method in the MusicFactory.java of AndEngine. I've just comment out line 91 (as in the following code snipet) and the MP3 resource was loaded without any exceptions. I think the same can be done with the asset loading method.

public static Music createMusicFromResource(final MusicManager pMusicManager, final Context pContext, final int pMusicResID) throws IOException {
        final MediaPlayer mediaPlayer = MediaPlayer.create(pContext, pMusicResID);
        **//mediaPlayer.prepare();**

        final Music music = new Music(pMusicManager, mediaPlayer);
        pMusicManager.add(music);

        return music;
    }

这篇关于Android Assets 文件夹中的 Mp3 音频无法从已签名且与 zip 对齐的 APK 播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆