microphone.lzx
<library>
<include href="mediadevice.lzx"/>
<class name="microphone" extends="mediadevice">
<passthrough when="$as3">
import flash.media.*;
</passthrough>
<attribute name="_mc" value="null
"/>
<attribute name="_sound" value="null
"/>
<attribute name="level" type="number" value="0
"/>
<event name="onlevel"/>
<attribute name="silencelevel" type="number" value="10
"/>
<event name="onsilencelevel"/>
<setter name="silencelevel" args="level">
//Debug.write("microphone set silencelevel", this, level, this.isinited, this._dev, this._mc);
if (level === this.silencelevel) {
return;
}
this.silencelevel = level;
if (this._dev != null) {
this._dev.setSilenceLevel(this.silencelevel, this.silencetimeout);
}
if (this.onsilencelevel.ready) {
this.onsilencelevel.sendEvent(level);
}
</setter>
<attribute name="silencetimeout" type="number" value="-1
"/>
<event name="onsilencetimeout"/>
<setter name="silencetimeout" args="timeout">
//Debug.write("microphone set silencetimeout", this, timeout, this.isinited, this._dev, this._mc);
if (timeout === this.silencetimeout) {
return;
}
this.silencetimeout = timeout;
if (this._dev != null) {
this._dev.setSilenceLevel(this.silencelevel, this.silencetimeout);
}
if (this.onsilencetimeout.ready) {
this.onsilencetimeout.sendEvent(timeout);
}
</setter>
<attribute name="codec" type="string" value="NellyMoser
"/>
<attribute name="encodequality" type="number" value="6
"/>
<attribute name="framesperpacket" type="number" value="6
"/>
<attribute name="rate" type="number" value="8
"/>
<attribute name="gain" type="number" value="50
"/>
<event name="ongain"/>
<setter name="gain" args="gain">
//Debug.write("microphone set gain", this, gain, this.isinited, this._dev, this._mc);
if (gain === this.gain) {
return;
}
this.gain = gain;
if (this._dev != null) {
this._dev.gain = gain;
}
if (this.ongain.ready) {
this.ongain.sendEvent(gain);
}
</setter>
<attribute name="echosuppression" type="boolean" value="true
"/>
<event name="onechosuppression"/>
<setter name="echosuppression" args="echosuppression">
//Debug.write("microphone set echosuppression", this, echosuppression, this.isinited, this._dev, this._mc);
if (echosuppression === this.echosuppression) {
return;
}
this.echosuppression = echosuppression;
if (this._dev != null) {
this._dev.setUseEchoSuppression(echosuppression);
}
if (this.onechosuppression.ready) {
this.onechosuppression.sendEvent(echosuppression);
}
</setter>
<attribute name="_leveldel" value="$once{new LzDelegate(this, '_updateLevel')}"/>
<attribute name="stream" value="null
"/>
<event name="onstream"/>
<setter name="stream" args="stream">
//Debug.write("microphone set stream", stream);
this.stream = stream;
if (stream) {
//Debug.write("microphone", this, "set stream", stream);
stream.setAttribute('mic', this);
}
</setter>
<method name="_makeDevice">
var deviceindex = this['deviceindex'];
var dev = null;
if ($as3) {
if (deviceindex == null) {
dev = flash.media.Microphone.getMicrophone();
if (dev == null) {
if ($debug) {
if (Microphone.names.length) {
Debug.warn('The default microphone is in use.');
} else {
Debug.warn('No microphones are available.');
}
}
return;
}
} else {
dev = flash.media.Microphone.getMicrophone(deviceindex);
if (dev == null) {
if ($debug) {
Debug.warn('The microphone at device index %w is in use or there are no microphones on this system. %w', deviceindex, Microphone.names);
}
return;
}
}
// This is done to supress pesky feedback.
dev.soundTransform = this._sound;
// apply defaults
dev.codec = this.codec;
dev.encodeQuality = this.encodequality;
dev.framesPerPacket = this.framesperpacket;
dev.gain = this.gain;
dev.rate = this.rate;
dev.setUseEchoSuppression(this.echosuppression)
dev.setSilenceLevel(this.silencelevel, this.silencetimeout);
} else {
if (deviceindex == null) {
dev = Microphone.get();
} else {
dev = Microphone.get(deviceindex);
}
}
this._dev = dev;
return dev;
</method>
<method name="_setupDevice">
var dev = this._dev;
// ...
</method>
<method name="startDevice">
//Debug.write("microphone startdevice", this);
// This is done to supress pesky feedback.
if ($as3) {
this._sound = new SoundTransform(0, 0);
} else {
this._mc =
_root.createEmptyMovieClip("__LZmicMovieClip", 10000);
this._sound =
new Sound(this._mc);
this._sound.setVolume(0);
}
super.startDevice();
// check for the existence of immediateparent.videoview
if (lz['videoview'] && immediateparent instanceof lz.videoview) {
immediateparent.setAttribute('mic', this);
}
//Debug.write("microphone startDevice setting capturing", this.capturing);
this.setAttribute("capturing", this.capturing);
</method>
<handler name="onactive">
//Debug.write("microphone onactive", this, "onlevel", this.onlevel, "active", this.active);
if (this.onlevel.ready) {
if (this.active && this.capturing) {
this._leveldel.register(lz.Idle, "onidle");
} else {
this._leveldel.unregisterAll();
this.setAttribute("level", 0);
}
}
</handler>
<method name="_updateAllowed">
//Debug.write("microphone _updateAllowed", this, this.allowed, "capturing", this.capturing);
super._updateAllowed();
this.setAttribute("capturing", this.capturing);
</method>
<setter name="capturing" args="capturing">
//Debug.write("microphone _setCapturing", this, capturing, this.isinited, this._dev, this._mc);
super.setAttribute('capturing', capturing);
if (!this.isinited) {
return;
}
if (this._dev != null) {
if ($as3) {
if (capturing) {
this._dev.setLoopBack(true);
} else {
this._dev.setLoopBack(false);
}
} else {
if (capturing) {
this._mc.attachAudio(this._dev);
} else {
this._mc.attachAudio(false);
}
}
this.setAttribute("active", this.active);
}
</setter>
<method name="_updateLevel" args="ignore = null">
var level = this._dev.activityLevel;
if (level < 0) {
level = 0;
}
if (level != this.level) {
this.setAttribute("level", level);
}
</method>
<doc>
<tag name="shortdesc">
<text>A microphone connection to a media server.</text>
</tag>
<text>
<p>The <class><microphone></class> element allows you to set up and control a microphone that is connected to a media server. For privacy protection, the <attribute>allowed</attribute> attribute must be set to <attribute>true</attribute> by the user.</p>
<p>The following figure shows a microphone control.</p>
<programlisting>
<canvas>
<microphone id="m"
capturing="${controlpanel.capturing.value}"
/>
<view
x="10"
y="20"
width="102"
height="12"
bgcolor="black"
>
<view
x="1"
y="1"
bgcolor="yellow"
width="${m.level}"
height="10"
/>
</view>
<view
y="100"
>
<simplelayout
axis="x"
inset="10"
spacing="20"
/>
<view
layout="axis: y; spacing: 4"
>
<text
text="${'deviceindex: ' + m.deviceindex}"
resize="true"
/>
<text
text="${'devicename: ' + m.devicename}"
resize="true"
/>
<text
text="${'capturing: ' + m.capturing}"
resize="true"
/>
<text
text="${'allowed: ' + m.allowed}"
resize="true"
/>
<text
text="${'level: ' + m.level}"
resize="true"
/>
</view>
<view
width="1"
height="100"
bgcolor="black"
/>
<view id="controlpanel"
layout="axis: y; spacing: 10"
>
<checkbox name="capturing"
text="Capturing"
value="false"
/>
<button
text="Show Settings"
onclick="m.showSettings()"
/>
</view>
</view>
</canvas>
</programlisting>
</text>
</doc>
</class>
</library>
Cross References
Includes
Classes