Пример #1
0
        /// <summary>Plays the passed <see cref="PlayResource"/></summary>
        /// <param name="invoker">The invoker of this resource. Used for responses and association.</param>
        /// <param name="play">The associated resource type string to a factory.</param>
        /// <param name="meta">Allows overriding certain settings for the resource.</param>
        /// <returns>Ok if successful, or an error message otherwise.</returns>
        public E <LocalStr> Play(InvokerData invoker, PlayResource play, MetaData meta)
        {
            if (!meta.FromPlaylist)
            {
                meta.ResourceOwnerUid = invoker.ClientUid;
            }

            var playInfo = new PlayInfoEventArgs(invoker, play, meta);

            BeforeResourceStarted?.Invoke(this, playInfo);

            // pass the song to the AF to start it
            var result = StartResource(play, meta);

            if (!result)
            {
                return(result);
            }

            // add it to our freelist for comfort
            if (!meta.FromPlaylist)
            {
                int index = PlaylistManager.InsertToFreelist(new PlaylistItem(play.BaseData, meta));
                PlaylistManager.Index = index;
            }

            CurrentPlayData = playInfo;             // TODO meta as readonly
            AfterResourceStarted?.Invoke(this, CurrentPlayData);

            return(R.Ok);
        }
Пример #2
0
        private E <LocalStr> StartResource(InvokerData invoker, PlayResource play, MetaData meta = null)
        {
            play.Meta = meta ?? play.Meta ?? new MetaData();
            var sourceLink = resourceResolver.RestoreLink(play.BaseData).OkOr(null);
            var playInfo   = new PlayInfoEventArgs(invoker, play, sourceLink);

            BeforeResourceStarted?.Invoke(this, playInfo);

            if (string.IsNullOrWhiteSpace(play.PlayUri))
            {
                Log.Error("Internal resource error: link is empty (resource:{0})", play);
                return(new LocalStr(strings.error_playmgr_internal_error));
            }

            Log.Debug("AudioResource start: {0}", play);
            var result = playerConnection.Play(play);

            if (!result)
            {
                Log.Error("Error return from player: {0}", result.Error);
                return(new LocalStr(strings.error_playmgr_internal_error));
            }

            playerConnection.Volume = Tools.Clamp(playerConnection.Volume, confBot.Audio.Volume.Min, confBot.Audio.Volume.Max);
            CurrentPlayData         = playInfo;     // TODO meta as readonly
            AfterResourceStarted?.Invoke(this, playInfo);

            return(R.Ok);
        }
Пример #3
0
        public R Play(InvokerData invoker, PlayResource play, MetaData meta)
        {
            if (!meta.FromPlaylist)
            {
                meta.ResourceOwnerDbId = invoker.DatabaseId;
            }

            // add optional beforestart here. maybe for blocking/interrupting etc.
            BeforeResourceStarted?.Invoke(this, new EventArgs());

            // pass the song to the AF to start it
            var result = AudioFramework.StartResource(play, meta);

            if (!result)
            {
                return(result);
            }

            // add it to our freelist for comfort
            if (!meta.FromPlaylist)
            {
                int index = PlaylistManager.InsertToFreelist(new PlaylistItem(play.BaseData, meta));
                PlaylistManager.Index = index;
            }

            // Log our resource in the history
            ulong?owner = meta.ResourceOwnerDbId ?? invoker.DatabaseId;

            HistoryManager.LogAudioResource(new HistorySaveData(play.BaseData, owner));

            CurrentPlayData = new PlayInfoEventArgs(invoker, play, meta);             // TODO meta as readonly
            AfterResourceStarted?.Invoke(this, CurrentPlayData);

            return(R.OkR);
        }
Пример #4
0
        private E <LocalStr> Start(PlayResource resource, string restoredLink)
        {
            Log.Trace("Starting resource...");

            var playInfo = new PlayInfoEventArgs(resource.Meta.ResourceOwnerUid, resource, restoredLink);

            BeforeResourceStarted?.Invoke(this, playInfo);
            if (string.IsNullOrWhiteSpace(resource.PlayUri))
            {
                Log.Error("Internal resource error: link is empty (resource:{0})", resource);
                return(new LocalStr(strings.error_playmgr_internal_error));
            }

            var gain = resource.BaseData.Gain ?? 0;

            Log.Debug("AudioResource start: {0} with gain {1}", resource, gain);
            var result = player.Play(resource, gain);

            if (!result)
            {
                Log.Error("Error return from player: {0}", result.Error);
                return(new LocalStr(strings.error_playmgr_internal_error));
            }

            player.Volume = Tools.Clamp(player.Volume, config.Audio.Volume.Min, config.Audio.Volume.Max);
            AfterResourceStarted?.Invoke(this, playInfo);
            return(R.Ok);
        }
Пример #5
0
        private E <LocalStr> StartResource(InvokerData invoker, PlayResource play, MetaData meta)
        {
            if (meta.From != PlaySource.FromPlaylist)
            {
                meta.ResourceOwnerUid = invoker.ClientUid;
            }

            var sourceLink = resourceFactory.RestoreLink(play.BaseData).OkOr(null);
            var playInfo   = new PlayInfoEventArgs(invoker, play, meta, sourceLink);

            BeforeResourceStarted?.Invoke(this, playInfo);

            if (string.IsNullOrWhiteSpace(play.PlayUri))
            {
                Log.Error("Internal resource error: link is empty (resource:{0})", play);
                return(new LocalStr(strings.error_playmgr_internal_error));
            }

            Log.Debug("AudioResource start: {0}", play);
            var result = playerConnection.AudioStart(play);

            if (!result)
            {
                Log.Error("Error return from player: {0}", result.Error);
                return(new LocalStr(strings.error_playmgr_internal_error));
            }

            playerConnection.Volume = Util.Clamp(playerConnection.Volume, confBot.Audio.Volume.Min, confBot.Audio.Volume.Max);
            CurrentPlayData         = playInfo;     // TODO meta as readonly
            AfterResourceStarted?.Invoke(this, playInfo);

            return(R.Ok);
        }
Пример #6
0
        private void OnBeforeResourceStarted(object sender, PlayInfoEventArgs e)
        {
            lock (Lock) {
                if (sender == null || !ReferenceEquals(sender, Current))
                {
                    return;
                }

                BeforeResourceStarted?.Invoke(this, e);
            }
        }
Пример #7
0
        public E <LocalStr> StartResource(SongAnalyzerResult result)
        {
            var resource     = result.Resource;
            var restoredLink = result.RestoredLink.OkOr(null);

            var playInfo = new PlayInfoEventArgs(resource.Meta.ResourceOwnerUid, resource, restoredLink);

            BeforeResourceStarted?.Invoke(this, playInfo);

            var res = StartResource(resource);

            if (!res.Ok)
            {
                return(res);
            }

            AfterResourceStarted?.Invoke(this, playInfo);
            return(res);
        }
Пример #8
0
        /// <summary>Plays the passed <see cref="PlayResource"/></summary>
        /// <param name="invoker">The invoker of this resource. Used for responses and association.</param>
        /// <param name="play">The associated resource type string to a factory.</param>
        /// <param name="meta">Allows overriding certain settings for the resource.</param>
        /// <returns>Ok if successful, or an error message otherwise.</returns>
        public E <LocalStr> Play(InvokerData invoker, PlayResource play, MetaData meta)
        {
            if (meta.From != PlaySource.FromPlaylist)
            {
                meta.ResourceOwnerUid = invoker.ClientUid;
            }

            var sourceLink = ResourceFactoryManager.RestoreLink(play.BaseData);
            var playInfo   = new PlayInfoEventArgs(invoker, play, meta, sourceLink);

            BeforeResourceStarted?.Invoke(this, playInfo);

            var result = StartResource(play, meta);

            if (!result)
            {
                return(result);
            }

            CurrentPlayData = playInfo;             // TODO meta as readonly
            AfterResourceStarted?.Invoke(this, playInfo);

            return(R.Ok);
        }
Пример #9
0
        private async Task StartResource(InvokerData invoker, PlayResource play)
        {
            var sourceLink = resourceResolver.RestoreLink(play.AudioResource);
            var playInfo   = new PlayInfoEventArgs(invoker, play, sourceLink);
            await BeforeResourceStarted.InvokeAsync(this, playInfo);

            if (string.IsNullOrWhiteSpace(play.PlayUri))
            {
                Log.Error("Internal resource error: link is empty (resource:{0})", play);
                throw Error.LocalStr(strings.error_playmgr_internal_error);
            }

            Log.Debug("AudioResource start: {0}", play);
            try { await playerConnection.Play(play); }
            catch (AudioBotException ex)
            {
                Log.Error("Error return from player: {0}", ex.Message);
                throw Error.Exception(ex).LocalStr(strings.error_playmgr_internal_error);
            }

            playerConnection.Volume = Tools.Clamp(playerConnection.Volume, confBot.Audio.Volume.Min, confBot.Audio.Volume.Max);
            CurrentPlayData         = playInfo;     // TODO meta as readonly
            await AfterResourceStarted.InvokeAsync(this, playInfo);
        }
Пример #10
0
 protected void InvokeBeforeResourceStarted(object sender, PlayInfoEventArgs e)
 {
     BeforeResourceStarted?.Invoke(sender, e);
 }
Пример #11
0
 private void OnBeforeResourceStarted(object sender, PlayInfoEventArgs e)
 {
     lock (Lock) {
         BeforeResourceStarted?.Invoke(this, e);
     }
 }