Example #1
0
        public async Task PlayAsync()
        {
            try
            {
                await SendChannelCommandAsync <IMediaChannel>(!IsInitialized || IsStopped,
                                                              async c =>
                {
                    if (insertingItems.Count > 0 && await ConnectAsync())
                    {
                        ISender sender             = MediaSender;
                        IMediaChannel mediaChannel = sender.GetChannel <IMediaChannel>();
                        await sender.LaunchAsync(mediaChannel);

                        Queue <QueueItem> itemsQueue = new Queue <QueueItem>(insertingItems);

                        await mediaChannel.QueueLoadAsync(RepeatMode.RepeatOff, itemsQueue.DequeueChunk(20).ToArray());

                        await RefreshQueueAsync();

                        while (itemsQueue.Count > 0)
                        {
                            await mediaChannel.QueueInsertAsync(itemsQueue.DequeueChunk(20).ToArray());
                        }

                        insertingItems.Clear();

                        IsInitialized = true;
                    }
                },
                                                              async c =>
                {
                    if (insertingItems.Count > 0)
                    {
                        Queue <QueueItem> itemsQueue = new Queue <QueueItem>(insertingItems);

                        while (itemsQueue.Count > 0)
                        {
                            await MediaSender.GetChannel <IMediaChannel>().QueueInsertAsync(itemsQueue.DequeueChunk(20).ToArray());
                        }

                        insertingItems.Clear();
                    }

                    await c.PlayAsync();
                });
            }
            catch
            { }
        }
        public async Task LoadSongs(List <SongInfo> songs)
        {
            try
            {
                MediaInformation[] info = new MediaInformation[songs.Count];

                for (int i = 0; i < info.Count(); i++)
                {
                    string[] res = null;
                    mbApiInterface.Library_GetFileTags(songs[i].FileURL, new[] { MetaDataType.Artist, MetaDataType.TrackTitle, MetaDataType.Album }, ref res);
                    info[i] = new MediaInformation
                    {
                        ContentId  = HttpUtility.UrlPathEncode(mediaContentURL + songs[i].Hashed + songs[i].SongFileExt),
                        StreamType = StreamType.Buffered,
                        Duration   = mbApiInterface.NowPlaying_GetDuration() / 1000,
                        Metadata   = new MusicTrackMediaMetadata
                        {
                            Artist    = res[0],
                            Title     = res[1],
                            AlbumName = res[2],
                            Images    = new[] {
                                new GoogleCast.Models.Image
                                {
                                    Url = mediaContentURL + songs[i].Hashed + ".jpg"
                                }
                            },
                        }
                    };
                    filenameStack.Push(songs[i].Hashed.ToString());
                }


                await mediaChannel.QueueLoadAsync(GoogleCast.Models.Media.RepeatMode.RepeatOff, info);
            }
            catch (OperationCanceledException)
            {
                Debug.WriteLine("Requested to close");
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }