Getting started in Flash CS4 Pro
I need something very simple as a Flash application.
Just a single button that will call an action script. That action script will call
private var m_MicCnx:NetConnection; //connection over the server
private var m_MicStream:NetStream; //Audio Output
private var m_Microphone:Microphone; //micro, attach with m_MicStream's audio content
m_MicCnx =new NetConnection();
m_MicCnx.client = this;
m_MicCnx.addEventListener(NetStatusEvent.NET_STATUS,HandlerMicCnxStatus);
m_MicCnx.connect(m_strMicUrl);
m_MicStream.publish ("mp3:myStream", "live");
m_Microphone=Microphone.getMicrophone();
m_Microphone.gain=85;
m_Microphone.rate=11;
m_Microphone.setSilenceLevel(15,2000);
private function HandlerMicCnxStatus(e:NetStatusEvent):void
{
var isConnected:Boolean; //tells whether the connection has succeded
isConnected=e.info.code=="NetConnection.Connect.Success";
if (isConnected)
{
m_MicStream=new NetStream(m_MicCnx);
m_MicStream.attachAudio (m_Microphone);
}
}
I think this is the most simple setup I can get that will publish a microphone to a RTMP stream. This is an attempt to shave down a larger product. This demo will help a third party create a RTMP Stream Reader for mp3.
My question is : How do I move from this snippet of code to a flash application? Most tutorials I've found so far were either about using menus or creating very开发者_JS百科 complex animations. I just need a single button that will call this, or maybe an auto-load.
Where can I get a straight-forward tutorial? I understand there will be a boatload of clicking everywhere, but if you could explain it here it would be awesome.
I'll explain as if it your first time working with flash.
Like any other language you have some sort of bootstrap file and a main function. In Flash you have something called a document class. The document class gets called when the application is started. Read more about that here.
To use a document class you specify it in the properties of your .fla file, like so:
This tells flash that it should use the class Main
in the file Main.as
as the document class.
Here's what the document class looks like that I am using in the example:
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
trace("Application started!");
}
}
}
The Main
class gets instantiated and the constructor is called, and thus tracing "Application started" to the console. The document must inherit a class which is a subclass of DisplayObject
. If you want to be able to use frame operations use MovieClip
and if you are not going to use any frame operations in your document class use Sprite
.
Next step is to move your code in to an separate class that you can call from your document class. So you will get a structure of your project like this:
Your MicrophoneRTMP.as
will look something like this:
package{
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.media.Microphone;
import flash.events.NetStatusEvent;
public class MicrophoneRTMP{
private var m_MicCnx:NetConnection;
private var m_MicStream:NetStream;
private var m_Microphone:Microphone;
public function MicrophoneRTMP(){
this.setupMicrophone();
this.setupConnection();
}
private function setupMicrophone():void{
m_Microphone = Microphone.getMicrophone();
m_Microphone.gain = 85;
m_Microphone.rate = 11;
m_Microphone.setSilenceLevel(15,2000);
}
private function setupConnection():void{
m_MicCnx = new NetConnection();
m_MicCnx.client = this;
m_MicCnx.addEventListener(NetStatusEvent.NET_STATUS,HandlerMicCnxStatus);
m_MicCnx.connect(m_strMicUrl);
m_MicStream.publish("mp3:myStream", "live");
}
private function HandlerMicCnxStatus(e:NetStatusEvent):void{
var isConnected:Boolean;
isConnected = e.info.code == "NetConnection.Connect.Success";
if(isConnected){
m_MicStream = new NetStream(m_MicCnx);
m_MicStream.attachAudio(m_Microphone);
trace("Microphone stream successful");
}else{
trace("Microphone stream unsuccessful");
}
}
}
}
Then you just need to alter your Main.as
to import and call your class:
package{
import flash.display.Sprite;
import MicrophoneRTMP;
public class Main extends Sprite{
public function Main(){
myButton.addEventListener(MouseEvent.CLICK, myButtonClicked); //myButton is a button put on the stage in Flash
}
private function myButtonClicked(event:MouseEvent):void{
trace("My button was clicked");
var microphoneRTMP:MicrophoneRTMP = new MicrophoneRTMP();
}
}
}
Note:
Now you will be able to compile your project and start debugging your MicrophoneRTMP
class, it contains some errors you need to look over.
Concept in my example you can read more about:
- NetConnection reference
- NetStream reference
- Microphone reference
- Basic event handling in as3
精彩评论