【问题标题】:Phonegap Build: Why Audio is not playing?Phonegap Build:为什么没有播放音频?
【发布时间】:2014-04-04 08:58:45
【问题描述】:

如果我使用 phonegap 构建,我将无法调试,因为开发人员工具没有名为 Media 的对象。但我已经构建了应用程序并在设备(Android 4.1.2)中进行了测试,但音频无法播放。

在我添加了Media 功能后,它就停止工作了。

这是代码示例:

config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<widget xmlns   = "http://www.w3.org/ns/widgets"
    xmlns:gap   = "http://phonegap.com/ns/1.0"
    id          = "com.phonegap.audiotest"
    versionCode = "10" 
    version     = "1.0.0" >
<!-- versionCode is optional and Android only -->

<name>Audio Test</name>

<description>
    An example for phonegap Audio. 
</description>

<author href="https://www.whatwgisnottelling.com" email="anyname@gmail.com">
    justTest
</author>

<gap:plugin name="org.apache.cordova.media" version="0.2.8" />

</widget>

索引.html

<script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" src="js/jquery.mobile-1.4.2.js"></script>
    <script type="text/javascript" src="js/main.js"></script>
    <script type="text/javascript">
        var media = null;   
        $(document).one('deviceready', App.initialize());   
</script>

main.js

var App = {
    initialize: function () {
        this.touchEvents();
    },
    touchEvents: function () {
        $(document).bind('touchmove', function(e) {
            e.preventDefault();
        });
        var media = new Media();
        var box = document.getElementById('box');
        box.addEventListener('touchstart', function () {
            box.style.backgroundColor = 'black';
            media.play("song.wav");
        });
        box.addEventListener('touchend', function () {
            box.style.backgroundColor = 'red';
            media.stop("song.wav");
        });
       }
};

【问题讨论】:

    标签: cordova phonegap-build


    【解决方案1】:

    媒体文件的路径不正确。您必须正确获取路径:

      var getPathMedia = function () {
        var path = window.location.pathname;
        var phoneGapPath = path.substring(0, path.lastIndexOf('/') + 1);
        // Return path at www folder
        return 'file://' + phoneGapPath;
      };
      var media = new Media(getPathMedia() + 'media/message.mp3');
    

    【讨论】:

    • 我尝试过使用和不使用'file://' 以及使用和不使用media/message.wav,但它都不起作用。有什么想法吗?
    • 我无法在 Eclipse 上进行仿真,我只是构建应用程序并在手机中对其进行测试。我收到“1”的错误。
    猜你喜欢
    • 2015-08-24
    • 1970-01-01
    • 2019-01-01
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    相关资源
    最近更新 更多