在Android资产文件夹的MP3音频失败从签署和zip对齐APK玩 [英] Mp3 audio in the Android Assets folder fails to play from a signed and zip-aligned APK

查看:318
本文介绍了在Android资产文件夹的MP3音频失败从签署和zip对齐APK玩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载MP3文件在Android游戏的基础上AndEngine玩。当我打包我的游戏作为一个APK,在资产文件夹mp3音频引发错误不能打开一个文件描述符;它可能是COM pressed

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.

如果我包使用导出的Andr​​oid应用程序,它创建了一个签名的APK准备部署在Android Market,然后安装应用程序,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全球。游戏适用于所有的这些设备,但没有健全的。

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文件,在资产的文件夹骂声研究。一个例子文件名是音乐-A-星云 - 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.

在code加载音频是如下,请注意,当我运行使用MOTODEV Studio中的回放声音完美的应用程序。只有当我尝试将应用程序导出为APK,并安装它,我得到关于COM pression前面提到的错误:

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中调试似乎让最后导出步骤的罪魁祸首。我已经看到,建议控制构建过程prevent COM $ P $的资产文件夹的文件pssion职位,但我不知道如何完成从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 /原始文件夹,但由于音频的资产文件夹播放从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工具第15节和Android SDK平台工具第9节针对的Andr​​oid SDK级别13(安卓3.2) - 所以认为它可能有一些做的SDK工具,我更新了SDK到第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的媒体播放器。prepare()方法。我刚刚注释掉线91(如下面的code snipet)和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资产文件夹的MP3音频失败从签署和zip对齐APK玩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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