private void DisposePlayer() { if (_mediaPlayer != null) { InvokeOnPlayerThread(() => { _mediaPlayer.Dispose(); _mediaPlayer = null; //force the object to get cleaned up }); } }
private void PlayTrack(string path, long startPositionTicks, bool isVideo, BaseItemDto item, MediaSourceInfo mediaSource, string forcedVideoRenderer) { var playableItem = new PlayableItem { MediaSource = mediaSource, PlayablePath = path, OriginalItem = item }; try { InvokeOnPlayerThread(() => { //create a fresh DS Player everytime we want one DisposePlayer(); _mediaPlayer = new DirectShowPlayer(this, _hostForm, _logger, GetConfiguration(), _httpClient); _mediaPlayer.Play(playableItem, forcedVideoRenderer); }, true); } catch { OnPlaybackStopped(playableItem, null, TrackCompletionReason.Failure, null); throw; } if (startPositionTicks > 0) { InvokeOnPlayerThread(() => _mediaPlayer.Seek(startPositionTicks)); } if (playableItem.OriginalItem.IsVideo) { var audioIndex = playableItem.MediaSource.DefaultAudioStreamIndex; var subtitleIndex = playableItem.MediaSource.DefaultSubtitleStreamIndex; if (audioIndex.HasValue && audioIndex.Value != -1) { SetAudioStreamIndex(audioIndex.Value); } SetSubtitleStreamIndex(subtitleIndex ?? -1); } }