javascript - phonegap 4.0 媒体插件 ReferenceError : Media is not defined

标签 javascript cordova audio

我对编程相当陌生,我正在尝试构建我的第一个phonegap应用程序。我正在尝试播放位于我的/www/sounds/文件夹中的一些声音。但是,当我在浏览器(chrome)中运行index.html时,我收到“ Uncaught ReferenceError :媒体未定义”错误。

这是我的 HTML:

<html>
<head>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="msapplication-tap-highlight" content="no" />
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
    <link rel="stylesheet" type="text/css" href="css/index.css" />
    <!--jQuery mobile-->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
    <script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

    <script type="text/javascript" src="cordova.js"></script>
   <script type="text/javascript" src="js/index.js"></script>
</head>
<body>


<div id="files-list-page" data-role="page">
    <header data-role="header">
        <h1>Fragmenten</h1>
    </header>
    <div data-role="content">
        <ul id="files-list" data-role="listview" data-autodividers="true" data-filter="false" data-split-icon="delete">
            <audio id="fr1" src="sounds/5euros.mp3" type="audio/mp3" ></audio>
        </ul>
        <button id="btn1" class=ui-btn" onclick="playAudio('sounds/5euros.mp3')">Play</button>
    </div>
    <footer data-role="footer">
        <h3 id="copyright-title">Soundboard</h3>
    </footer>
</div>

</body>

这是我的 JS:

var app = {
// Application Constructor
initialize: function() {
    this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
    app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
    var parentElement = document.getElementById(id);
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');

    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');

    console.log('Received Event: ' + id);
}


};

app.initialize();


// Play audio
//
function playAudio(url) {
    // Play the audio file at url
    var my_media = new Media(url,
        // success callback
        function () {
            console.log("playAudio():Audio Success");
        },
        // error callback
        function (err) {
            console.log("playAudio():Audio Error: " + err);
        }
    );
    // Play audio
    my_media.play();
}

我的config.xml:

    <?xml version='1.0' encoding='utf-8'?>
<widget id="com.martijn.HuisbaasBob" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>HuisbaasBob</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="dev@cordova.apache.org" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <access origin="*" />
</widget>

任何建议都会有很大帮助,提前致谢!

最佳答案

您需要在 config.xml 中添加此行才能使您的插件正常工作...

(在 app/res/xml/config.xml 中)

<feature name="Media">
    <param name="android-package" value="org.apache.cordova.media.AudioHandler" />
</feature>

“ Uncaught ReferenceError :媒体未定义”意味着您的代码无法识别它...意味着“未配置”

关于javascript - phonegap 4.0 媒体插件 ReferenceError : Media is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28363666/

相关文章:

javascript - Accordion 菜单自动折叠

javascript - tweenmax 动画无法分配给只读属性 '_gsTweenID'

java - cordova 插件添加外部 .aar 文件(不是 .jar)

android - 在 Phonegap Android 应用程序中安装 FacebookConnect 插件时出错

c++ - OpenAL 和 Vista : Device is always 'Generic Software'

java - Android:为什么我的按钮不播放它的音效?

android - 从 Intent 播放音频 - ActivityNotFoundException

javascript - 尝试从 Chrome 中的本地 .html 访问 JSON API

javascript - 来自 ajax 的 Bootstrap 表 json

android - jQuery Mobile + Phonegap::Ajax "Loading"gif 没有动画