在后台服务中加载资产文件

时间:2015-08-29 12:57:39

标签: android android-service android-assets

我正在开发一个播放随机声音的小部件。我的问题在于获取我用来处理MediaPlayer的服务并播放声音以加载我存储在应用程序资产文件夹中的声音文件。
我选择SoundPool而不是MediaPlayer,因为它有一个onCompletionListener,它允许我在声音完全播放后停止服务。这样我希望最小化窗口小部件的资源使用。

更确切地说,在我的代码(checkout the [now outdated] commit on GitHub)中,我尝试使用MediaPlayer create()便捷方法using an Uri to file:///android_asset/pathToFile加载随机声音。
但是,这会在运行时抛出IOException。

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null) {
        final String action = intent.getAction();
        if (ACTION_PLAY_FART.equals(action)) {
            Log.v(LOG_TAG, "Intent received");

            String pathToFile = String.format(
                    Locale.US,
                    "fart%02d.ogg",
                    Utility.getIntBetween(1, 15)
            );
           MediaPlayer tempMediaPlayer = MediaPlayer.create(
                    this,
                    Uri.parse("file:///android_asset/"+pathToFile)
            );
            tempMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    mp.release();
                    stopSelf();
                }
            });
            Log.v(LOG_TAG, "Start playing");
            tempMediaPlayer.start();
        }
    }

    return START_NOT_STICKY;
}

是否有一种简单的方法(即内容提供商除外)加载来自内部 Service

[编辑,提供更多信息]

显然可以访问资产文件,但MediaPlayer无法正确加载它们。

我将代码修改为:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null) {
        final String action = intent.getAction();
        if (ACTION_PLAY_FART.equals(action)) {
            Log.v(LOG_TAG, "Intent received");

            String pathToFile = String.format(
                    Locale.US,
                    "fart%02d.ogg",
                    Utility.getIntBetween(1, 15)
            );
            try {
                AssetFileDescriptor assetFD = this.getAssets().openFd(pathToFile);

                MediaPlayer tempMediaPlayer = new MediaPlayer();
                tempMediaPlayer.setDataSource(assetFD.getFileDescriptor());
                tempMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mp.release();
                        stopSelf();
                    }
                });
                Log.v(LOG_TAG, "Start playing");
                tempMediaPlayer.start();
            } catch (IOException e) {
                Log.e(LOG_TAG, "File "+pathToFile+" could not be loaded.", e);
            }
        }
    }

我得到的输出是:

08-29 15:29:54.868  10191-10191/com.y0hy0h.furzknopf V/VolatileFartService﹕ Intent received
08-29 15:29:54.868  10191-10191/com.y0hy0h.furzknopf V/MediaPlayer﹕ constructor
08-29 15:29:54.868  10191-10191/com.y0hy0h.furzknopf V/MediaPlayer﹕ setListener
08-29 15:29:54.868  10191-10191/com.y0hy0h.furzknopf V/MediaPlayer﹕ setDataSource(51, 0, 576460752303423487)
08-29 15:29:54.898  10191-10191/com.y0hy0h.furzknopf E/MediaPlayer﹕ Unable to to create media player
08-29 15:29:54.908  10191-10191/com.y0hy0h.furzknopf E/VolatileFartService﹕ File fart07.ogg could not be loaded.
    java.io.IOException: setDataSourceFD failed.: status=0x80000000
            at android.media.MediaPlayer.setDataSource(Native Method)
            at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1032)
            at com.y0hy0h.furzknopf.widget.VolatileFartService.onStartCommand(VolatileFartService.java:39)
            at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2524)
            at android.app.ActivityThread.access$1900(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4929)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
            at dalvik.system.NativeStart.main(Native Method)

注意第39行是

tempMediaPlayer.setDataSource(assetFD.getFileDescriptor());

Alternitavely,有没有办法传递 FileDescriptor或其他对象来加载服务?

或者可能有一个甚至更好的方式来同时处理可能多个声音的播放,而不是将该功能放在Service中?

2 个答案:

答案 0 :(得分:0)

试试这个;

  • 在资源目录中创建一个原始文件夹,例如res / raw
  • 将文件放在原始文件夹中
  • 使用媒体播放器加载文件,如下所示

    MediaPlayer mediaPlayer = MediaPlayer.create(serviceClass.this,R.raw.sound_file_1); mediaPlayer.start();

了解更多; http://developer.android.com/guide/topics/media/mediaplayer.html#mpandservices

答案 1 :(得分:0)

作为Want2bExpert's suggestion将文件移动到res / raw文件夹的替代方法,可以手动加载资产文件"进入MediaPlayer。

显然资产是not stored as would be expected。相反,资产的起始位置和长度及其( Asset - )FileDescriptor需要传递给MediaPlayer。

工作代码:

String pathToFile = String.format(
        Locale.US,
        "fart%02d.ogg",
        Utility.getIntBetween(1, 15)
);

try {
    AssetFileDescriptor assetFD = getAssets().openFd(pathToFile);
    MediaPlayer tempMediaPlayer = new MediaPlayer();
    tempMediaPlayer.setDataSource(
            assetFD.getFileDescriptor(),
            assetFD.getStartOffset(),
            assetFD.getLength()
    );
    assetFD.close();
    tempMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.release();
            stopSelf();
        }
    });
    tempMediaPlayer.prepare();
    Log.v(LOG_TAG, "Start playing");
    tempMediaPlayer.start();
} catch (IOException e) {
    Log.e(LOG_TAG, "File "+pathToFile+" could not be loaded.", e);
}