コード例 #1
0
ファイル: TestRun.cs プロジェクト: parta0207/rtmp-mediaplayer
        public void Run()
        {
            zPlayBuffer = new CircularBlockBuffer();
            zPlay = new ZPlay();
            netConnection = new NetConnection();
            netConnection.OnDisconnect += new NC_OnDisconnect(OnDisconnect);
            netConnection.OnTick += new NC_OnTick(NC_OnTick);
            try
            {
                int result = -1;
                // This is to connect to default vod app
                netConnection.Connect(new ServerLink("rtmp://localhost:1935/vod"), new NC_ResultCallBackConnect((sender, success) =>
                {
                    // Runs in RTMP thread (NOT MainThread!!!)
                    Console.WriteLine("NetConnection.Connect => Success=" + success.ToString());

                    if (success)
                    {
                        result = 1;
                    }
                    else
                    {
                        result = 0;
                    }
                }));

                // Wait until we are connected (needed because we run async)
                while (result == -1)
                {
                    Thread.Sleep(100);
                } //while


                // Succes for connecting to rtmp server
                if (result == 1)
                {
                    NetStream netStream = new NetStream(netConnection);
                    netStream.OnStatus += new NS_OnStatus(NS_OnStatus);
                    netStream.OnAudioPacket += new NC_OnMediaPacket(NC_OnMediaPacket);

                    netStream.WaitForValidStream_ID(4000); // wait max 4 seconds for the netstream to become valid (for real test connect to event)

                    // This is to get and MP3 stream
                    netStream.Play("Comfort_Fit_-_03_-_Sorry.mp3", 0, -1, true);
                }


                Console.WriteLine("Press enter to stop.");
                Console.ReadLine();
            }
            finally
            {
                // Cleanup
                if (netConnection != null)
                {
                    netConnection.Close();
                    netConnection = null;
                }
                if (zPlay != null)
                {
                    TStreamStatus status = new TStreamStatus();
                    zPlay.GetStatus(ref status);
                    if (status.fPlay)
                    {
                        zPlay.StopPlayback();
                    }

                    zPlay.Close();
                    zPlay = null;
                }
                if (zPlayBuffer != null)
                {
                    zPlayBuffer = null;
                }
            }
        }