javascript 如何在Android中播放歌曲 - phonegap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4159117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 10:33:20  来源:igfitidea点击:

How to play song in Android - phonegap

javascriptandroidcordovaaudio

提问by Mihail Velikov

A month ago I started working with phonegap, html5, css3 and jQtouch. I am working on an application and I need to play sound in the application. I have a serious problem with this task. First I've found out that I can play .mp3 files through phonegap using the new Media(...) function. I am not sure about .wav files, can I play those? Second I've found that files must be less than 30 sec, is this so? Third I can't find the right place for my sound files. My project structure is the following:

一个月前,我开始使用 phonegap、html5、css3 和 jQtouch。我正在开发一个应用程序,我需要在应用程序中播放声音。我对这项任务有一个严重的问题。首先,我发现我可以使用新的 Media(...) 功能通过 phonegap 播放 .mp3 文件。我不确定 .wav 文件,我可以播放这些文件吗?其次,我发现文件必须少于 30 秒,是吗?第三,我找不到我的声音文件的正确位置。我的项目结构如下:

project
-- src
-- gen
-- assets
-- -- www
-- -- DANCE.mp3
-- -- jqtouch
-- -- -- (some folders and files)
-- -- phonegap.js
-- libs
-- res

I've tried placing the file in 'www' folder and creating a new one called 'audio'. None of this gave me what I wanted. I am using this code for executing the song:

我尝试将文件放在“www”文件夹中并创建一个名为“audio”的新文件。这些都没有给我想要的。我正在使用此代码来执行歌曲:

function playStream() {
    mp3file = new Media("DANCE.mp3",
            function() {
                alert("playAudio():Audio Success");
            },
                function(err) {
                    alert(err);
            }
            );
          mp3file.play();
}

I use Android 2.1 Simulator and I've tried 2.2 as well without success. I hope I was clear enough. I am looking forward to hearing from you. Yours, Mihail Velikov

我使用 Android 2.1 Simulator,我也尝试过 2.2,但没有成功。我希望我说得够清楚了。我期待着您的回音。你的,米哈伊尔·维利科夫

回答by Hyman Palevich

PhoneGap for Android lets you store local audio files in two places: android_asset: file name must start with /android_asset/sound.mp3 sdcard: file name is just sound.mp3

适用于 Android 的 PhoneGap 允许您将本地音频文件存储在两个位置: android_asset:文件名必须以 /android_asset/sound.mp3 开头 sdcard:文件名就是 sound.mp3

If you place your DANCE.mp3 file in your project's assets directory, then you can play it using the path:

如果将 DANCE.mp3 文件放在项目的资产目录中,则可以使用以下路径播放它:

mp3file = new Media("/android_asset/DANCE.mp3",
        function() {
            alert("playAudio():Audio Success");
        },
            function(err) {
                alert(err);
        }
        );
      mp3file.play();

(You can read the source for AudioPlayer.java in the Android PhoneGap sources to see how this works.)

(您可以在 Android PhoneGap 源代码中阅读 AudioPlayer.java 的源代码以了解其工作原理。)

回答by Kim Ras

Can I offer an advice.. If you use the PhoneGap Media player.. You need to remove the file:// from the path.. It doesn not accept a full URI for local files Regards Kim

我可以提供建议吗.. 如果您使用 PhoneGap 媒体播放器.. 您需要从路径中删除 file://.. 它不接受本地文件的完整 URI 问候 Kim

回答by Mihail Velikov

I have found out where was my mistake. The audio file that I want to play was not placed properly, as I thought at first. I had moved it in /sdcard folder and everything was just wonderful! P.S. I use only filename, no folders.

我已经发现我的错误在哪里。我想播放的音频文件没有正确放置,正如我最初所想的那样。我已经将它移到 /sdcard 文件夹中,一切都很棒!PS我只使用文件名,没有文件夹。

回答by Aaron Saunders

why dont you just play the songs using the HTML 5 audio tag? Your view is a webView.. is shouldn't matter, it is simpler and you have more control.

为什么不使用HTML 5 音频标签播放歌曲?你的视图是一个 webView .. 应该没有关系,它更简单,你有更多的控制权。