admin 管理员组

文章数量: 1086019

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.

本文标签: playbackPlaying PCM data as sound in ActionScript 30 (Flash and AIR)Stack Overflow