Phonegap:无法找到录制的文件

时间:2016-02-11 10:24:08

标签: android cordova phonegap-plugins phonegap-build

我正在使用phonegap的媒体插件录制声音。 录音后我找不到录制的声音。

录制后如何找到录制的声音?

我正在使用以下代码:

的index.html

<!DOCTYPE html>
 <html>
 <head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Recorder</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js">  </script>
    <script type="text/javascript" charset="utf-8">

    <script>
    function startRecording()
    {
        var src = "FinalAudio.wav";
        myMedia = new Media(src, onSuccess, onError);
        myMedia.startRecord();
        alert("Started recording");
     }
    function onSuccess() {
        console.log("Created Audio for Recording");
    }
    function onError(error) {
        alert('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');
    }
    function stopRecording()
    {
        myMedia.stopRecord();
        alert("Stopped recording");
    }
    window.onload = function()
    {
        document.addEventListener("deviceready", onDeviceReady, false);
    }
    function onDeviceReady()
    {
        document.getElementById("btnStart").addEventListener('click', startRecording, false);
        document.getElementById("btnStop").addEventListener('click', stopRecording, false);
    }
 </script>
 </head>
 <body>
<button id="btnStart">Start Recording</button>
<button id="btnStop">Stop Recording</button>
</body>
</html>

config.xml中

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.domainurl.name" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
<name>New Recorder</name>
<description>
    Hello World sample application that responds to the deviceready event.
</description>
<author email="support@phonegap.com" href="http://phonegap.com">
    PhoneGap Team
</author>

<gap:platform name="ios" />
<gap:platform name="android" />
<gap:platform name="winphone" />

 <preference name="orientation" value="default" />
 <preference name="fullscreen" value="true" />
  <preference name="permissions" value="none" />
<preference name="target-device" value="universal" />
<preference name="webviewbounce" value="true" />
<preference name="prerendered-icon" value="true" />
<preference name="stay-in-webview" value="false" />
<preference name="ios-statusbarstyle" value="black-opaque" />
<preference name="detect-data-types" value="true" />
<preference name="exit-on-suspend" value="false" />
<preference name="show-splash-screen-spinner" value="true" />
<preference name="auto-hide-splash-screen" value="true" />
<preference name="disable-cursor" value="false" />
<preference name="android-minSdkVersion" value="14" />
<preference name="android-installLocation" value="auto" />
<gap:plugin name="com.phonegap.plugins.barcodescanner" />
<gap:plugin name="org.apache.cordova.battery-status" />
<gap:plugin name="org.apache.cordova.camera" />
<gap:plugin name="org.apache.cordova.media-capture" />
<gap:plugin name="org.apache.cordova.console" />
<gap:plugin name="org.apache.cordova.contacts" />
<gap:plugin name="org.apache.cordova.device" />
<gap:plugin name="org.apache.cordova.device-motion" />
<gap:plugin name="org.apache.cordova.device-orientation" />
<gap:plugin name="org.apache.cordova.dialogs" />
<gap:plugin name="org.apache.cordova.file" />
<gap:plugin name="org.apache.cordova.file-transfer" />
<gap:plugin name="org.apache.cordova.geolocation" />
<gap:plugin name="org.apache.cordova.globalization" />
<gap:plugin name="org.apache.cordova.inappbrowser" />
<gap:plugin name="org.apache.cordova.media" />
<gap:plugin name="org.apache.cordova.network-information" />
<gap:plugin name="org.apache.cordova.splashscreen" />
<gap:plugin name="org.apache.cordova.vibration" />
 <preference name="webviewbounce" value="false" />

 <preference name="DisallowOverscroll" value="false" />

 <preference name="disallowOverscroll" value="false" />

 <preference name="exit-on-suspend" value="true" />

 <access origin="*" subdomains="true" />

2 个答案:

答案 0 :(得分:0)

在文档中,说IOS路径是这样的:

Files can be recorded and played back using the documents URI:

var myMedia = new Media("documents://beer.mp3")

在android中并不清楚,可能在/ media或/ data文件夹中

答案 1 :(得分:0)

@ user2899728

既不是/ media或/ data。这取决于你如何创建它。或者如果它已经存储在设备上。

这是working example。在您的情况下,“当前目录”与index.html所在的目录相同,或者根目录

注意:如果您使用的是 CLI ,那么 。在这种情况下,您的config.xml是index.html下面的一个目录,但您的音频文件仍然位于相同的相对目录中。

最后,如果您使用 Phonegap Build ,请说明。

这是工作代码(源代码中的更多注释)。 - 最好的运气

function onDeviceReady() {
    var audioFile = "";
    if (device.platform === "iOS") {
        // iOS picks it up fom the current directory 
        audioFile = "ice_cream_truck.wav";
    } else if (device.platform == 'Android') {
        audioFile = "/android_asset/www/fanfare3.ogg";
    }
    // Don't do much with this right now. Just play it.
    my_media = new Media(audioFile, onSuccess, onError);
    // The 'numberOfLoops' is iOS only.
    my_media.play({ numberOfLoops: 99 });
}