Example #1
0
        public override async Task <bool> PrepareData(PlaybackData data, bool isPlaying)
        {
            playbackData = data;
            stateObserver?.Dispose();
            stateObserver = null;
            CurrentSongId = data.SongId;
            AVPlayerItem playerItem       = null;
            var          songPlaybackData = data.SongPlaybackData;

            if (songPlaybackData.IsLocal || songPlaybackData.CurrentTrack.ServiceType == MusicPlayer.Api.ServiceType.iPod)
            {
                if (songPlaybackData.Uri == null)
                {
                    return(false);
                }
                var url = string.IsNullOrWhiteSpace(songPlaybackData?.CurrentTrack?.FileLocation) ? new NSUrl(songPlaybackData.Uri.AbsoluteUri) : NSUrl.FromFilename(songPlaybackData.CurrentTrack.FileLocation);
                playerItem = AVPlayerItem.FromUrl(url);
                await playerItem.WaitStatus();
            }
            else
            {
#if HttpPlayback
                var urlEndodedSongId = HttpUtility.UrlEncode(data.SongId);
                var url = $"http://localhost:{LocalWebServer.Shared.Port}/api/GetMediaStream/Playback?SongId={urlEndodedSongId}";
                playerItem = AVPlayerItem.FromUrl(new NSUrl(url));
#else
                NSUrlComponents comp =
                    new NSUrlComponents(
                        NSUrl.FromString(
                            $"http://localhost/{songPlaybackData.CurrentTrack.Id}.{data.SongPlaybackData.CurrentTrack.FileExtension}"), false);
                comp.Scheme = "streaming";
                if (comp.Url != null)
                {
                    var asset = new AVUrlAsset(comp.Url, new NSDictionary());
                    asset.ResourceLoader.SetDelegate(resourceDelegate, DispatchQueue.MainQueue);
                    playerItem    = new AVPlayerItem(asset, (NSString)"duration");
                    stateObserver = playerItem.AddObserver("status", NSKeyValueObservingOptions.New, (obj) =>
                    {
                        if (shouldBePlaying)
                        {
                            player.Play();
                        }
                        Console.WriteLine($"Player Status {playerItem.Status}");
                    });
                }
#endif
            }
            player.ReplaceCurrentItemWithPlayerItem(playerItem);
            IsPrepared = true;

            return(true);
        }
Example #2
0
        public override async Task <bool> PrepareData(PlaybackData data, bool isPlaying)
        {
            CurrentSongId = data.SongId;
            AVPlayerItem playerItem   = null;
            var          playbackData = data.SongPlaybackData;

            if (playbackData.IsLocal || playbackData.CurrentTrack.ServiceType == MusicPlayer.Api.ServiceType.iPod)
            {
                if (playbackData.Uri == null)
                {
                    return(false);
                }
                var url = string.IsNullOrWhiteSpace(playbackData?.CurrentTrack?.FileLocation) ? new NSUrl(playbackData.Uri.AbsoluteUri) : NSUrl.FromFilename(playbackData.CurrentTrack.FileLocation);
                playerItem = AVPlayerItem.FromUrl(url);
                await playerItem.WaitStatus();
            }
            else
            {
#if HttpPlayback
                var urlEndodedSongId = HttpUtility.UrlEncode(data.SongId);
                var url = $"http://localhost:{LocalWebServer.Shared.Port}/api/GetMediaStream/Playback?SongId={urlEndodedSongId}";
                playerItem = AVPlayerItem.FromUrl(new NSUrl(url));
#else
                NSUrlComponents comp =
                    new NSUrlComponents(
                        NSUrl.FromString(
                            $"http://localhost/{playbackData.CurrentTrack.Id}.{data.SongPlaybackData.CurrentTrack.FileExtension}"), false);
                comp.Scheme = "streaming";
                if (comp.Url != null)
                {
                    var asset = new AVUrlAsset(comp.Url, new NSDictionary());
                    asset.ResourceLoader.SetDelegate(NativeAudioPlayer.LoaderDelegate, DispatchQueue.MainQueue);
                    playerItem = new AVPlayerItem(asset);
                }
#endif
            }
            player.ReplaceCurrentItemWithPlayerItem(playerItem);
            IsPrepared = true;
            return(true);
        }
Example #3
0
        async Task <Tuple <bool, AVPlayerItem> > prepareSong(Song song, bool playVideo = false)
        {
            try
            {
                isVideo = playVideo;
                LogManager.Shared.Log("Preparing Song", song);
                var data         = GetPlaybackData(song.Id);
                var playbackData = await MusicManager.Shared.GetPlaybackData(song, playVideo);

                if (playbackData == null)
                {
                    return(new Tuple <bool, AVPlayerItem>(false, null));
                }
                if (data.CancelTokenSource.IsCancellationRequested)
                {
                    return(new Tuple <bool, AVPlayerItem>(false, null));
                }

                AVPlayerItem playerItem = null;

                if (song == CurrentSong)
                {
                    Settings.CurrentTrackId = playbackData.CurrentTrack.Id;
                    isVideo = playbackData.CurrentTrack.MediaType == MediaType.Video;
                    Settings.CurrentPlaybackIsVideo = isVideo;
                    NotificationManager.Shared.ProcVideoPlaybackChanged(isVideo);
                }
                if (playbackData.IsLocal || playbackData.CurrentTrack.ServiceType == MusicPlayer.Api.ServiceType.iPod)
                {
                    if (playbackData.Uri == null)
                    {
                        return(new Tuple <bool, AVPlayerItem>(false, null));
                    }
                    LogManager.Shared.Log("Local track found", song);
                    var url = string.IsNullOrWhiteSpace(playbackData?.CurrentTrack?.FileLocation) ? new NSUrl(playbackData.Uri.AbsoluteUri) : NSUrl.FromFilename(playbackData.CurrentTrack.FileLocation);
                    playerItem = AVPlayerItem.FromUrl(url);
                    await playerItem.WaitStatus();

                    NotificationManager.Shared.ProcSongDownloadPulsed(song.Id, 1f);
                }
                else
                {
                    data.SongPlaybackData = playbackData;
                    data.DownloadHelper   = await DownloadManager.Shared.DownloadNow(playbackData.CurrentTrack.Id, playbackData.Uri);

                    if (data.CancelTokenSource.IsCancellationRequested)
                    {
                        return(new Tuple <bool, AVPlayerItem>(false, null));
                    }
                    LogManager.Shared.Log("Loading online Track", data.SongPlaybackData.CurrentTrack);
                    SongIdTracks[data.SongPlaybackData.CurrentTrack.Id] = song.Id;
                    NSUrlComponents comp =
                        new NSUrlComponents(
                            NSUrl.FromString(
                                $"http://localhost/{playbackData.CurrentTrack.Id}.{data.SongPlaybackData.CurrentTrack.FileExtension}"), false);
                    comp.Scheme = "streaming";
                    if (comp.Url != null)
                    {
                        var asset = new AVUrlAsset(comp.Url, new NSDictionary());
                        asset.ResourceLoader.SetDelegate(LoaderDelegate, DispatchQueue.MainQueue);
                        playerItem = new AVPlayerItem(asset);
                    }
                    if (data.CancelTokenSource.IsCancellationRequested)
                    {
                        return(new Tuple <bool, AVPlayerItem>(false, null));
                    }

                    await playerItem.WaitStatus();
                }
                lastSeconds = -1;
                var success = !data.CancelTokenSource.IsCancellationRequested;

                return(new Tuple <bool, AVPlayerItem>(true, playerItem));
            }
            catch (Exception ex)
            {
                LogManager.Shared.Report(ex);
                return(new Tuple <bool, AVPlayerItem>(false, null));
            }
        }