在Cordova应用程序中播放音频 [英] playing audio sounds in Cordova application

查看:488
本文介绍了在Cordova应用程序中播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Cordova媒体插件在我的移动应用程序中播放音频 我尝试了很多代码,但是我没有弄清楚我到底在做什么错,我在代码中放了两段我尝试过的代码

I'm using Cordova media plugin for playing audio sound in my mobile application I tried many codes but I didn't figure out what I'm doing wrong at the bottom I put two piece of code that I tried them

第一个代码(单独文件中的js代码)

the first code (js code in a separate file)

   var app = {
    initialize: function() {
        this.bindEvents();
    },
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    onDeviceReady: function() {


        var myMedia = new Media("../sounds/clapping.mp3");
        myMedia.play();

    }
};

app.initialize();

第二个代码(脚本标记中的js代码):

the second code (js code in a script tag) :

     document.addEventListener("deviceready", function(){

   var myMedia = null;
    function playAudio() {

    var src = "sounds/clapping.mp3";

    if(myMedia === null) {
    myMedia = new Media(src, onSuccess, onError);

    function onSuccess() {
    console.log("playAudio Success");
    }

    function onError(error) {
    console.log("playAudio Error: " + error.code);
    }

    }

    myMedia.play();
    }

    document.getElementById("playAudio").addEventListener("click", playAudio);
    });

带有按钮:

<button id ="playAudio">PLAY</button>

我该如何解决这个问题?

How can I solve this problem ?

推荐答案

要回答您的问题,您可以在以下

To answer your question, you can find the working sample of cordova app using Media Plugin in the following github page.

如示例项目的自述文件中所述,您还必须安装cordova设备插件来检查设备平台.

As mentioned in the sample project's README, you gotta install cordova device plugin as well to check the device platform.

另外,为了澄清您在评论中提到的疑问,android_asset是指项目的根文件夹.

Also to clarify the doubt you mentioned in the comment, android_asset refers to the project's root folder.

这篇关于在Cordova应用程序中播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆