Пример #1
0
        /// <summary>
        /// Create a playback list from the list of songs received from the foreground app.
        /// </summary>
        /// <param name="songs"></param>
        private async Task CreatePlaybackList(QuranAudioTrack newTrack)
        {
            BackgroundMediaPlayer.Current.Pause();

            // Make a new list
            if (_playbackList != null)
            {
                _playbackList.CurrentItemChanged -= PlaybackListCurrentItemChanged;
                _playbackList.Items.Clear();
            }
            else
            {
                _playbackList = new MediaPlaybackList();
            }

            // Initialize FileUtils
            await FileUtils.Initialize(newTrack.ScreenInfo);

            var missingFiles = await AudioUtils.GetMissingFiles(newTrack);

            // Add playback items to the list
            QuranAudioTrack nextTrack = newTrack.GetFirstAyah();
            var             reciter   = nextTrack.GetReciter();

            while (nextTrack != null)
            {
                string      fileName = AudioUtils.GetFileName(nextTrack.GetQuranAyah(), reciter);
                MediaSource source;
                if (missingFiles.Contains(fileName))
                {
                    source = MediaSource.CreateFromUri(new Uri(Path.Combine(reciter.ServerUrl, fileName)));
                }
                else
                {
                    var file = await FileUtils.GetFile(await reciter.GetStorageFolder(), fileName);

                    if (file != null)
                    {
                        source = MediaSource.CreateFromStorageFile(file);
                        //source = MediaSource.CreateFromUri(new Uri(file.Path));
                    }
                    else
                    {
                        source = MediaSource.CreateFromUri(new Uri(Path.Combine(reciter.ServerUrl, fileName)));
                    }
                }
                source.CustomProperties[SurahKey]      = nextTrack.Surah;
                source.CustomProperties[AyahKey]       = nextTrack.Ayah;
                source.CustomProperties[ReciterKey]    = nextTrack.ReciterId;
                source.CustomProperties[QuranTrackKey] = nextTrack.ToString();
                source.CustomProperties[TitleKey]      = QuranUtils.GetSurahAyahString(nextTrack.Surah, nextTrack.Ayah);
                _playbackList.Items.Add(new MediaPlaybackItem(source));
                nextTrack = nextTrack.GetNextInSurah();
            }

            // Don't auto start
            BackgroundMediaPlayer.Current.AutoPlay = false;

            // Assign the list to the player
            BackgroundMediaPlayer.Current.Source = _playbackList;

            // Add handler for future playlist item changes
            _playbackList.CurrentItemChanged += PlaybackListCurrentItemChanged;
        }