本帖最后由 wgq341 于 2018-12-24 19:46 编辑
如题:声音不能跨域采样播放;
代码是根据以前一个网络上的SWF文件反编修改的
[Actionscript3] 纯文本查看 复制代码 //所有注释是我自己的理解,也不知道对否
var SAMPLE_SIZE:int = 8192;//声音样本的数目
var SOUND_RATE:int = 44100;//声音的频率44100
var sound:Sound;//声音对象
var sampler:Sound;//采集样本的声音对象
var soundChannel:SoundChannel = new SoundChannel();//声音通道对象
var startPos:uint = 0;//采样开始点
var seekpoint:uint;//记录采样的位置
var bufferTime:int = 3000;//缓冲区的时间(毫秒)
var bufferInterval:Number;//缓冲记时器
/********播放*****************/
//http://image.hnol.net/c/swf/2018-12/24/18/201812241810585961-2768212.swf是一个mp3声音地址
initPlay("http://image.hnol.net/c/swf/2018-12/24/18/201812241810585961-2768212.swf");
function initPlay(url:String):void
{
sound=new Sound();
sound.load(new URLRequest(url));
sampler=new Sound();
sampler.addEventListener(SampleDataEvent.SAMPLE_DATA,sampleDataHandler);
start();
}
/************取样***********************/
//好像这个方法不能用了(采集不到声音数据)
function sampleDataHandler(event:SampleDataEvent):void
{
var bytes:ByteArray = new ByteArray ;
var length:int = startPos + SAMPLE_SIZE > sampleTotal ? sampleTotal - startPos:SAMPLE_SIZE;
/*提取其声音数据:从bytes的0开始,读取length个字节放到byteArray里,byteArray从byteArray.position开始放. (bytes的position不变, byteArray的position增加)*/
sound.extract(bytes,length,startPos);
event.data.writeBytes(bytes);
startPos += length;
}
/*******sampler播放************/
function start():void
{
startPos = seekpoint;
if (soundChannel)
{
soundChannel.stop();
soundChannel.removeEventListener(Event.SOUND_COMPLETE,soundCompleteHandler);
}
soundChannel = sampler.play();
soundChannel.addEventListener(Event.SOUND_COMPLETE,soundCompleteHandler);
}
/*************播放完毕*************/
function soundCompleteHandler(event:Event):void
{
if (isComplete)
{
trace("*** complete ***")
initPlay("http://image.hnol.net/c/swf/2018-12/24/18/201812241810585961-2768212.swf");
}
else
{
if (buffering)
{
bufferInterval = setInterval(bufferUpdate,30);
}
else
{
start();
}
}
}
/*************缓冲************/
function bufferUpdate():void
{
if (! buffering)
{
clearInterval(bufferInterval);
start();
}
}
/*************采样的方法**********************/
/*采样的位置*/
function get samplePosition():Number
{
return soundChannel.position/1000*SOUND_RATE;
}
/*样本总数*/
function get sampleTotal():uint
{
return duration/1000*SOUND_RATE;
}
/*播放时间*/
function get playheadTime():uint
{
return Math.ceil(samplePosition+seekpoint)/SOUND_RATE*1000;
}
/*************总时间************/
function get duration():Number
{
return sound.length/(sound.bytesLoaded/sound.bytesTotal);
}
/*缓冲条件的布尔值*/
function get buffering():Boolean
{
var buffer:Boolean;
var cachedTime:int = duration - bufferTime;
if (sound.length <= cachedTime)
{
buffer = sound.length < playheadTime + bufferTime;
}
return buffer;
}
/*播放整个文件完毕布尔值*/
function get isComplete():Boolean
{
var complete:Boolean=Math.ceil(playheadTime/1000)>0&&Math.ceil(playheadTime/1000)>=Math.floor(duration/1000);
return complete;
}
|