基本FMS连接AS3

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基本FMS连接AS3相关的知识,希望对你有一定的参考价值。

This is the standard code needed when connecting to a media server.
  1. package {
  2.  
  3. import flash.display.Sprite;
  4. import flash.net.NetConnection;
  5. import flash.events.Event;
  6. import flash.events.NetStatusEvent;
  7. import flash.events.IOErrorEvent;
  8. import flash.events.SecurityErrorEvent;
  9.  
  10.  
  11. public class FMSConnection extends Sprite {
  12. public static const CONNECTED:String = 'connected';
  13. private var nc:NetConnection;
  14.  
  15. public function FMSConnection() {
  16. // constructor code
  17. connect('rtmp://YOUR-SERVERSIDE-APP', 'USERNAME');
  18. }
  19.  
  20. public function connect(url:String, username:String):void
  21. {
  22. // Create new net connection
  23. nc = new NetConnection();
  24. // Set this instance/class to receive callback functions.
  25. nc.client = this;
  26. // Call the connect method on the FMS and pass in the username.
  27. nc.connect(url, username);
  28.  
  29. nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
  30. nc.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
  31. nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
  32. }
  33.  
  34. private function onNetStatus(event:NetStatusEvent):void
  35. {
  36. if(event.info.code == "NetConnection.Connect.Success")
  37. {
  38. trace("Connected To server");
  39. // Dispatch event letting the application know it connected.
  40. // Do Stuff here!
  41. dispatchEvent(new Event(FMSConnection.CONNECTED, true));
  42. }else{
  43. throw new Error("Failed to connect to server!");
  44. }
  45. }
  46.  
  47. private function onIoError(event:IOErrorEvent):void
  48. {
  49. throw new Error("IO Error on connection.");
  50. }
  51.  
  52. private function onSecurityError(event:SecurityErrorEvent):void
  53. {
  54. throw new Error("Security Error on connection.");
  55. }
  56. }
  57. /////////////////////////////////////////
  58. /* Brian Shantz -- 2010 */
  59. /* http://www.brianshantz.com */
  60. /* Flash Media Server Connection Script */
  61. /////////////////////////////////////////
  62. }

以上是关于基本FMS连接AS3的主要内容,如果未能解决你的问题,请参考以下文章

FME突然无法连接FMS服务器

使用带有 RTMP 的 Flash Media Server (FMS) 进行实时视频聊天

使用 Flash 媒体服务器在 as3 中进行群聊?

FMS 服务器端代码未执行

NetStream 从 FMS 中的流中加载 ID3 信息

as3 air无法连接远程服务器