【问题标题】:Unable to play audio in android emulator using html5 audio tag无法使用 html5 音频标签在 android 模拟器中播放音频
【发布时间】:2012-01-05 05:45:27
【问题描述】:

我开发了一个包含所有 htmls 标签的 html5 页面。我将该文件推送到 android 模拟器/设备中。当我尝试启动该页面时,我能够查看图像,但无法播放音频..还有其他原因吗?请尽快帮助我..

我可以在桌面浏览器上播放 tat 音频。

【问题讨论】:

标签: android html html5-audio


【解决方案1】:

Android(WebKit) 2.2 默认浏览器在技术上支持 HTML5 音频,但无法播放使用流行编解码器编码的任何媒体文件。由于浏览器供应商对 HTML5 音频标签的实现不一致,您需要使用 JavaScript 来准确确定是否可以使用原生音频或后备音频。

【讨论】:

    【解决方案2】:

    这只是实现从 android 中的 html 文件播放音频的目标的替代方式。因为android 2.2不支持HTML5的audio标签。

    使用 Android 的 Mediaplayer (http://developer.android.com/reference/android/media/MediaPlayer.html) 播放音频。您可以从您在 HTML 文件中编写的 javascript 调用 android 的功能..

    这是如何从 javascript 代码调用用 java 文件编写的函数的示例 (内置 ShowAndroidToast 函数,您可以编写代码使用 mediaplayer 对象播放音频。)

      WebView webView = (WebView) findViewById(R.id.webview);
        webView.addJavascriptInterface(new WebAppInterface(this), "Android");
        --------------------------------
        public class WebAppInterface {
    
            Context mContext;
            /** Instantiate the interface and set the context */
            WebAppInterface(Context c) {
                mContext = c;
            }
    
            /** Show a toast from the web page */
            @JavascriptInterface
            public void showToast(String toast) {
                Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
            }
        }
    
        ---------------------------
        java sript code
    
        <input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
    
        <script type="text/javascript">
            function showAndroidToast(toast) {
                Android.showToast(toast);
            }
        </script>
    

    这样您就可以从 Android 代码中调用音频。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-16
      • 1970-01-01
      • 2017-04-12
      • 2019-03-11
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多