as3/phpmp3字节数组的传输和写入


as3/php mp3 byte array transport and write

一段时间以来,我一直在思考一个问题。

我正在做一个儿童游戏(flash as3),里面给孩子们读故事、诗歌、歌曲等。有一段朗读/演唱文字的录音,文字在说话/演唱时会突出显示。孩子可以独立地打开和关闭语音和单词突出显示。孩子也可以选择自己录制。现在说录音效果很好。我可以从麦克风上捕捉到它,孩子可以毫无问题地回放。

问题在于客户端希望孩子能够将此录制保存到web服务器。

我想我已经通过使用URLRequest和URLLoader对象解决了这个问题。mp3出现在Web服务器上指定的文件夹中。我用媒体播放器播放,效果很好。孩子也可以加载所说的文件没有问题。

然后,当我通过浏览器(而不是flash播放器)尝试它时,我得到了可怕的沙盒错误。在浏览器环境中对所述对象进行这种操作的唯一方式是用户通过对话窗口启动。这是孩子们在谈论的,我们无论如何都不会在当地储蓄。

为孩子提供了3个保存槽,他们点击这些槽(WSprites)。从技术上讲,这是用户发起的,但flash无法知道这一点。3个保存槽将声音保存在内存中,只有当用户录制或加载时才会更改。当用户保存时,他们会被发送到php进行保存。

现在我确实尝试使用js作为中间人,但在这个过程中我最终丢失了字节,而我的js和php可能是我所有编程技能中最薄弱的领域。

我的问题是,有人知道在不设置沙箱的情况下将字节数组发送到php的方法吗。(最好没有js,但如果必须的话,我必须)

下面是php脚本:

<?php
    $default_path = 'images/';
    // check to see if a path was sent in from flash //
    $target_path = ($_POST['dir']) ? $_POST['dir'] : $default_path;
    if (!file_exists($target_path)) mkdir($target_path, 0777, true);
    // full path to the saved image including filename //
    $destination = $target_path . basename( $_FILES[ 'Filedata' ][ 'name' ] ); 

    // move the image into the specified directory //
    if (move_uploaded_file($_FILES[ 'Filedata' ][ 'tmp_name' ], $destination)) {
      echo "The file " . basename( $_FILES[ 'Filedata' ][ 'name' ] ) . " has been uploaded;";
    } else {
        echo "FILE UPLOAD FAILED";
    }
?>

下面是与之交互的as3方法:

public function save(slotNum:uint, byteArray:ByteArray, fileName:String, 
                     $destination:String = null, $script:String=null, 
                 parameters:Object = null):void
{
//trace("this happens"); //debug                
_curRecordSlot = slotNum; //set slot number
_recorder = _recordSlots[_curRecordSlot]; //set recorder to new slot
_saveFileName = "recording" + _curRecordSlot.toString() + ".mp3"; //set recording file name         
var i: int;
var bytes:String;
var postData:ByteArray = new ByteArray();
postData.endian = Endian.BIG_ENDIAN;
var ldr:URLLoader = new URLLoader(); //instantiate a url loader
ldr.dataFormat = URLLoaderDataFormat.BINARY; //set loader format
_request = new URLRequest(); //reinstantiate request
_request.url = $script; //set path to upload script
//add Filename to parameters
if (parameters == null) 
{
    parameters = new Object();
}
parameters.Filename = fileName;
//add parameters to postData
for (var name:String in parameters) 
{
    postData = BOUNDARY(postData);
    postData = LINEBREAK(postData);
    bytes = 'Content-Disposition: form-data; name="' + name + '"';
    for ( i = 0; i < bytes.length; i++ ) 
    {
        postData.writeByte( bytes.charCodeAt(i) );
    }
    postData = LINEBREAK(postData);
    postData = LINEBREAK(postData);
    postData.writeUTFBytes(parameters[name]);
    postData = LINEBREAK(postData);
}
//add img destination directory to postData if provided //
if ($destination)
{    
    postData = BOUNDARY(postData);
    postData = LINEBREAK(postData);
    bytes = 'Content-Disposition: form-data; name="dir"';
    for ( i = 0; i < bytes.length; i++ ) 
    {
        postData.writeByte( bytes.charCodeAt(i) );
    }
    postData = LINEBREAK(postData);
    postData = LINEBREAK(postData);
    postData.writeUTFBytes($destination);
    postData = LINEBREAK(postData);
}
//add Filedata to postData
postData = BOUNDARY(postData);
postData = LINEBREAK(postData);
bytes = 'Content-Disposition: form-data; name="Filedata"; filename="';
for ( i = 0; i < bytes.length; i++ ) 
{
    postData.writeByte( bytes.charCodeAt(i) );
}
postData.writeUTFBytes(fileName);
postData = QUOTATIONMARK(postData);
postData = LINEBREAK(postData);
bytes = 'Content-Type: application/octet-stream';
for ( i = 0; i < bytes.length; i++ ) 
{
    postData.writeByte( bytes.charCodeAt(i) );
}
postData = LINEBREAK(postData);
postData = LINEBREAK(postData);
postData.writeBytes(byteArray, 0, byteArray.length);
postData = LINEBREAK(postData);
//add upload file to postData
postData = LINEBREAK(postData);
postData = BOUNDARY(postData);
postData = LINEBREAK(postData);
bytes = 'Content-Disposition: form-data; name="Upload"';
for ( i = 0; i < bytes.length; i++ ) 
{
    postData.writeByte( bytes.charCodeAt(i) );
}
postData = LINEBREAK(postData);
postData = LINEBREAK(postData);
bytes = 'Submit Query';
for ( i = 0; i < bytes.length; i++ ) 
{
    postData.writeByte( bytes.charCodeAt(i) );
}
postData = LINEBREAK(postData);
//closing boundary
postData = BOUNDARY(postData);
postData = DOUBLEDASH(postData);        
//finally set up the urlrequest object //
_request.data = postData;
_request.contentType = 'multipart/form-data; boundary=' + _boundary;
_request.method = URLRequestMethod.POST;
_request.requestHeaders.push( new URLRequestHeader( 'Cache-Control', 'no-cache' ) );
//add listener to listen for completion
      ldr.addEventListener(Event.COMPLETE, onSaveComplete, false, 0, true); 
//add listener for io errors
      ldr.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
//add listener for security errors
      ldr.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError, false, 
                           0, true);
ldr.load(_request); //load the file 
}

上面的代码在flash播放器中运行良好,但会在浏览器中触发沙盒错误。

编辑:

根据要求,这里是我的嵌入代码(我用TITLEOFGAME替换了任何有游戏名称的地方):

<html lang="en">
<head>
<meta charset="utf-8"/>
<title>TITLEOFGAME</title>
<meta name="description" content="" />
<script src="js/swfobject.js"></script>
<script>
    var flashvars = {
    };
    var params = {
        menu: "false",
        scale: "noScale",
        allowFullscreen: "true",
        allowScriptAccess: "always",
        bgcolor: "",
        wmode: "direct" // can cause issues with FP settings & webcam
    };
    var attributes = {
        id:"TITLEOFGAME"
    };
    swfobject.embedSWF(
        "Hub.swf", 
        "altContent", "900", "506", "10.0.0", 
        "expressInstall.swf", 
        flashvars, params, attributes,
        {name:"TITLEOFGAME"}
    );
</script>       
<style>
    html, body { height:100%; overflow:hidden; }
    body { margin:0; }
</style>
</head>
<body>
<div id="altContent">
    <h1>TITLEOFGAME</h1>
    <p><a href="http://www.adobe.com/go/getflashplayer">Get Adobe Flash 
                player</a></p> //this line was just moved down for limitations text input for 
                               //this post
</div>
</body>
</html>

之前曾询问过此问题

似乎可能是内容类型或堆栈中丢失鼠标事件

显然,只有当URLLoader POST在Content-Disposition标头中包含"filename"属性时,才会发生这种情况
bytes = 'Content-Disposition: form-data; name="Filedata"; filename="';尝试base64编码并作为字符串发送以绕过它。

[编辑]
我的猜测是这里是违规代码。

bytes = 'Content-Disposition: form-data; name="Filedata"; filename="';
for ( i = 0; i < bytes.length; i++ ) 
{
    postData.writeByte( bytes.charCodeAt(i) );
}

当您使用表单数据和文件名进行内容处置时,它将触发安全错误
包含文件的表单数据只能通过堆栈中的用户交互(IE:鼠标单击)发送
话虽如此,但您需要删除Content-Disposition:表单数据;name="文件数据";filename="'并将其替换为字符串。
就我个人而言,我会采用这种方法。开发人员显然没有在生产环境中测试此代码

// disclaimer none of this code is tested as I pretty much just wrote it.
// however it should at least compile and you should be able to get a little idea of whats going on
// first create the endoder
var encoder:Base64Encoder = new Base64Encoder( )
// now encode the bytearray
    encoder.encodeBytes( byteArrayToEncode )
// get the encoded data as a string
var myByteArrayString:String = encoder.toString()
// lets verify the data should see a sting with base64 characters
trace( "show me the string->" + myByteArrayString )
// create the variables object that we want to POST to the server
var urlVars:URLVariables = new URLVariables();
// assign the base64 encoded bytearray to the POST variable of your choice here is use "data"
    urlVars.data = myByteArrayString;
// create the request object with the url you are sending the data to
var request:URLRequest = new URLRequest( 'Url of the PHP page below' ); 
// assign the POST data to the request
    request.data = urlVars
// just making sure POST method is being used
    request.method = URLRequestMethod.POST;
// here we make a loader even though it is a loader it can be used to send POST data along with the request to a page to load
var urlLoader:URLLoader = new URLLoader();
// just making sure the server knows we are sending data as a string
    urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
// create your call back functions of your choice
 //   urlLoader.addEventListener(Event.COMPLETE, recievedData );
 //   urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler );
 //   urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler );
// wrap the load in a try catch because we are special
try {
// load the request object we just created
  urlLoader.load( request );
} catch (e:Error) {
  trace(e);
}

跟踪应该输出类似这样的内容dG8gQ29udmVydA==,只是字符串末尾的等号是填充符,所以这应该会提示您数据是否正确转换。请注意字符串是如何全部为字母数字字符的
既然,你对byteArray的新了解,我建议你今晚在空闲时间在谷歌上搜索一下,试着了解它是什么以及它是如何工作的。

<?php
$decodedData= null;
if (!empty($_POST['data'])){
  // here is your data in pre-encoded format do what you want with it
  $decodedData= base64_decode( $_POST['data'] );
  file_put_contents("test.txt",$decodedData);
}
?>

您可以将记录的数据以字节数组的形式发送到服务器端应用程序,byte-aray将按照所需格式保存,如flv或任何其他文件(仅支持格式)。请检查AMFPHP、PHP和Byte数组是否使用PHP&ActionScript 3.0。你会得到它的例子。请检查此链接以重新编码和转换字节阵列中的音频:点击此处