using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using fis.media.Library.Media.Players; using fis.media.Library.Media.Rooms; using fis.media.Library.Players; using fis.media.ThirdPartLibrary.Daniu.Player; using fis.media.ThirdPartLibrary.Tencent; namespace fis.media.Managers { public class PlayerManager:IDisposable { private static PlayerManager? _instance; private SmartPlayer? _smartPlayer { get; set; } public EventHandler? VideoFrameReceived; private RoomTypeEnum _roomType; private string _rtmpPullUrl; private PlayerManager() { } public static PlayerManager Instance => _instance ?? (_instance = new PlayerManager()); /// /// 注册SmartPlayerSDK /// public static void RegistSmartPlayerSDK() { SmartPlayer.RegistSmartPlayerSDK(); } /// /// 开始RTMP拉流 /// /// private void StartRTMP() { if (_smartPlayer == null) { _smartPlayer = new SmartPlayer(); //_smartPlayer.VideoFrameReceived += VideoFrameReceived; } _smartPlayer.Play(_rtmpPullUrl); } /// /// 开始TRTC拉流 /// /// private void StartTRTC() { TRTCChatRoom.Instance.RemoteVideoFrameArrived += OnRemoteVideoFrameArrived; TRTCChatRoom.Instance.RemoteVideoScreenFrameArrived += OnRemoteVideoScreenFrameArrived; } public void Start(string[] args) { if (SetRoomPara(args)) { if (_roomType == RoomTypeEnum.TRTCRoom) { StartTRTC(); } if (_roomType == RoomTypeEnum.RTMPVirtualRoom) { StartRTMP(); } } } private bool SetRoomPara(string[] args) { if (args == null || args.Length < 1) { return false; } _roomType = (RoomTypeEnum)Enum.Parse(typeof(RoomTypeEnum), args[0], true); if (_roomType == RoomTypeEnum.RTMPVirtualRoom && args.Length == 2) { var url = args[1]; _rtmpPullUrl = url; } return true; } private void OnRemoteVideoScreenFrameArrived(object? sender, RemoteVideoFrameData e) { throw new NotImplementedException(); } private void OnRemoteVideoFrameArrived(object? sender, RemoteVideoFrameData e) { VideoFrameReceived?.Invoke(this,e); } /// /// 停止拉流 /// public void Stop() { _smartPlayer?.Stop(); TRTCChatRoom.Instance.RemoteVideoFrameArrived -= OnRemoteVideoFrameArrived; TRTCChatRoom.Instance.RemoteVideoScreenFrameArrived -= OnRemoteVideoScreenFrameArrived; } /// /// 切换拉流地址 /// /// public void SwitchUrl(string url) { _smartPlayer?.SwitchUrl(url); } /// /// 设置录制参数 /// /// /// /// /// public void SetRecordParameter(string recordingDirectory, int maxFileSize, string filePrefix, bool attachDateTime) { _smartPlayer?.SetRecordParameter(recordingDirectory, maxFileSize, filePrefix, attachDateTime); } /// /// 开始录制 /// public void StartRecord() { _smartPlayer?.StartRecord(); } /// /// 停止录制 /// public void StopRecord() { _smartPlayer?.StopRecord(); } /// /// 设置静音 /// /// public void SetMute(bool isMute) { _smartPlayer?.SetMute(isMute); } public void Dispose() { Stop(); if (_smartPlayer != null) { //_smartPlayer.VideoFrameReceived -= VideoFrameReceived; } } } }