mercredi 7 décembre 2016

AS3 Save And Record Audio With Microphone Not Working

So this has been bugging me for the last week and I'm so close to getting it to work but I can getting these slight errors and I'm at my wits end. I'm trying to make a microphone record and save in Flash with AS3 for Android. Anyone, please for the love of God, please help me before I throw my computer across the room.

import flash.media.Microphone;
import flash.display.Sprite;
import flash.events.SampleDataEvent;
import flash.utils.ByteArray;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.media.Sound;
import flash.filesystem.File;
import flash.filesystem.FileStream;
import flash.filesystem.FileMode;
import org.bytearray.micrecorder.encoder.WaveEncoder;

var wav:WaveEncoder=new WaveEncoder()
var file:FileReference=new FileReference()
var ch:SoundChannel;
var bytes:ByteArray = new ByteArray();
var DaMic:Microphone= Microphone.getMicrophone();
var level:Sprite = new Sprite;
var soundO:ByteArray = new ByteArray();
var sound:Sound= new Sound();
var recMode:Boolean=false;
var playMode:Boolean=false;


//Microphone
function init(){
    DaMic.gain = 60; 
    DaMic.rate = 11;  
    DaMic.setLoopBack(true); 
    DaMic.setSilenceLevel(5, 1000); 
    var sw:Number = stage.stageWidth / 2;
    var sh:Number = stage.stageHeight / 2;
} 
//Start Recording
DaMic.addEventListener(SampleDataEvent.SAMPLE_DATA,recording);
function startRecord():void;
{
DaMic.addEventListener(SampleDataEvent.SAMPLE_DATA, recording);
function init(){
    DaMic.gain = 60; 
    DaMic.rate = 11;  
    DaMic.setLoopBack(true); 
    DaMic.setSilenceLevel(5, 1000); 
    var sw:Number = stage.stageWidth / 2;
    var sh:Number = stage.stageHeight / 2;
{
//Stop Recording

DaMic.removeEventListener(SampleDataEvent.SAMPLE_DATA,recording);
function stopRecord():void;
{
    DaMic.removeEventListener(SampleDataEvent.SAMPLE_DATA, recording);
    bytes.position = 0;
    soundO.length=0;
    soundO.writeBytes(bytes);
    soundO.position = 0;
    bytes.length=0; 
}
//Save Audio

function recording(event:SampleDataEvent):void{
    while(event.data.bytesAvailable){
    var sample:Number = event.data.readFloat();
        bytes.writeFloat(sample);
}
//Play Back Audio

function playSound():void
{   
soundO.position = 0;
    sound.addEventListener(SampleDataEvent.SAMPLE_DATA, playback);
    ch=sound.play();
    ch.addEventListener(Event.SOUND_COMPLETE,onSC)  
}
function stopSound():void
{   
    sound.removeEventListener(SampleDataEvent.SAMPLE_DATA, playback);
    ch.stop()
    ch.removeEventListener(Event.SOUND_COMPLETE,onSC)   
}
//triggered once the sound finishes playing
function onSC(evt:Event):void
{
    trace("SOUND_COMPLETE")
    stopSound()
    soundO.position=0
    playMode=!true
        play_lab.gotoAndStop(1)
        play_mesg.gotoAndStop(1)
        rec_btn.visible=rec_lab.visible=!false
        save_btn.visible=save_lab.visible=true
}
function playback(event:SampleDataEvent):void{
    var sample:Number;
    for(var i:int = 0; i<8192;i++){
        if (bytes.bytesAvailable>0){
        sample = bytes.readFloat();
        event.data.writeFloat(sample); //Channel Left
        event.data.writeFloat(sample); //Channel Right
}

//WAV Files

function saveWav(bytesArray):void{
    var directory:File = File.documentsDirectory.resolvePath("mySounds");
    if(!directory.exists) {
        directory.createDirectory();
    }


//Dynamic Buttons
rec_lab.mouseEnabled=play_lab.mouseEnabled=save_lab.mouseEnabled=false
rec_btn.buttonMode=play_btn.buttonMode=save_btn.buttonMode=true

//Visibility

function showPlayUI(){
    play_btn.visible=play_lab.visible=save_btn.visible=save_lab.visible=true;
}

function hidePlayUI(){
    play_btn.visible=play_lab.visible=save_btn.visible=save_lab.visible=!true;
}

rec_btn.addEventListener(MouseEvent.CLICK,onRecord)
play_btn.addEventListener(MouseEvent.CLICK,onPlay)
save_btn.addEventListener(MouseEvent.CLICK,onSave)

//Triggers
function onSave(evt:MouseEvent=null):void
{
    hidePlayUI()
    soundO.position=0
    var o:ByteArray=wav.encode(soundO,1)
    file.save(o,"sample.wav")
    showPlayUI()
}

function onRecord(evt:MouseEvent=null):void
{
    if(!recMode){
        recMode=true
        rec_lab.gotoAndStop(2)
        rec_mesg.gotoAndPlay(2)
        startRecord()
    }else{
        recMode=!true
        rec_lab.gotoAndStop(1)
        rec_mesg.gotoAndStop(1)
        stopRecord()
        showPlayUI()
    }
}
//triggered when play button is clicked
function onPlay(evt:MouseEvent=null):void
{
    if(!playMode){
        playMode=true
        play_lab.gotoAndStop(2)
        play_mesg.gotoAndPlay(2)
        rec_btn.visible=rec_lab.visible=false
        save_btn.visible=save_lab.visible=!true
        playSound()
    }else{
        playMode=!true
        play_lab.gotoAndStop(1)
        play_mesg.gotoAndStop(1)
        rec_btn.visible=rec_lab.visible=!false
        save_btn.visible=save_lab.visible=true
        stopSound()
        }
    }
    }
}
}
}

init()
hideUI()

Aucun commentaire:

Enregistrer un commentaire