Esempio n. 1
0
        public static async Task play(string url, string title, string subtitle, string image)
        {
            mediaChannel = sender.GetChannel <IMediaChannel>();
            await sender.LaunchAsync(mediaChannel);

            var mediaInfo = new MediaInformation()
            {
                ContentId = url
            };

            mediaInfo.Metadata = new GenericMediaMetadata();
            if (title != null)
            {
                mediaInfo.Metadata.Title = title;
            }
            if (subtitle != null)
            {
                mediaInfo.Metadata.Subtitle = subtitle;
            }
            if (image != null)
            {
                mediaInfo.Metadata.Images    = new GoogleCast.Models.Image[1];
                mediaInfo.Metadata.Images[0] = new GoogleCast.Models.Image()
                {
                    Url = image
                };
            }
            mediastatus = await mediaChannel.LoadAsync(mediaInfo);
        }
Esempio n. 2
0
        public async Task <bool> Play(string contentLink)
        {
            MediaInformation mediaInformation = new MediaInformation();

            mediaInformation.ContentId   = contentLink;
            mediaInformation.ContentType = "audio/x-wav";
            await mediaChannel.LoadAsync(mediaInformation);

            await mediaChannel.PlayAsync();

            return(true);
        }
Esempio n. 3
0
        public async Task <MediaStatus> LoadAsync(
            MediaInformation media,
            bool autoPlay        = true,
            double seekedSeconds = 0,
            params int[] activeTrackIds)
        {
            _logger.LogInfo($"{nameof(LoadAsync)}: Trying to load media = {media.ContentId}");
            CurrentContentId = null;
            CancelAndSetListenerToken();

            FileLoading?.Invoke(this, EventArgs.Empty);

            var app = await _receiverChannel.GetApplication(_sender, _connectionChannel, _mediaChannel.Namespace);

            var status = await _mediaChannel.LoadAsync(_sender, app.SessionId, media, autoPlay, activeTrackIds);

            if (status is null)
            {
                LoadFailed?.Invoke(this, EventArgs.Empty);
                _logger.LogWarn($"{nameof(LoadAsync)}: Couldn't load media {media.ContentId}");
                return(null);
            }

            CurrentContentId     = media.ContentId;
            CurrentMediaDuration = media.Duration ?? status?.Media?.Duration ?? 0;
            CurrentVolumeLevel   = status?.Volume?.Level ?? 0;
            IsMuted        = status?.Volume?.IsMuted ?? false;
            ElapsedSeconds = 0;
            _seekedSeconds = seekedSeconds;

            TriggerTimeEvents();
            IsPlaying = true;
            IsPaused  = false;
            ListenForMediaChanges(_listenerToken.Token);
            ListenForReceiverChanges(_listenerToken.Token);

            FileLoaded?.Invoke(this, EventArgs.Empty);
            _logger.LogInfo($"{nameof(LoadAsync)}: Media = {media.ContentId} was loaded. Duration = {CurrentMediaDuration} - SeekSeconds = {_seekedSeconds}");
            return(status);
        }
Esempio n. 4
0
        private async Task PlayVideo()
        {
            IReceiver selectedCastDevice = CvChromecastDevices.SelectedItem as IReceiver;

            if (selectedCastDevice == null)
            {
                return;
            }

            Sender ccSender = new Sender();
            await ccSender.ConnectAsync(selectedCastDevice);

            IMediaChannel mediaChannel = ccSender.GetChannel <IMediaChannel>();
            await ccSender.LaunchAsync(mediaChannel);

            MediaStatus mediaStatus = await mediaChannel.LoadAsync(new MediaInformation()
            {
                ContentId = EnUrl.Text
            });

            CvChromecastDevices.SelectedItem = null;
            await CloseReceiversList();
        }
        public async Task LoadSong(string hashed, string songFileExt)
        {
            string filetype   = mbApiInterface.NowPlaying_GetFileProperty(FilePropertyType.Kind).Replace(" audio file", "");
            string samplerate = mbApiInterface.NowPlaying_GetFileProperty(FilePropertyType.SampleRate);
            string bitrate    = mbApiInterface.NowPlaying_GetFileProperty(FilePropertyType.Bitrate);
            string channels   = mbApiInterface.NowPlaying_GetFileProperty(FilePropertyType.Channels);
            string properties = "";
            string nextSong   = mbApiInterface.NowPlayingList_GetFileTag(mbApiInterface.NowPlayingList_GetNextIndex(1), MetaDataType.TrackTitle)
                                + " by " + mbApiInterface.NowPlayingList_GetFileTag(mbApiInterface.NowPlayingList_GetNextIndex(1), MetaDataType.Artist);

            nextSong = nextSong == " by " || nextSong == null ? "End of List" : nextSong;

            if (filetype == "FLAC")
            {
                using (FlacFile file = new FlacFile(mbApiInterface.NowPlaying_GetFileUrl()))
                {
                    properties = filetype + " " + file.StreamInfo.BitsPerSample.ToString() + " bit, " + samplerate + ", " + bitrate + ", " + channels;
                }
            }
            else
            {
                properties = filetype + " " + samplerate + ", " + bitrate + ", " + channels;
            }


            string[] temp = null;
            mbApiInterface.NowPlayingList_QueryFilesEx("", ref temp);
            int size = temp.Count();

            try
            {
                await mediaChannel.LoadAsync(
                    new MediaInformation()
                {
                    ContentId  = HttpUtility.UrlPathEncode(mediaContentURL + hashed + songFileExt),
                    StreamType = StreamType.Buffered,
                    Duration   = mbApiInterface.NowPlaying_GetDuration() / 1000,
                    Metadata   = new MusicTrackMediaMetadata
                    {
                        Artist    = mbApiInterface.NowPlaying_GetFileTag(MetaDataType.Artist),
                        Title     = mbApiInterface.NowPlaying_GetFileTag(MetaDataType.TrackTitle),
                        AlbumName = mbApiInterface.NowPlaying_GetFileTag(MetaDataType.Album),
                        Images    = new[] {
                            new GoogleCast.Models.Image
                            {
                                Url = mediaContentURL + hashed + ".jpg"
                            }
                        },
                    },

                    CustomData = new Dictionary <string, string>()
                    {
                        { "Properties", properties },
                        { "Position", (mbApiInterface.NowPlayingList_GetCurrentIndex() + 1).ToString() + " / " + size.ToString() },
                        { "Next", nextSong }
                    }
                });

                filenameStack.Push(hashed.ToString());
            }
            catch (OperationCanceledException)
            {
                Debug.WriteLine("Requested to close");
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }
Esempio n. 6
0
        public void ReceiveNotification(string sourceFileUrl, NotificationType type)
        {
            switch (type)
            {
            case NotificationType.PlayStateChanged:


                //Play and pause the chromecast from the MB player
                if (csSender != null)
                {
                    switch (mbApiInterface.Player_GetPlayState())
                    {
                    case PlayState.Paused:
                        csSender.GetChannel <IMediaChannel>().PauseAsync().WaitWithoutException();
                        break;

                    case PlayState.Playing:
                        csSender.GetChannel <IMediaChannel>().PlayAsync().WaitWithoutException();
                        break;
                    }

                    //csSender.GetChannel<IMediaChannel>().SeekAsync(mbApiInterface.Player_GetPosition() / 1000).WaitWithoutException();
                }

                break;

            case NotificationType.PluginStartup:
                break;

            case NotificationType.VolumeLevelChanged:
                break;

            case NotificationType.TrackChanged:

                if (!PrerequisitesMet())
                {
                    return;
                }


                try
                {
                    Task.Run(() =>
                    {
                        //Get the songname and format it into half of the url
                        StringBuilder songName = new StringBuilder(@mbApiInterface.NowPlaying_GetFileUrl());
                        songName.Replace(library, "");
                        songName.Replace(@"\", @"/");

                        mediaChannel.LoadAsync(
                            new MediaInformation()
                        {
                            ContentId  = mediaContentURL + HttpUtility.UrlPathEncode(songName.ToString()),        //Where the media is located
                            StreamType = StreamType.Buffered,
                            Metadata   = new MusicTrackMediaMetadata
                            {
                                Artist    = mbApiInterface.NowPlaying_GetFileTag(MetaDataType.Artist),      //Shows the Artist
                                Title     = mbApiInterface.NowPlaying_GetFileTag(MetaDataType.TrackTitle),  //Shows the Track Title
                                AlbumName = mbApiInterface.NowPlaying_GetFileTag(MetaDataType.Album),
                            },
                        });
                    });
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.Message);
                }



                break;
            }
        }