private async void SingleMedia_Click(object sender, RoutedEventArgs e) { var image = sender as ImageView; if (image.DataContext is TLMessage message && message.Media is TLMessageMediaWebPage webPageMedia && webPageMedia.WebPage is TLWebPage webPage) { if (webPage.HasEmbedUrl) { await WebPageView.Current.ShowAsync(webPage); return; } else if (webPage.IsInstantGallery()) { var viewModel = new InstantGalleryViewModel(message, webPage); await GalleryView.Current.ShowAsync(viewModel, () => image.Parent as FrameworkElement); return; } } if (image.Constraint is TLPhoto photo) { var viewModel = new SingleGalleryViewModel(new GalleryPhotoItem(photo, null as string)); await GalleryView.Current.ShowAsync(viewModel, () => image.Parent as FrameworkElement); } }
public static async void Download(object sender, TransferCompletedEventArgs e) { var element = sender as FrameworkElement; var message = element.DataContext as TLMessage; if (message != null) { if (message.IsVideo() || message.IsRoundVideo() || message.IsGif() || message.IsPhoto()) { var media = element.Ancestors().FirstOrDefault(x => x is FrameworkElement && ((FrameworkElement)x).Name.Equals("MediaControl")) as FrameworkElement; ConnectedAnimationService.GetForCurrentView().PrepareToAnimate("FullScreenPicture", media); GalleryViewModelBase viewModel; if (message.Parent != null) { viewModel = new DialogGalleryViewModel(message.Parent.ToInputPeer(), message, MTProtoService.Current); } else { viewModel = new SingleGalleryViewModel(new GalleryMessageItem(message)); } await GalleryView.Current.ShowAsync(viewModel, () => media); } else if (e != null) { var file = await StorageFile.GetFileFromApplicationUriAsync(FileUtils.GetTempFileUri(e.FileName)); await Launcher.LaunchFileAsync(file); } } }
private async void SingleMedia_Click(object sender, RoutedEventArgs e) { var image = sender as ImageView; var item = image.Constraint as TLPhoto; if (item != null) { //ConnectedAnimationService.GetForCurrentView().PrepareToAnimate("FullScreenPicture", image); var viewModel = new SingleGalleryViewModel(new GalleryPhotoItem(item, null as string)); await GalleryView.Current.ShowAsync(viewModel, () => image); } }
public static async void Download_Click(FrameworkElement sender, TransferCompletedEventArgs e) { var element = sender as FrameworkElement; var bubble = element.Ancestors <MessageBubbleBase>().FirstOrDefault() as MessageBubbleBase; if (bubble == null) { return; } if (element.DataContext is TLMessageService serviceMessage && serviceMessage.Action is TLMessageActionChatEditPhoto editPhotoAction) { var media = element.Parent as FrameworkElement; if (media == null) { media = element; } var chat = serviceMessage.Parent as TLChatBase; if (chat == null) { return; } var chatFull = InMemoryCacheService.Current.GetFullChat(chat.Id); if (chatFull != null && chatFull.ChatPhoto is TLPhoto && chat != null) { var viewModel = new ChatPhotosViewModel(bubble.ContextBase.ProtoService, bubble.ContextBase.CacheService, chatFull, chat, serviceMessage); await GalleryView.Current.ShowAsync(viewModel, () => media); } return; } var message = element.DataContext as TLMessage; if (message == null) { return; } var document = message.GetDocument(); if (TLMessage.IsGif(document) && !ApplicationSettings.Current.IsAutoPlayEnabled) { var page = bubble.Ancestors <IGifPlayback>().FirstOrDefault() as IGifPlayback; if (page == null) { return; } if (bubble.ViewModel is TLMessage inner) { page.Play(inner); } } else if (TLMessage.IsVideo(document) || TLMessage.IsRoundVideo(document) || TLMessage.IsGif(document) || message.IsPhoto()) { var media = element.Ancestors().FirstOrDefault(x => x is FrameworkElement && ((FrameworkElement)x).Name.Equals("Media")) as FrameworkElement; if (media == null) { media = element; } //ConnectedAnimationService.GetForCurrentView().PrepareToAnimate("FullScreenPicture", media); GalleryViewModelBase viewModel; if (message.Parent == null || TLMessage.IsRoundVideo(document) || TLMessage.IsGif(document)) { viewModel = new SingleGalleryViewModel(new GalleryMessageItem(message)); } else { viewModel = new DialogGalleryViewModel(bubble.ContextBase.ProtoService, bubble.ContextBase.CacheService, message.Parent.ToInputPeer(), message); } await GalleryView.Current.ShowAsync(viewModel, () => media); } else if (e != null) { var file = await StorageFile.GetFileFromApplicationUriAsync(FileUtils.GetTempFileUri(e.FileName)); await Launcher.LaunchFileAsync(file); } }
public async void PlayMessage(MessageViewModel message, FrameworkElement target) { var text = message.Content as MessageText; // If autoplay is enabled and the message contains a video note, then we want a different behavior if (ViewModel.Settings.IsAutoPlayAnimationsEnabled && (message.Content is MessageVideoNote || text?.WebPage != null && text.WebPage.Video != null)) { ViewModel.PlaybackService.Play(message, ViewModel.ThreadId); //if (_old.TryGetValue(message.Id, out MediaPlayerItem item)) //{ // if (item.Presenter == null || item.Presenter.MediaPlayer == null) // { // return; // } // // If the video player is muted, then let's play the video again with audio turned on // if (item.Presenter.MediaPlayer.IsMuted) // { // TypedEventHandler<MediaPlayer, object> handler = null; // handler = (player, args) => // { // player.MediaEnded -= handler; // player.IsMuted = true; // player.IsLoopingEnabled = true; // player.Play(); // }; // item.Presenter.MediaPlayer.MediaEnded += handler; // item.Presenter.MediaPlayer.IsMuted = false; // item.Presenter.MediaPlayer.IsLoopingEnabled = false; // item.Presenter.MediaPlayer.PlaybackSession.Position = TimeSpan.Zero; // // Mark it as viewed if needed // if (message.Content is MessageVideoNote videoNote && !message.IsOutgoing && !videoNote.IsViewed) // { // ViewModel.ProtoService.Send(new OpenMessageContent(message.ChatId, message.Id)); // } // } // // If the video player is paused, then resume playback // else if (item.Presenter.MediaPlayer.PlaybackSession.PlaybackState == MediaPlaybackState.Paused) // { // item.Presenter.MediaPlayer.Play(); // } // // And last, if the video player can be pause, then pause it // else if (item.Presenter.MediaPlayer.PlaybackSession.CanPause) // { // item.Presenter.MediaPlayer.Pause(); // } //} } else if (ViewModel.Settings.IsAutoPlayAnimationsEnabled && (message.Content is MessageAnimation || (text?.WebPage != null && text.WebPage.Animation != null) || (message.Content is MessageGame game && game.Game.Animation != null))) { if (_prev.TryGetValue(message.AnimationHash(), out WeakReference reference) && reference.Target is IPlayerView item) { GalleryViewModelBase viewModel; if (message.Content is MessageAnimation) { viewModel = new ChatGalleryViewModel(ViewModel.ProtoService, ViewModel.StorageService, ViewModel.Aggregator, message.ChatId, ViewModel.ThreadId, message.Get()); } else { viewModel = new SingleGalleryViewModel(ViewModel.ProtoService, ViewModel.StorageService, ViewModel.Aggregator, new GalleryMessage(ViewModel.ProtoService, message.Get())); } await GalleryView.GetForCurrentView().ShowAsync(viewModel, () => target); } else { ViewVisibleMessages(false); } }
public async void PlayMessage(MessageViewModel message, FrameworkElement target) { var text = message.Content as MessageText; // If autoplay is enabled and the message contains a video note, then we want a different behavior if (ViewModel.Settings.IsAutoPlayAnimationsEnabled && (message.Content is MessageVideoNote || text?.WebPage != null && text.WebPage.Video != null)) { if (SettingsService.Current.Diagnostics.SoftwareDecoderEnabled) //TODO: Make audio and video playable like in other clients: together & control on top... { ViewModel.PlaybackService.Enqueue(message.Get()); } //if (_old.TryGetValue(message.Id, out MediaPlayerItem item)) //{ // if (item.Presenter == null || item.Presenter.MediaPlayer == null) // { // return; // } // // If the video player is muted, then let's play the video again with audio turned on // if (item.Presenter.MediaPlayer.IsMuted) // { // TypedEventHandler<MediaPlayer, object> handler = null; // handler = (player, args) => // { // player.MediaEnded -= handler; // player.IsMuted = true; // player.IsLoopingEnabled = true; // player.Play(); // }; // item.Presenter.MediaPlayer.MediaEnded += handler; // item.Presenter.MediaPlayer.IsMuted = false; // item.Presenter.MediaPlayer.IsLoopingEnabled = false; // item.Presenter.MediaPlayer.PlaybackSession.Position = TimeSpan.Zero; // // Mark it as viewed if needed // if (message.Content is MessageVideoNote videoNote && !message.IsOutgoing && !videoNote.IsViewed) // { // ViewModel.ProtoService.Send(new OpenMessageContent(message.ChatId, message.Id)); // } // } // // If the video player is paused, then resume playback // else if (item.Presenter.MediaPlayer.PlaybackSession.PlaybackState == MediaPlaybackState.Paused) // { // item.Presenter.MediaPlayer.Play(); // } // // And last, if the video player can be pause, then pause it // else if (item.Presenter.MediaPlayer.PlaybackSession.CanPause) // { // item.Presenter.MediaPlayer.Pause(); // } //} if (_old.TryGetValue(message.Id, out MediaPlayerItem item)) { if (item.MediaPlayerPresenter?.MediaPlayer == null) { return; } // If the video player is muted, then let's play the video again with audio turned on if (item.MediaPlayerPresenter.MediaPlayer.IsMuted) { item.MediaPlayerPresenter.MediaPlayer.IsMuted = false; item.MediaPlayerPresenter.MediaPlayer.IsLoopingEnabled = false; item.MediaPlayerPresenter.MediaPlayer.PlaybackSession.Position = TimeSpan.Zero; PauseAllOtherVideoNotes(message.Id); // Mark it as viewed if needed if (message.Content is MessageVideoNote videoNote && !message.IsOutgoing && !videoNote.IsViewed) { ViewModel.ProtoService.Send(new OpenMessageContent(message.ChatId, message.Id)); } if (item.Container.FindName("Progress") is RadialProgressBar progress) { progress.Maximum = item.MediaPlayerPresenter.MediaPlayer.PlaybackSession.NaturalDuration.TotalSeconds; async void ProgressHandler(Windows.Media.Playback.MediaPlaybackSession session, object args) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { progress.Value = session.Position.TotalSeconds; }); } item.MediaPlayerPresenter.MediaPlayer.PlaybackSession.PositionChanged -= ProgressHandler; item.MediaPlayerPresenter.MediaPlayer.PlaybackSession.PositionChanged += ProgressHandler; } // MediaEnded if (item.Container.FindName("MutedIcon") is StackPanel mutedIcon) { mutedIcon.Visibility = Visibility.Collapsed; async void MediaEndedHandler(Windows.Media.Playback.MediaPlayer player, object args) { player.IsMuted = true; if (player.PlaybackSession.CanPause) { player.Pause(); } await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { mutedIcon.Visibility = Visibility.Visible; }); //TODO: Play next media content and scroll to that one } item.MediaPlayerPresenter.MediaPlayer.MediaEnded -= MediaEndedHandler; item.MediaPlayerPresenter.MediaPlayer.MediaEnded += MediaEndedHandler; } if (item.MediaPlayerPresenter.MediaPlayer.PlaybackSession.PlaybackState == Windows.Media.Playback.MediaPlaybackState.Paused) { item.MediaPlayerPresenter.MediaPlayer.Play(); } } // If the video player is paused, then resume playback else if (item.MediaPlayerPresenter.MediaPlayer.PlaybackSession.PlaybackState == Windows.Media.Playback.MediaPlaybackState.Paused) { item.MediaPlayerPresenter.MediaPlayer.IsMuted = false; item.MediaPlayerPresenter.MediaPlayer.Play(); PauseAllOtherVideoNotes(message.Id); if (item.Container.FindName("MutedIcon") is StackPanel mutedIcon) { mutedIcon.Visibility = Visibility.Collapsed; } } // And last, if the video player can be pause, then pause it else if (item.MediaPlayerPresenter.MediaPlayer.PlaybackSession.CanPause) { item.MediaPlayerPresenter.MediaPlayer.Pause(); // Note: UWP has no event or public player state property, so it gets hardcoded: if (item.Container.FindName("MutedIcon") is StackPanel mutedIcon) { mutedIcon.Visibility = Visibility.Visible; } } } } else if (ViewModel.Settings.IsAutoPlayAnimationsEnabled && (message.Content is MessageAnimation || (text?.WebPage != null && text.WebPage.Animation != null) || (message.Content is MessageGame game && game.Game.Animation != null))) { if (_old.TryGetValue(message.AnimationHash(), out MediaPlayerItem item)) { GalleryViewModelBase viewModel; if (message.Content is MessageAnimation) { viewModel = new ChatGalleryViewModel(ViewModel.ProtoService, ViewModel.Aggregator, message.ChatId, ViewModel.ThreadId, message.Get()); } else { viewModel = new SingleGalleryViewModel(ViewModel.ProtoService, ViewModel.Aggregator, new GalleryMessage(ViewModel.ProtoService, message.Get())); } await GalleryView.GetForCurrentView().ShowAsync(viewModel, () => target); } else { ViewVisibleMessages(false); } }