protected int GetTotalNumberOfAudioStreams()
        {
            int numberOfAudioStreams = 0;
            IPlayerContextManager playerContextManager = ServiceRegistration.Get <IPlayerContextManager>();

            for (int i = 0; i < playerContextManager.NumActivePlayerContexts; i++)
            {
                IPlayerContext pc = playerContextManager.GetPlayerContext(i);
                if (pc == null || !pc.IsActive)
                {
                    continue;
                }
                AudioStreamDescriptor currentAudioStream;
                numberOfAudioStreams += pc.GetAudioStreamDescriptors(out currentAudioStream).Count;
            }
            return(numberOfAudioStreams);
        }
        public ICollection <AudioStreamDescriptor> GetAvailableAudioStreams(out AudioStreamDescriptor currentAudioStream)
        {
            currentAudioStream = null;
            IPlayerManager playerManager = ServiceRegistration.Get <IPlayerManager>();
            ICollection <AudioStreamDescriptor> result = new List <AudioStreamDescriptor>();
            int audioSlotIndex = playerManager.AudioSlotIndex;

            for (int i = 0; i < 2; i++)
            {
                IPlayerContext playerContext = GetPlayerContext(i);
                if (playerContext == null)
                {
                    continue;
                }
                AudioStreamDescriptor current;
                CollectionUtils.AddAll(result, playerContext.GetAudioStreamDescriptors(out current));
                if (audioSlotIndex == i)
                {
                    currentAudioStream = current;
                }
            }
            return(result);
        }
        protected void UpdatePlayerSlotAudioMenu()
        {
            // Some updates could be avoided if we tracked a "dirty" flag and break execution if !dirty
            lock (_syncObj)
            {
                IPlayerManager        playerManager        = ServiceRegistration.Get <IPlayerManager>();
                IPlayerContextManager playerContextManager = ServiceRegistration.Get <IPlayerContextManager>();

                _playerSlotAudioMenu.Clear();
                IPlayerContext pc = _playerAudioMenuPlayerContext;
                if (pc == null || !pc.IsActive)
                {
                    LeaveAudioMenuWorkflow();
                    return;
                }
                IPlayer player = pc.CurrentPlayer;
                AudioStreamDescriptor         currentAudioStream;
                IList <AudioStreamDescriptor> asds = new List <AudioStreamDescriptor>(pc.GetAudioStreamDescriptors(out currentAudioStream));
                foreach (AudioStreamDescriptor asd in asds)
                {
                    string playedItem = player == null ? null : player.MediaItemTitle ?? pc.Name;
                    string choiceItemName;
                    if (asds.Count > 1)
                    {
                        // Only display the audio stream name if the player has more than one audio stream
                        choiceItemName = playedItem + ": " + asd.AudioStreamName;
                    }
                    else
                    {
                        choiceItemName = playedItem;
                    }
                    AudioStreamDescriptor asdClosureCopy = asd;
                    ListItem item = new ListItem(Consts.KEY_NAME, choiceItemName)
                    {
                        Command  = new MethodDelegateCommand(() => ChooseAudioStream(asdClosureCopy)),
                        Selected = asd == currentAudioStream,
                    };
                    item.AdditionalProperties[Consts.KEY_NAVIGATION_MODE] = NavigationMode.ExitPCWorkflow;
                    _playerSlotAudioMenu.Add(item);
                }
                if (_showToggleMute)
                {
                    ListItem item;
                    if (playerManager.Muted)
                    {
                        item = new ListItem(Consts.KEY_NAME, Consts.RES_MUTE_OFF)
                        {
                            Command  = new MethodDelegateCommand(PlayersResetMute),
                            Selected = true,
                        }
                    }
                    ;
                    else
                    {
                        item = new ListItem(Consts.KEY_NAME, Consts.RES_MUTE)
                        {
                            Command = new MethodDelegateCommand(PlayersMute)
                        }
                    };
                    item.AdditionalProperties[Consts.KEY_NAVIGATION_MODE] = NavigationMode.ExitPCWorkflow;
                    _playerSlotAudioMenu.Add(item);
                }

                IList <IPlayerContext> playerContexts = playerContextManager.PlayerContexts.ToList();
                _playerSlotAudioMenu.FireChange();
                _playerSlotAudioMenuHeader = LocalizationHelper.CreateResourceString(Consts.RES_PLAYER_SLOT_AUDIO_MENU).Evaluate(
                    GetNameForPlayerContext(_playerAudioMenuPlayerContext, playerContexts.IndexOf(_playerAudioMenuPlayerContext)));
            }
        }
        protected void UpdateAudioStreamsMenu()
        {
            // Some updates could be avoided if we tracked a "dirty" flag and break execution if !dirty
            lock (_syncObj)
            {
                IPlayerContextManager playerContextManager = ServiceRegistration.Get <IPlayerContextManager>();
                int numActivePlayers     = playerContextManager.NumActivePlayerContexts;
                int numberOfAudioStreams = GetTotalNumberOfAudioStreams();

                _audioStreamsMenu.Clear();
                for (int i = 0; i < numActivePlayers; i++)
                {
                    IPlayerContext pc = playerContextManager.GetPlayerContext(i);
                    if (pc == null || !pc.IsActive)
                    {
                        continue;
                    }
                    IPlayer player = pc.CurrentPlayer;
                    AudioStreamDescriptor         currentAudioStream;
                    IList <AudioStreamDescriptor> asds = new List <AudioStreamDescriptor>(pc.GetAudioStreamDescriptors(out currentAudioStream));
                    foreach (AudioStreamDescriptor asd in asds)
                    {
                        string playedItem = player == null ? null : player.MediaItemTitle ?? pc.Name;
                        string choiceItemName;
                        int    count = asds.Count;
                        if (numActivePlayers > 1 && count > 1 && count != numberOfAudioStreams)
                        {
                            // Only display the playedItem name if more than one player is able to provide audio streams. If a single player provides
                            // multiple streams, they will be distinguished by the VideoPlayer.
                            choiceItemName = playedItem + ": " + asd.AudioStreamName;
                        }
                        else
                        {
                            choiceItemName = count != numberOfAudioStreams ? playedItem : asd.AudioStreamName;
                        }

                        AudioStreamDescriptor asdClosureCopy = asd;
                        ListItem item = new ListItem(Consts.KEY_NAME, choiceItemName)
                        {
                            Command  = new MethodDelegateCommand(() => ChooseAudioStream(asdClosureCopy)),
                            Selected = asd == currentAudioStream,
                        };
                        item.AdditionalProperties[Consts.KEY_NAVIGATION_MODE] = NavigationMode.ExitPCWorkflow;
                        _audioStreamsMenu.Add(item);
                    }
                }
                _audioStreamsMenu.FireChange();
            }
        }
        protected void UpdateAudioStreamsMenu()
        {
            // Some updates could be avoided if we tracked a "dirty" flag and break execution if !dirty
            lock (_syncObj)
            {
                IPlayerManager        playerManager        = ServiceRegistration.Get <IPlayerManager>();
                IPlayerContextManager playerContextManager = ServiceRegistration.Get <IPlayerContextManager>();

                _audioStreamsMenu.Clear();
                for (int i = 0; i < playerManager.NumActiveSlots; i++)
                {
                    IPlayerContext                pc     = playerContextManager.GetPlayerContext(i);
                    IPlayer                       player = pc.CurrentPlayer;
                    AudioStreamDescriptor         currentAudioStream;
                    IList <AudioStreamDescriptor> asds = new List <AudioStreamDescriptor>(pc.GetAudioStreamDescriptors(out currentAudioStream));
                    foreach (AudioStreamDescriptor asd in asds)
                    {
                        string playedItem = player == null ? null : player.MediaItemTitle ?? pc.Name;
                        string choiceItemName;
                        if (asds.Count > 1)
                        {
                            // Only display the audio stream name if the player has more than one audio stream
                            choiceItemName = playedItem + ": " + asd.AudioStreamName;
                        }
                        else
                        {
                            choiceItemName = playedItem;
                        }
                        AudioStreamDescriptor asdClosureCopy = asd;
                        ListItem item = new ListItem(Consts.KEY_NAME, choiceItemName)
                        {
                            Command  = new MethodDelegateCommand(() => ChooseAudioStream(asdClosureCopy)),
                            Selected = asd == currentAudioStream,
                        };
                        item.AdditionalProperties[Consts.KEY_NAVIGATION_MODE] = NavigationMode.ExitPCWorkflow;
                        _audioStreamsMenu.Add(item);
                    }
                }
                _audioStreamsMenu.FireChange();
            }
        }