示例#1
0
        private PlaybackItem GetPlaybackItem(Message message)
        {
            var token = $"{message.ChatId}_{message.Id}";
            var file  = GetFile(message);

            var binder = new MediaBinder();

            binder.Token    = token;
            binder.Binding += Binder_Binding;

            var source = MediaSource.CreateFromMediaBinder(binder);
            var item   = new PlaybackItem(source);

            source.CustomProperties["file"]    = file.Id;
            source.CustomProperties["message"] = message.Id;
            source.CustomProperties["chat"]    = message.ChatId;
            source.CustomProperties["token"]   = token;

            item.File    = file;
            item.Message = message;
            item.Token   = token;

            if (message.Content is MessageAudio audio)
            {
                var performer = string.IsNullOrEmpty(audio.Audio.Performer) ? null : audio.Audio.Performer;
                var title     = string.IsNullOrEmpty(audio.Audio.Title) ? null : audio.Audio.Title;

                if (performer == null && title == null)
                {
                    item.Title  = audio.Audio.FileName;
                    item.Artist = string.Empty;
                }
                else
                {
                    item.Title  = string.IsNullOrEmpty(audio.Audio.Title) ? Strings.Resources.AudioUnknownTitle : audio.Audio.Title;
                    item.Artist = string.IsNullOrEmpty(audio.Audio.Performer) ? Strings.Resources.AudioUnknownArtist : audio.Audio.Performer;
                }
            }

            _mapping[token] = item;

            return(item);
        }
        private PlaybackItem GetPlaybackItem(Message message)
        {
            var token    = $"{message.ChatId}_{message.Id}";
            var file     = GetFile(message);
            var mime     = GetMimeType(message);
            var duration = GetDuration(message);

            var stream = new RemoteFileStream(_protoService, file, TimeSpan.FromSeconds(duration));
            var source = MediaSource.CreateFromStream(stream, mime);
            var item   = new PlaybackItem(source);

            _streams[file.Id].Add(stream);

            source.CustomProperties["file"]    = file.Id;
            source.CustomProperties["message"] = message.Id;
            source.CustomProperties["chat"]    = message.ChatId;
            source.CustomProperties["token"]   = token;

            item.File    = file;
            item.Message = message;
            item.Token   = token;

            if (message.Content is MessageAudio audio)
            {
                var performer = string.IsNullOrEmpty(audio.Audio.Performer) ? null : audio.Audio.Performer;
                var title     = string.IsNullOrEmpty(audio.Audio.Title) ? null : audio.Audio.Title;

                if (performer == null && title == null)
                {
                    item.Title  = audio.Audio.FileName;
                    item.Artist = string.Empty;
                }
                else
                {
                    item.Title  = string.IsNullOrEmpty(audio.Audio.Title) ? Strings.Resources.AudioUnknownTitle : audio.Audio.Title;
                    item.Artist = string.IsNullOrEmpty(audio.Audio.Performer) ? Strings.Resources.AudioUnknownArtist : audio.Audio.Performer;
                }
            }

            _mapping[token] = item;

            return(item);
        }