播放位于资产的文件夹的媒体文件 [英] Play media files located in assets folder

查看:204
本文介绍了播放位于资产的文件夹的媒体文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有在迅速加载和播放的时候使用媒体播放器类叫做Android项目的原始文件夹中的媒体文件集。我需要添加这些文件的更多的变化和分类到文件夹中,但显然原始文件夹不支持文件夹。我将能够迅速从资产的文件夹中加载这些文件,并与媒体播放器播放它们?如果是这样,怎么样?

I currently have a set of media files in the raw folder of the android project that are loaded quickly and played when called using the mediaplayer class. I need to add more variations of these files and categorize them into folders, but apparently the raw folder does not support folders. Would I be able to quickly load these files from the assets folder and play them with mediaplayer? If so, how?

推荐答案

我有这个方法返回的所有文件按扩展名的文件夹中的资产文件夹内:

I've this method that returns the all files by extension in a folder inside asset folder:

public static String[] getAllFilesInAssetByExtension(Context context, String path, String extension){
        Assert.assertNotNull(context);

        try {
            String[] files = context.getAssets().list(path);

            if(StringHelper.isNullOrEmpty(extension)){
                return files;
            }

            List<String> filesWithExtension = new ArrayList<String>();

            for(String file : files){
                if(file.endsWith(extension)){
                    filesWithExtension.add(file);
                }
            }

            return filesWithExtension.toArray(new String[filesWithExtension.size()]);  
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

如果你调用它使用:

getAllFilesInAssetByExtension(yourcontext, "", ".mp3");

这将返回资产文件夹的根目录的所有我的MP3文件。

this will return all my mp3 files in the root of assets folder.

如果你调用它使用:

getAllFilesInAssetByExtension(yourcontext, "somefolder", ".mp3");

这将在somefolder的MP3文件中搜索

this will search in "somefolder" for mp3 files

现在你已经列出所有要打开的文件,你需要这样的:

Now that you have list all files to open you will need this:

AssetFileDescriptor descriptor = getAssets().openFd("myfile");

要播放的文件只是做:

MediaPlayer player = new MediaPlayer();

long start = descriptor.getStartOffset();
long end = descriptor.getLength();

player.setDataSource(this.descriptor.getFileDescriptor(), start, end);
player.prepare();

player.setVolume(1.0f, 1.0f);
player.start();

希望这有助于

Hope this helps

这篇关于播放位于资产的文件夹的媒体文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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