可以的Andr​​oid的MediaPlayer在一个压缩文件播放音频? [英] Can Android MediaPlayer play audio in a zipped file?

查看:186
本文介绍了可以的Andr​​oid的MediaPlayer在一个压缩文件播放音频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

code编辑:

我为Android开发一个字典应用程序。我已经成功地使应用程序,每一个字正在抬头。这里是code:

I am developing a dictionary app for Android. I have been succeeding in making the app pronounce each word being looked up. Here is the code:

btnPronounce.setOnClickListener(new View.OnClickListener() {            
        @Override
        public void onClick(View v) {                               
            // TODO Auto-generated method stub
            //Log.i(MAIN_TAG,"Start pronounciation ...");
            btnPronounce.setEnabled(false);
            String currentWord = edWord.getText().toString().toLowerCase();         

            try {
                ZipFile zip = new ZipFile("/sdcard/app_folder/sound/zip_test.zip");
                ZipEntry entry = zip.getEntry(currentWord);
                if (entry != null) {
                    InputStream in = zip.getInputStream(entry);
                    // see Note #3.
                    File tempFile = File.createTempFile("_AUDIO_", ".wav");
                    FileOutputStream out = new FileOutputStream(tempFile);
                    IOUtils.copy(in, out);

                    // do something with tempFile (like play it)
                    File f = tempFile;   
                    try {
                        if (f.exists())
                        {
                            Log.i(MAIN_TAG,"Audio file found!");
                            MediaPlayer mp = new MediaPlayer();

                            mp.prepare();
                            mp.setLooping(false);
                            mp.start();
                            while (mp.getCurrentPosition() < mp.getDuration());
                            mp.stop();
                            mp.release();
                            Log.i(MAIN_TAG,"Pronounciation finished!");
                        }   
                      else
                        {
                            Log.i(MAIN_TAG,"File doesn't exist!!");
                        }
                    }
                    catch (IOException e)
                    {
                        Log.i(MAIN_TAG,e.toString());
                    }
                    btnPronounce.setEnabled(true);  }

                else {
                    // no such entry in the zip
                }
            } catch (IOException e) {
                // handle your exception cases...
                e.printStackTrace();
            }       
     }      

    });

但问题是,有一个文件夹中太多WAV文件,并且所有这些文件都被视为由Android设备的音乐文件。其结果是,它需要年龄索引此类文件。此外,索引和用户浏览有时会强制应用程序崩溃。

But the problem is that there are too many WAV files in one folder, and all these files are treated as music files by Android devices. As a result, it takes ages to index such files. In addition, indexing and user browsing sometimes force the app to crash.

所以,我只是想知道下可以以编程方式完成的:

Therefore, I just wonder if the following could be programmatically done:

  1. 能的Andr​​oid的MediaPlayer播放WAV / MP3文件压缩或包裹在一个单独的文件?我的意思是我要压缩或包裹的音频文件(或做一些相似),使它们显示为一个单一的文件复制到Android设备上的MediaPlayer,但仍然可以玩里面每一个人的WAV文件。
  2. 如果以上是不可能的,你们可以提出一个解决问题的办法?
  1. Can Android MediaPlayer play WAV/MP3 files zipped or wrapped in a single file? I mean I want to zip or wrap the audio files (or do something alike) so that they appear as one single file to Android devices but MediaPlayer can still play each individual WAV file inside.
  2. If the above is impossible, can you guys suggest a solution to the problem?

编辑: 是否有任何其他的方法/解决方案,使音频文件被简单地说成一个大文件(图像,zip或类似......),然后让阅读的MediaPlayer在其个人档案?

Are there any other ways/solutions that allow audio files to be simply put into one big file (an image, zip or the like...) and then let MediaPlayer read individual files in it?

非常感谢你。

推荐答案

您可以使用的 的ZipFile 或的 ZipInputStream java.io 文件操作读取压缩必要的数据,创建临时文件和那些使用发挥的MediaPlayer

You can use a combination of ZipFile or ZipInputStream and java.io file operations to read the necessary data from the zip, create temp files and play those using MediaPlayer.

另外,你可以只使用TTS引擎并没有传递出一个50 bagillion字节的APK。

Alternatively, you could just use a TTS engine and not pass out a 50-bagillion-byte APK.

修改 - 使用实例的请求:

Edit - Example by request:

try {
    ZipFile zip = new ZipFile("someZipFile.zip");
    ZipEntry entry = zip.getEntry(fileName);
    if (entry != null) {
        InputStream in = zip.getInputStream(entry);
        // see Note #3.
        File tempFile = File.createTempFile("_AUDIO_", ".wav");
        FileOutputStream out = new FileOutputStream(tempFile);
        IOUtils.copy(in, out);
        // do something with tempFile (like play it)
    } else {
        // no such entry in the zip
    }
} catch (IOException e) {
    // handle your exception cases...
    e.printStackTrace();
}

注:

  1. 我不包括任何安全文件处理的做法在这里。这取决于你。

  1. I didn't include any safe file handling practices here. That's up to you.

这是没有的的方法做到这一点,只有 办法做。可能有100个其他的方式,其中一些可能会更适合你的需求。我没有使用 ZipInputStream 仅仅是因为有涉及多一点的逻辑,我要去为简洁起见。你必须检查的每次的入口,看它是否是你要找的内容与 ZipInputStream ,而的ZipFile ,您可以只问你想要的名字是什么。我不知道是什么(如果有的话),使用性能的影响比任何其他会这么做。

This isn't the way to do it, only a way to do it. There are probably 100 other ways, some of which may be better suited to what you need. I didn't use ZipInputStream simply because there's a little more logic involved and I was going for brevity. You have to check every entry to see if it's what you're looking for with ZipInputStream, whereas ZipFile allows you to just ask for what you want by name. I'm not sure what (if any) performance implications using either over the other would have.

绝不是你的需要的使用临时文件(或文件在所有的,真的),而Android的MediaPlayer的并不真的很喜欢流,所以这可能是最简单的解决方案。

By no means are you required to use temp files (or files at all, really), but Android's MediaPlayer doesn't really like streams, so this is probably the easiest solution.

这篇关于可以的Andr​​oid的MediaPlayer在一个压缩文件播放音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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