在Android的Phonegap应用程序上播放声音

编程入门 行业动态 更新时间:2024-10-10 23:18:23
本文介绍了在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="html5multimedia/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应用程序上播放声音

本文发布于:2023-11-12 13:29:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581635.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   声音   Android   Phonegap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!