1

我正在为一个项目使用 PhoneGap,但我在上传 .mp3 时遇到了一些问题

我可以使用设备的内置麦克风录制 mp3,然后我可以使用以下 JavaScript 上传文件:

var audio_name = 'fyp_app_recording_' + Math.round(new Date().getTime()/1000) + '.mp3';

// Record audio
// 
function recordAudio() {

    var src = audio_name;
    var mediaRec = new Media(src, onSuccess(src), onError);

    // Record audio
    mediaRec.startRecord();

    // Stop recording after 10 sec
    var recTime = 0;
    var recInterval = setInterval(function() {
        recTime = recTime + 1;
        setAudioPosition("Recording Audio - " + recTime + " sec");
        if (recTime >= 10) {
            clearInterval(recInterval);
            mediaRec.stopRecord();
            setAudioPosition("Recording Saved As " + src);
        }
    }, 1000);
}

// onSuccess Callback
//
function onSuccess(src) {
    console.log("recordAudio():Audio Success" + src);
}

// onError Callback 
//
function onError(error) {
    alert('code: '    + error.code    + '\n' + 
          'message: ' + error.message + '\n');
}

// Set audio position
// 
function setAudioPosition(position) {
    document.getElementById('audio_position').innerHTML = position;
}

//File upload

    function uploadAudio() {    

      var options = new FileUploadOptions();
        options.fileKey="file";
        options.fileName="test.mp3";
        options.mimeType="audio/mpeg";

        var ft = new FileTransfer();
        ft.upload("mnt/sdcard/" + audio_name, "http://www.richardsonweb.co.uk/fyp/test1/upload.php", win, fail, options);
    }

    function win() {
        alert("Yay! It worked!");
    }

    function 7(error) {
        alert("An error has occurred: Code = " = error.code);
    }

upload.php 文件包含以下代码:

    print_r($_FILES);



$new_file_name = time() . ".mp3";
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$new_file_name);

但是,当我从 FTP 客户端下载文件时,Windows Media Player 声称文件已损坏。

奇怪的是,文件在 VLC 中播放良好(进度条在 10 秒时卡住,并且没有移动,但它可以完美地播放音频)。

任何想法为什么会发生这种情况?提前致谢!

4

1 回答 1

2

try using this php code

<?php
   $destination_path = getcwd().DIRECTORY_SEPARATOR;

   $result = 0;

   $target_path = $destination_path . time() . ".mp3";

   ini_set("upload_max_filesize", "3200000000");

   if(@move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
      $result = 1;
   }

   sleep(1);
?>
于 2013-05-13T16:29:49.693 回答