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

as3/php mp3字节数组的传输和写入,php,actionscript-3,file,mp3,bytearray,Php,Actionscript 3,File,Mp3,Bytearray,一段时间以来,我一直在讨论一个问题 我正在制作一个儿童游戏(flash as3),其中的故事、诗歌、歌曲等都会读给孩子听。有一个语音记录在读/唱课文,课文在说/唱时突出显示。孩子可以独立地打开和关闭语音和单词突出显示。孩子也可以选择自己录音。现在说录音很好。我可以从麦克风中捕捉到它,孩子可以播放它,没有问题 问题在于客户端希望孩子能够将此录制保存到web服务器 我想我已经通过使用URLRequest和URLLoader对象解决了这个问题。mp3显示在Web服务器上的指定文件夹中。我用一个媒体播放

一段时间以来,我一直在讨论一个问题

我正在制作一个儿童游戏(flash as3),其中的故事、诗歌、歌曲等都会读给孩子听。有一个语音记录在读/唱课文,课文在说/唱时突出显示。孩子可以独立地打开和关闭语音和单词突出显示。孩子也可以选择自己录音。现在说录音很好。我可以从麦克风中捕捉到它,孩子可以播放它,没有问题

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

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

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

孩子们被提供了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 
}
公共函数保存(slotNum:uint,byteArray:byteArray,fileName:String,
$destination:String=null,$script:String=null,
参数:Object=null):void
{
//跟踪(“发生这种情况”);//调试
_curRecordSlot=slotNum;//设置插槽号
_记录器=_recordslot[_curRecordSlot];//将记录器设置为新插槽
_saveFileName=“recording”+\u curRecordSlot.toString()+“.mp3”//设置录制文件名
变量i:int;
变量字节:字符串;
var postData:ByteArray=newbytearray();
postData.endian=endian.BIG_endian;
var ldr:URLLoader=new URLLoader();//实例化一个url加载器
ldr.dataFormat=URLLoaderDataFormat.BINARY;//设置加载程序格式
_request=new URLRequest();//重新实例化请求
_request.url=$script;//设置上载脚本的路径
//将文件名添加到参数
if(参数==null)
{
参数=新对象();
}
parameters.Filename=文件名;
//向postData添加参数
for(变量名称:参数中的字符串)
{
postData=边界(postData);
postData=换行符(postData);
字节='内容处置:表单数据;名称='+名称+'';
对于(i=0;i
上面的代码在flash播放器中非常有效,但会在浏览器中触发沙盒错误

编辑:

根据要求,这里是我的嵌入代码(我将游戏名称替换为TITLEOFGAME):


名衔
var flashvars={
};
var para
<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>
bytes = 'Content-Disposition: form-data; name="Filedata"; filename="';
for ( i = 0; i < bytes.length; i++ ) 
{
    postData.writeByte( bytes.charCodeAt(i) );
}
// 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);
}
<?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);

}

?>