缺少cordova.js和Uncaught ReferenceError:Media未定义

时间:2014-03-12 14:56:04

标签: android cordova

所以我使用最新版本的Cordova创建了一个从头开始的应用程序,并遵循Cordova API文档中的说明。我想在Android上安装一个非常基本的mp3播放器,所以我安装了Android平台并通过CLI安装了Media和Device插件。我还使用了API文档中的媒体播放器的完整示例。我的代码目前看起来像这样:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
    <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" />
    <title>Hello World</title>
</head>
<body>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">
        app.initialize();
    </script>

    <script type="text/javascript" charset="utf-8">

    // Wait for device API libraries to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // device APIs are available
    //
    function onDeviceReady() {
        playAudio("https://ia600808.us.archive.org/21/items/NasaAudioHighlightReels/Launch-Sound_STS-1-Launch.mp3");
    }

    // Audio player
    //
    var my_media = null;
    var mediaTimer = null;

    // Play audio
    //
    function playAudio(src) {
        // Create Media object from src
        my_media = new Media(src, onSuccess, onError);

        // Play audio
        my_media.play();

        // Update my_media position every second
        if (mediaTimer == null) {
            mediaTimer = setInterval(function() {
                // get my_media position
                my_media.getCurrentPosition(
                    // success callback
                    function(position) {
                        if (position > -1) {
                            setAudioPosition((position) + " sec");
                        }
                    },
                    // error callback
                    function(e) {
                        console.log("Error getting pos=" + e);
                        setAudioPosition("Error: " + e);
                    }
                );
            }, 1000);
        }
    }

    // Pause audio
    //
    function pauseAudio() {
        if (my_media) {
            my_media.pause();
        }
    }

    // Stop audio
    //
    function stopAudio() {
        if (my_media) {
            my_media.stop();
        }
        clearInterval(mediaTimer);
        mediaTimer = null;
    }

    // onSuccess Callback
    //
    function onSuccess() {
        console.log("playAudio():Audio Success");
    }

    // onError Callback
    //
    function onError(error) {
        alert('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');
    }

    // Set audio position
    //
    function setAudioPosition(position) {
        document.getElementById('audio_position').innerHTML = position;
    }

    </script>
  </head>
  <body>
    <a href="#" class="btn large" onclick="playAudio('https://ia600808.us.archive.org/21/items/NasaAudioHighlightReels/Launch-Sound_STS-1-Launch.mp3');">Play Audio</a>
    <a href="#" class="btn large" onclick="pauseAudio();">Pause Playing Audio</a>
    <a href="#" class="btn large" onclick="stopAudio();">Stop Playing Audio</a>
    <p id="audio_position"></p>

</body>
</html>

我已在CLI中构建应用程序,然后使用Chrome Developer运行它,我得到2个错误,1)找不到cordova.js文件,2)未捕获的ReferenceError:未定义媒体。< / p>

我知道当添加Android平台时,cordova文件位于platform_www文件夹中,但如何链接到该文件夹​​?我已尝试将cordova.js复制到主www文件夹,但每次我都收到此链接中描述的消息Dreamweaver CC and cordova.js

这是推动坚果,所以如果有人能够对这个问题有所了解,那将非常感激!

1 个答案:

答案 0 :(得分:2)

我认为你的代码运行正常。如果您想使用浏览器进行测试,则需要使用ripple chrome扩展程序在移动环境中运行。如果您未在移动环境中运行该应用,则会收到此错误消息。

Uncaught ReferenceError: cordova is not defined 

https://chrome.google.com/webstore/detail/ripple-emulator-beta/geelfhphabnejjhdalkjhgipohgpdnoc?hl=en