最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

playback - Playing PCM data as sound in ActionScript 3.0 (Flash and AIR) - Stack Overflow

programmeradmin2浏览0评论

I'm trying to make an ABC/HR tool in Flash, but I can't manage to play the data as sound. Actually I prefer AS2, but I found that it is not enough for this purpose, so I decided to use AS3. But I can't play the ByteArray of the file user selects as sound (I need to play it as 16bit little-endian stereo PCM at 44100Hz).

Folder selection and subfolder (1-level) reading would be better and I remember that AIR can do this but I have troubles even running something I make with AIR so I want code for both Flash and AIR.

I want to indicate that compatibility with Ruffle is not important for me. This tool is aimed to run via projector and from local even if it's done as Flash.

I use Adobe Flash CS6 as the authoring tool.


I tried with this code:

import flash.FileReference
import flash.events.MouseEvent
import flash.events.Event
import flash.events.IOErrorEvent
import flash.events.ProgressEvent
import flash.utils.ByteArray
import flash.media.Sound
import flash.media.SoundChannel
import flash.events.SampleDataEvent

var snd:Sound = new Sound()
var sndchannel:SoundChannel

var fileref:FileReference = new FileReference()

browsebtn.addEventListener(MouseEvent.CLICK, function(){
    fileref.browse()
})

function playpcm(pcmdata:ByteArray):void {
    snd.addEventListener(SampleDataEvent.SAMPLE_DATA, onsample)
    pcmdata.position = 0
    sndchannel = snd.play()
    function onsample(event:SampleDataEvent):void {
        while (pcmdata.bytesAvailable > 0 && event.data.length < 8192) {
            var sample:Number = pcmdata.readFloat()
            event.data.writeFloat(sample)
            event.data.writeFloat(sample)
        }
    }
}

fileref.addEventListener(Event.SELECT, function(){fileref.load()})
fileref.addEventListener(Event.COMPLETE, function(){var fileData:ByteArray = fileref.data;/*trace(fileData);*/playpcm(fileData)})

It prompts for file selection but doesn't play the data. But when I trace the fileData I see that it gets it right (hence the comment-out-ed trace here).

I know that this code will play the WAV header too, it's just for testing.

发布评论

评论列表(0)

  1. 暂无评论