java - Android MediaPlayer 可以播放压缩文件中的音频吗?

标签 java android eclipse audio media-player

代码已编辑:

我正在为 Android 开发一个字典应用程序。我已经成功地让应用程序发音了每个被查找的单词。这是代码:

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设备视为音乐文件。因此,索引此类文件需要很长时间。此外,索引和用户浏览有时会迫使应用程序崩溃。

因此,我只是想知道是否可以通过编程方式完成以下操作:

  1. Android MediaPlayer 可以播放压缩或打包在单个文件中的 WAV/MP3 文件吗?我的意思是我想压缩或打包音频文件(或做类似的事情),以便它们显示为一个文件文件到 Android 设备,但 MediaPlayer 仍然可以播放其中的每个单独的 WAV 文件。
  2. 如果以上都不可行,你们能提出解决问题的方法吗?

编辑: 是否有任何其他方法/解决方案允许将音频文件简单地放入一个大文件(图像、zip 等...),然后让 MediaPlayer 读取其中的单个文件?

非常感谢。

最佳答案

您可以使用 ZipFile 的组合或 ZipInputStreamjava.io 文件操作以从 zip 中读取必要的数据,创建临时文件并使用 MediaPlayer 播放这些文件。

或者,您可以只 use a TTS engine并且不会传递 50 亿字节的 APK。

编辑 - 按要求举例:

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. 我没有在此处包含任何安全文件处理实践。这取决于你。

  2. 这不是方式,只是一种方式 .可能还有 100 种其他方法,其中一些可能更适合您的需要。我没有使用 ZipInputStream 只是因为涉及到更多的逻辑并且为了简洁起见。您必须使用 ZipInputStream 检查每个 条目以查看它是否是您正在寻找的内容,而 ZipFile 允许您只询问您要的内容想要的名字。我不确定使用其中任何一个会产生什么(如果有的话)性能影响。

  3. 绝对不是要求使用临时文件(或者根本不是文件,真的),但 Android 的 MediaPlayer 并不真正喜欢流,因此这可能是最简单的解决方案。

关于java - Android MediaPlayer 可以播放压缩文件中的音频吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8900249/

相关文章:

java - 如何仅在Java中从文本文件中读取字母(a-z || A-Z)和数字(0-9)字符?

java - 函数作为参数 : Strong Coupling?

java - Android:收到后如何删除对firebase动态链接的引用?

java - 在 GitHub 上使用 Eclipse 建立 Java 项目的正确方法是什么

eclipse - 如何在 JDK 中启动 eclipse?

java - 如何在 SWT 中强制 StyledText 换行

java - 非分布式场景下的 Apache Storm

java - JDialog:防止父框架关闭

android - 在linux shell脚本中执行程序

安卓错误 :You must supply layout_width attribute