开发者

Internal Microphone call

How do you call the microphone built into a computer to turn on when a user visits a site? I've heard that there a number of different ways to do so, but I'd like some advice on the best way.

To provide a meta-level view, I'm planning on having the mic pick up noise a开发者_开发知识库nd display it as a graphic equalizer (of sorts) but not record it.

Code is appreciated!


Here is an example of a Java applet that reads from a microphone.


For my flash microphone i use this class

import org.bytearray.micrecorder.MicRecorder;

import org.bytearray.micrecorder.encoder.WaveEncoder; import org.bytearray.micrecorder.events.RecordingEvent;

(just google it to get the code) and its as easy as calling this

            recorder = new MicRecorder(new WaveEncoder(),null,75,16);
    recorder.addEventListener(RecordingEvent.RECORDING, onRecording)
    recorder.addEventListener(Event.COMPLETE, onRecordComplete)

then you can do something like this to visualize the change in microphone noise. you of course have to make your own .fla movie clip that will display the "sound" in however you want to visualize it

public function onRecording(e:RecordingEvent)
{           
    var al:Number = recorder.microphone.activityLevel;
    TweenMax.to(soundMeter, .1, {scaleX:al * .01, onUpdate:onActivitylevelUpdate});//, onUpdateParams:[al]})
}       
public function onActivitylevelUpdate()
{
    xpos = speedX;
    ypos = centerY + Math.sin(angle) * amplitude
    angle += speedAngle;
    graphics.lineTo(xpos,ypos)
}


You want to look at the Microphone class to pick up sound off the mic, and at the computeSpectrum() method to calculate frequency values for the equalizer.

Here's an example of what you're trying to do, from the adobe samples:

package {
import flash.display.Sprite;
import flash.display.Graphics;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.text.TextField;

public class SoundMixer_computeSpectrumExample extends Sprite {

    public function SoundMixer_computeSpectrumExample() {
        var snd:Sound = new Sound();
        var req:URLRequest = new URLRequest("Song1.mp3");
        snd.load(req);

        var channel:SoundChannel;
        channel = snd.play();
        addEventListener(Event.ENTER_FRAME, onEnterFrame);
        channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
    }

    private function onEnterFrame(event:Event):void {
        var bytes:ByteArray = new ByteArray();
        const PLOT_HEIGHT:int = 200;
        const CHANNEL_LENGTH:int = 256;

        SoundMixer.computeSpectrum(bytes, false, 0);

        var g:Graphics = this.graphics;

        g.clear();

        g.lineStyle(0, 0x6600CC);
        g.beginFill(0x6600CC);
        g.moveTo(0, PLOT_HEIGHT);

        var n:Number = 0;

        for (var i:int = 0; i < CHANNEL_LENGTH; i++) {
            n = (bytes.readFloat() * PLOT_HEIGHT);
            g.lineTo(i * 2, PLOT_HEIGHT - n);
        }

        g.lineTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);
        g.endFill();

        g.lineStyle(0, 0xCC0066);
        g.beginFill(0xCC0066, 0.5);
        g.moveTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);

        for (i = CHANNEL_LENGTH; i > 0; i--) {
            n = (bytes.readFloat() * PLOT_HEIGHT);
            g.lineTo(i * 2, PLOT_HEIGHT - n);
        }

        g.lineTo(0, PLOT_HEIGHT);
        g.endFill();
    }

    private function onPlaybackComplete(event:Event):void {
        removeEventListener(Event.ENTER_FRAME, onEnterFrame);
    }
}}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜