How to convert byte array to mp3 using aspx script

2019-08-17 15:40发布

问题:

In Flash as3.0 recorded voice is stored in a BytesArray variable. Now need to store the recorded sound as .mp3 or .wav file in aspx script. Any can help me? How to convert byte array to audio format and save in server any concept please share.

回答1:

refer a following my code. this code is skills about after recording your voice to be converted to wav or mp3 to save. converted output mp3 and wav file is is guaranteed about socket communication with the C and Java. I tested in Windows-xp(32bit) Windows7(64bit), MacOSX, Android.

Framework used here is the link.

WAVWriter: WAVWriter

ShineMP3Encoder: ShineMP3Encoder

import com.adobe.audio.format.WAVWriter;
import fr.kikko.lab.ShineMP3Encoder;
import flash.events.SampleDataEvent;
import flash.media.Microphone;
import flash.media.Sound;
import flash.utils.ByteArray;
import flash.events.Event;

var mp3encoder:ShineMP3Encoder;
var microphone:Microphone;
var isRecording:Boolean=false;
var soundRecording:ByteArray;

function startMicRecording():void 
{
    isRecording=true;
    soundRecording = new ByteArray();
    microphone=Microphone.getMicrophone();
    microphone.rate=44;
    microphone.addEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);
}

function stopMicRecording():void {

    isRecording=false;
    microphone.removeEventListener(SampleDataEvent.SAMPLE_DATA, gotMicData);
    soundRecording.position=0;

    convert2MP3();
}


function gotMicData(micData:SampleDataEvent):void 
{
    soundRecording.writeBytes(micData.data);
}

function convert2MP3():void
{
    var wavWrite:WAVWriter = new WAVWriter();
    wavWrite.numOfChannels=1;
    wavWrite.sampleBitRate=16;
    wavWrite.samplingRate=44100;

    var wav:ByteArray = new ByteArray();

    wavWrite.processSamples(wav, soundRecording, 44100,1);
    wav.position=0;

    /* 
     If the process is compressed into mp3 if you have big problems, 
     just as .wav format save. following code: 
     var file:FileReference = new FileReference();
     file.save(wav,"your_file_name.wav");
    */

    mp3encoder=new ShineMP3Encoder(wav);
    mp3encoder.addEventListener(Event.COMPLETE, onEncoded);
    mp3encoder.start();
}

function onEncoded(e:Event):void 
{
    mp3encoder.mp3Data.position=0;
    mp3encoder.saveAs("your_file_name.mp3");
}

startMicRecording();