播放声音在PhoneGap的应用程序为Android [英] Play sound on Phonegap app for Android

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

问题描述

我尝试播放MP3文件。这工作,如果我的路径更改为我的本地网络服务器的文件,但如果我运行这个在Android设备上的声音不打了,并没有显示任何错误。

I try to play an mp3 file. This works if I change the path to the file on my local webserver, but if I run this on an Android device the sound is not played and no error is shown.

我未找到的MP3文件pretty的舒尔,但我还是不知道如何解决它。

I am pretty shure that the mp3 file is not found, but I still don't know how to fix it.

这是在 HTML

  <body>

        <audio id="successSound" src="/android_asset/www/sound/successSound.mp3" type="audio/mpeg" ></audio>
           <audio id="errorSound" src="/android_asset/www/sound/errorSound.mp3" type="audio/mpeg" ></audio>
<!-- some more UI -->
  </body>

这是在 Javascript的

document.getElementById('errorSound').play();

的是文件结构

phonyapp
`-- www
    `-- index.html
        |-- sound
        |   |-- errorSound.mp3
        |   `-- successSound.mp3
        |-- res
        `-- spec

修改1

我试过

<audio id="successSound" src="sound/successSound.mp3" type="audio/mpeg" ></audio>

这曾在Chrome浏览器在我的本地网络服务器,但不是在Android上。

This worked in chrome on my local webserver but not on Android.

我试过

<audio id="successSound" src="http://html5multimedia.com/media/sayHello.mp3" type="audio/mpeg" ></audio>

这工作,但我需要得到本地文件播放

This worked, but I need to get local files playing

推荐答案

该剧方法,它的HTML5 API提供了用于播放媒体文件可在web.Phonegap有用提供媒体插件来播放本地媒体files.I签出您的code和通过以下changes.Please支票最终运行的声音成功。

The play method which the HTML5 API provides is useful for playing media files available on the web.Phonegap provides the media plugin to play local media files.I checked out your code and run the sound successfully by making the following changes.Please check at your end.

1.增加以下行到config.xml

1.Add the following line to config.xml

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

2.Replace index.html中的行

2.Replace the lines in index.html

<button onclick="document.getElementById('successSound').play()">Play successSound local</button>
<button onclick="document.getElementById('successSound').play()">Play successSound local</button>

这些行

<button onclick="playAudio('successSound')">Play successSound local</button>
<button onclick="playAudio('errorSound')">Play errorSoundlocal</button>

3.添加以下函数的js / index.js

3.Add the following function to js/index.js

function playAudio(id) {
    var audioElement = document.getElementById(id);
    var url = audioElement.getAttribute('src');
    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();
}

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

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