Exemplo n.º 1
0
        public void Prev()
        {
            if (MusicsListCollection.Count == 0)
            {
                return;
            }

            var currentIndex = MusicsListCollection.IndexOf(CurrentMusic);

            switch (CurrentCycleType)
            {
            case PlayCycleType.RepeatAll:
                if (currentIndex == 0)
                {
                    CurrentMusic = MusicsListCollection[MusicsListCollection.Count - 1];
                }
                else
                {
                    CurrentMusic = MusicsListCollection[currentIndex - 1];
                }
                break;

            case PlayCycleType.RepeatOne:
                CurrentMusic = CurrentMusic;
                break;

            case PlayCycleType.Order:
                if (currentIndex > 0)
                {
                    CurrentMusic = MusicsListCollection[currentIndex - 1];
                }
                else
                {
                    CurrentMusic = null;
                }
                break;

            case PlayCycleType.Random:
                if (this._randomIndex == null || this._randomIndex.Length != MusicsListCollection.Count || this._randomIndex.All(x => x == 0))
                {
                    this._randomIndex = RandomSequence(MusicsListCollection.Count);
                }
                var randomIndex = this._randomIndex.IndexOf(currentIndex);
                if (randomIndex == 0)
                {
                    randomIndex = this._randomIndex.Length - 1;
                }
                else
                {
                    randomIndex--;
                }
                CurrentMusic = MusicsListCollection[this._randomIndex[randomIndex]];
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            if (CurrentMusic != null)
            {
                Play(CurrentMusic).Wait(1000);
            }
            else
            {
                Stop();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 播放指定的音乐
        /// </summary>
        /// <param name="music">如果传入null,则自动播放当前列表的第一个</param>
        public async Task Play(Music music = null)
        {
            if (music == null && MusicsListCollection.Count == 0)
            {
                throw new ArgumentException();
            }

            if (this._audioPlayableServices.PlayState == PlayState.Paused)
            {
                this._audioPlayableServices.Resume();
                return;
            }
            Stop();
            if (MusicsListCollection.Count == 0)
            {
                MusicsListCollection.Add(music);
                CurrentMusic = music;
            }

            if (music == null)
            {
                CurrentMusic = MusicsListCollection[0];
            }
            else
            {
                var tmp = MusicsListCollection.FirstOrDefault(x => x.Id == music.Id);
                if (tmp == null)
                {
                    MusicsListCollection.Add(music);
                    CurrentMusic = music;
                }
                else
                {
                    CurrentMusic = tmp;
                }
            }
            var netWorkDataResult = await this._netWorkServices.GetAsync <Music>("Common", "GetMusicById", new { CurrentMusic.Id });

            if (!netWorkDataResult.Successed)
            {
                this._logger.Log($"请求音乐地址失败,对应的id:{CurrentMusic.Id},名称{CurrentMusic.Name}", Category.Exception, Priority.High);
                return;
            }

            CurrentMusic.Url = netWorkDataResult.Data.Url;
            if (!string.IsNullOrEmpty(netWorkDataResult.Data.Url))
            {
                this._audioPlayableServices.Play(netWorkDataResult.Data.Url);
                PlayStateChanged?.Invoke(this, this._audioPlayableServices.PlayState);
            }
            else if (PayMusicProgress(CurrentMusic))
            {
                netWorkDataResult = await this._netWorkServices.GetAsync <Music>("Common", "GetMusicById", new { CurrentMusic.Id });

                if (!netWorkDataResult.Successed)
                {
                    this._logger.Log($"请求音乐地址失败,对应的id:{CurrentMusic.Id},名称{CurrentMusic.Name}", Category.Exception, Priority.High);
                    return;
                }
                CurrentMusic.Url = netWorkDataResult.Data.Url;
                if (!string.IsNullOrEmpty(netWorkDataResult.Data.Url))
                {
                    this._audioPlayableServices.Play(netWorkDataResult.Data.Url);
                    PlayStateChanged?.Invoke(this, this._audioPlayableServices.PlayState);
                }
            }
        }