使用shouldInterceptRequest()在Android WebView中无法使用音频

时间:2016-01-14 17:58:55

标签: android html5 audio android-webview android-mediaplayer

我的Android应用程序在WebView中显示html5电子书 我有一个压缩文件,其中包含一个包含所有资源的电子书:文本,图像和音频(mp3文件) 为了解压缩本书,我使用了shouldInterceptRequest(),它拦截了file:/// ... requests,并通过WebResourceResponse对象返回数据。该代码适用于文本和图像 当我到达音频资源时,我得到运行时错误,并且没有播放音频文件 注意:我确实看到以正确的大小(大约10MB)返回解压缩的文件。

我收到错误消息:
cr_MediaResourceGetter文件不存在
cr_MediaResourceGetter无法配置元数据提取器

我的音频HTML代码:

<div xmlns="http://www.w3.org/1999/xhtml">
  <p style="text-align:center;margin:0px;">
    <audio controls="controls" src="../Audio/01-AudioTrack-01.mp3">Your browser does not support the audio tag.</audio>
    <br />
  </p>
</div>

我的Android代码:

    setWebViewClient(new WebViewClient()
    {
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, final String url)
    {
        String urlWithoutAnchor = URLUtil.stripAnchor(url); 
        String fileName = urlWithoutAnchor;

        try {
            byte [] resource = tbxPool.getResource(fileName); 
         /* SIMPLE VERSION without calling setResponseHeaders():
            return new WebResourceResponse(mimeType, "UTF-8", new ByteArrayInputStream(resource)); 
            */
            WebResourceResponse returnedMediaResource = new WebResourceResponse(mimeType, "UTF-8", new ByteArrayInputStream(resource));
            if (mimeType.toLowerCase().startsWith("audio")) {
                Map<String, String> responseHeaders = new HashMap<String, String>();

                responseHeaders.put("Content-Type", mimeType);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//2CLEAN
                    returnedMediaResource.setResponseHeaders(responseHeaders); 
                    Logger.v(TAG, "Response Headers added to audio resource");
                }
                else {
                    //TODO: Handle else for API<21. Toast?
                }
            }                               
            return returnedMediaResource;
        } catch (IOException e) {
            Logger.e(TAG, "failed to load resource "+fileName,e);
            return null;
        }
    }
}

环境
Android 6.0.1(Nexus 5) Android系统WebView版本47

要求澄清
音频是在浏览器中播放的,就像html5文档一样,不需要外部播放器。

问题:
我究竟做错了什么?!非常感谢提前!

1 个答案:

答案 0 :(得分:2)

我发现这个问题的解决方法并不优雅,但它是唯一适合我的方法:将音频文件写入SD卡:(。
阶段1):当使用章节URL调用shouldInterceptRequest()时 该章首先被拦截(在其他章节资源(图像,音频,字体,......)被拦截之前 当章节被拦截时,我们在html中搜索<audio>标签。如果找到,我们替换相对路径(例如SRC =“../ Audio / abc.mp3”) 绝对路径(例如SRC =“/ storage / tmp / abc.mp3”)

阶段2):当使用音频网址调用shouldInterceptRequest()时 你的注意力。像所有的解决方法一样,这有点棘手(但有效!):
在阶段1)之后,音频网址将是一个绝对网址(绝对网址是现在在修改后的网址中写的)。
我们现在要做两件事:
a)从压缩的epub中读取音频文件    为此,我们需要“欺骗”代码,并从其原始的压缩相对URL中读取音频文件,例如我们的例子中有“../Audio/abc.mp3”    (虽然使用“/storage/tmp/abc.mp3”调用了shouldInterceptRequest) b)读取压缩的音频文件后,将其写入存储(sdcard)

阶段3)当使用章节url调用shouldInterceptRequest()时,   我们删除临时音频文件   注意:如果您遵循代码,您将看到这是在第1阶段之前执行的shouldInterceptRequest()中的步骤0),但我发现它更清楚如上所述。

if (isChapterFile(mimeType)) {  
    deleteTempFiles(); // this line is stage 3)  
        changeAudioPathsInHtml(tzis); // this line is stage 1)

这是代码:

setWebViewClient(new WebViewClient()
{
    private String tmpPath = TbxApplication.getAppPath(null) + "/tmp/"; 
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, final String url) 
    {
        Logger.d(TAG, "in shouldInterceptRequest for " + url);  
        String urlWithoutAnchor = URLUtil.stripAnchor(url); 
        String mimeType = StringUtils.getFileMimeType(urlWithoutAnchor);
        String urlWithoutBase; //the url stripped from leading 'epubBaseUrl' (base url for example:"file:///storage/.../123456.tbx")

            if (isAudioFile(mimeType)) { //write AUDIO file to phone storage. See AUDIO WORKAROUND DOCUMENTATION    
                String storagePath = StringUtils.truncateFileScheme(url);  //WebView calls shoudlInterceptRequest() with "file://..."
                try {
                    String oEBPSAudioPath = storagePathToOEBPSAudioPath(storagePath); //e.g. change"/storage/tmp" to "OEBPS/Audio/abc.mp3"
                    byte[] audioBytes = tbxPool.getMedia(oEBPSAudioPath); 
                    FileUtils.writeByteArrayToFile(audioBytes, storagePath); //TODO: To be strict, write in separate thread
                    Logger.d(TAG, String.format("%s written to %s", oEBPSAudioPath, storagePath));
                    return null;//webView will read resource from file
                                //Note: return new WebResourceResponse("audio/mpeg", "UTF-8", new ByteArrayInputStream(audioBytes));
                                //did NOT work,so we had to change html for audio to point to local storage & write to disk
                                //see AUDIO WORKAROUND DOCUMENTATION in this file
                } catch (Exception e) {
                    Logger.e(TAG,e.getMessage());
                    return null;
                } 
            } 
            .....
            else {
                if (isChapterFile(mimeType)) { //This is a CHAPTER
                    deleteTempFiles(); //Loading a new chapter. Delete previous chapter audio files. See AUDIO WORKAROUND DOCUMENTATION in this file
                    InputStream htmlWithChangedAudioPaths = changeAudioPathsInHtml(tzis); //see AUDIO WORKAROUND DOCUMENTATION in this file
                    WebResourceResponse webResourceResponse = new WebResourceResponse(mimeType, "UTF-8", htmlWithChangedAudioPaths);
                    return webResourceResponse; 
                }

        //Changes relative paths of audio files, to absolute paths on storage
        //see AUDIO WORKAROUND DOCUMENTATION in this file
        private InputStream changeAudioPathsInHtml(InputStream inputStream) {
            String inputString = StringUtils.inputStreamToString(inputStream);
            String outputString = inputString.replaceAll("\"../Audio/", "\"" + tmpPath);// e.g. SRC="../Audio/abc.mp3" ==>SRC="/sdcard/tmp/abc.mp3"                                                                              //where '*' stands for multiple whitespaces would be more elegant
            return StringUtils.stringToInputStream(outputString); 
        }

        /** Example:
         * storagePath="/storage/tmp/abc.mp3
         * Returns: "OEBPS/Audio/abc.mp3"*/
        private String storagePathToOEBPSAudioPath(String storagePath){
            String fileName = StringUtils.getFileName(storagePath);
            String tbxOEBPSAudioPath = "OEBPS/Audio/" + fileName;
            return tbxOEBPSAudioPath; 
        }

public static void writeByteArrayToFile(byte[] byteArray, String outPath) { 
    try {
        File file = new File(outPath);
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(byteArray);
        fos.close();
    } catch (IOException e) {
        Logger.e(TAG, String.format("Could not write %s", outPath));
    }
}