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

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

问题描述

我尝试播放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文件没有找到,但我仍然不知道如何解决它。

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提供的播放方法对于播放网络上可用的媒体文件非常有用。 Phonegap提供媒体插件来播放本地媒体文件。我检查了您的代码,并通过进行以下更改成功地播放声音。请检查您的结尾。

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 played 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.更换index.html中的行

2.Replace the lines in index.html

<button onclick="document.getElementById('successSound').play()">Play successSound local</button>
<button onclick="document.getElementById('errorSound').play()">Play errorSound 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();
}

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

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