public VlcException() : base() { IntPtr errorPointer = LibVlc.libvlc_errmsg(); _err = errorPointer == IntPtr.Zero ? "VLC Exception" : Marshal.PtrToStringAuto(errorPointer); }
private void TryDispose() { if (playerState == 1) { TryStop(); } LibVlc.libvlc_release(VlcInstanceHandle); tryDisposeSemaphore.Release(); }
private void TryPlay() { using (VlcMedia media = new VlcMedia(VlcInstanceHandle, mLocation)) { VlcMediaPlayerHandle = LibVlc.libvlc_media_player_new_from_media(media.Handle); if (VlcMediaPlayerHandle == IntPtr.Zero) { throw new VlcException(); } } LibVlc.libvlc_media_player_set_hwnd(VlcMediaPlayerHandle, mDrawHandle); LibVlc.libvlc_media_player_play(VlcMediaPlayerHandle); }
public VlcMedia(IntPtr VlcInstanceHandle, string location) { byte[] retArray = Encoding.UTF8.GetBytes(location); byte[] retArrayZ = new byte[retArray.Length + 1]; Array.Copy(retArray, retArrayZ, retArray.Length); retArrayZ[retArrayZ.Length - 1] = 0; IntPtr url = Marshal.AllocHGlobal(retArrayZ.Length); Marshal.Copy(retArrayZ, 0, url, retArrayZ.Length); Handle = LibVlc.libvlc_media_new_location(VlcInstanceHandle, url); if (Handle == IntPtr.Zero) { throw new VlcException(); } }
private void TryStop() { LibVlc.libvlc_media_player_stop(VlcMediaPlayerHandle); LibVlc.libvlc_media_player_release(VlcMediaPlayerHandle); }
public SimpleRtspPlayer(string location, IntPtr drawHandle) { mLocation = location; mDrawHandle = drawHandle; VlcInstanceHandle = LibVlc.libvlc_new(args.Length, args); if (VlcInstanceHandle == IntPtr.Zero) { throw new VlcException(); } Thread tt = new Thread(() => { int vlcState; int command; while (true) { command = checkCommand(); switch (command) { //没有命令 case -1: if (playerState == 1) { //IDLE/CLOSE=0, OPENING=1, BUFFERING=2, PLAYING=3, PAUSED=4, STOPPING=5, ENDED=6, ERROR=7 vlcState = LibVlc.libvlc_media_player_get_state(VlcMediaPlayerHandle); if (vlcState == 7 || vlcState == 6) //检测到错误,重启并暂停0.5秒 { //停止播放 TryStop(); TryPlay(); Thread.Sleep(500); } else //没有检测到错误,暂停0.1秒 { Thread.Sleep(100); } } else //入口:没有命令且stop状态 { tmpSemaphore.Wait(); } break; case 0: //停止播放命令 if (playerState == 1) { TryStop(); playerState = 0; //第一次wait消耗信号 tmpSemaphore.Wait(); //第二次wait等待信号 tmpSemaphore.Wait(); } else { //此次的信号由case 0:playerState == 1消耗 //但是在停止状态,所以继续wait tmpSemaphore.Wait(); } break; case 1: //开始播放命令 if (playerState != 1) { //此次的信号由case 0:playerState == 1消耗 TryPlay(); playerState = 1; } else { //只需要消耗一个信号 tmpSemaphore.Wait(); } break; //垃圾回收命令,不是break,而是return。 case 2: //消耗一个信号 if (playerState == 1) { tmpSemaphore.Wait(); } TryDispose(); return; } } }); tt.IsBackground = true; tt.Start(); }
public void Dispose() { LibVlc.libvlc_media_release(Handle); }