简体   繁体   English

Android MediaPlayer可以在压缩文件中播放音频吗?

[英]Can Android MediaPlayer play audio in a zipped file?

CODE EDITED: 编辑编码:

I am developing a dictionary app for Android. 我正在为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();
            }       
     }      

    });

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. 但问题是一个文件夹中的WAV文件太多,所有这些文件都被Android设备视为音乐文件。 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. Can Android MediaPlayer play WAV/MP3 files zipped or wrapped in a single file? Android MediaPlayer可以播放压缩或包装在单个文件中的WAV / MP3文件吗? 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. 我的意思是我想压缩或包装音频文件(或做类似的事情),以便它们作为单个文件出现在Android设备上,但MediaPlayer仍然可以播放内部的每个单独的WAV文件。
  2. If the above is impossible, can you guys suggest a solution to the problem? 如果以上是不可能的,你们可以建议解决问题吗?

EDIT: 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? 编辑:有没有其他方法/解决方案,只允许将音频文件放入一个大文件(图像,zip或类似...),然后让MediaPlayer读取其中的单个文件?

Thank you very much. 非常感谢你。

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 . 您可以使用ZipFileZipInputStreamjava.io文件操作的组合来从zip读取必要的数据,创建临时文件并使用MediaPlayer播放这些文件。

Alternatively, you could just use a TTS engine and not pass out a 50-bagillion-byte APK. 或者,你可以使用TTS引擎而不是传递50万亿字节的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();
}

Notes: 笔记:

  1. I didn't include any safe file handling practices here. 我这里没有包含任何安全的文件处理方法。 That's up to you. 随你(由你决定。

  2. 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. 可能有100种其他方式,其中一些可能更适合您的需要。 I didn't use ZipInputStream simply because there's a little more logic involved and I was going for brevity. 我没有使用ZipInputStream只是因为涉及到更多的逻辑而我只是为了简洁。 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. 您必须检查每个条目以查看它是否是您正在寻找的ZipInputStream ,而ZipFile允许您只是通过名称询问您想要的内容。 I'm not sure what (if any) performance implications using either over the other would have. 我不确定使用其他任何一个会产生什么(如果有的话)性能影响。

  3. 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. 绝不是你需要使用临时文件(或者根本就是文件),但Android的MediaPlayer并不真正喜欢流,所以这可能是最简单的解决方案。

An alternative you should consider is to download the individual sound files when the user want to listen to a pronunciation. 您应该考虑的另一种方法是在用户想要收听发音时下载单个声音文件。 This should reduce the file size although it does mean that you can't listen to a pronunciation when there is no Internet. 这应该会减小文件大小,尽管它确实意味着在没有Internet的情况下你不能听发音。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM