private void LoadAudioItem(CommentAdapterViewHolder soundViewHolder, int position, CommentObjectExtra item) { try { item.SoundViewHolder ??= soundViewHolder; soundViewHolder.VoiceLayout.Visibility = ViewStates.Visible; var fileName = item.Record.Split('/').Last(); var mediaFile = ObeeNetworkTools.GetFile(item.PostId, Methods.Path.FolderDcimSound, fileName, item.Record); soundViewHolder.DurationVoice.Text = string.IsNullOrEmpty(item.MediaDuration) ? Methods.AudioRecorderAndPlayer.GetTimeString(Methods.AudioRecorderAndPlayer.Get_MediaFileDuration(mediaFile)) : item.MediaDuration; soundViewHolder.PlayButton.Visibility = ViewStates.Visible; if (!soundViewHolder.PlayButton.HasOnClickListeners) { soundViewHolder.PlayButton.Click += (o, args) => { try { if (PositionSound != position) { var list = CommentList.Where(a => a.MediaPlayer != null).ToList(); if (list.Count > 0) { foreach (var extra in list) { if (extra.MediaPlayer != null) { extra.MediaPlayer.Stop(); extra.MediaPlayer.Reset(); } extra.MediaPlayer = null; extra.MediaTimer = null; extra.MediaPlayer?.Release(); extra.MediaPlayer = null; } } } if (mediaFile.Contains("http")) { mediaFile = ObeeNetworkTools.GetFile(item.PostId, Methods.Path.FolderDcimSound, fileName, item.Record); } if (item.MediaPlayer == null) { PositionSound = position; item.MediaPlayer = new Android.Media.MediaPlayer(); item.MediaPlayer.SetAudioAttributes(new AudioAttributes.Builder().SetUsage(AudioUsageKind.Media).SetContentType(AudioContentType.Music).Build()); item.MediaPlayer.Completion += (sender, e) => { try { soundViewHolder.PlayButton.Tag = "Play"; //soundViewHolder.PlayButton.SetImageResource(item.ModelType == MessageModelType.LeftAudio ? Resource.Drawable.ic_play_dark_arrow : Resource.Drawable.ic_play_arrow); soundViewHolder.PlayButton.SetImageResource(Resource.Drawable.ic_play_dark_arrow); item.MediaIsPlaying = false; item.MediaPlayer.Stop(); item.MediaPlayer.Reset(); item.MediaPlayer = null; item.MediaTimer.Enabled = false; item.MediaTimer.Stop(); item.MediaTimer = null; } catch (Exception exception) { Console.WriteLine(exception); } }; item.MediaPlayer.Prepared += (s, ee) => { try { item.MediaIsPlaying = true; soundViewHolder.PlayButton.Tag = "Pause"; soundViewHolder.PlayButton.SetImageResource(AppSettings.SetTabDarkTheme ? Resource.Drawable.ic_media_pause_light : Resource.Drawable.ic_media_pause_dark); if (item.MediaTimer == null) { item.MediaTimer = new Timer { Interval = 1000 } } ; item.MediaPlayer.Start(); //var durationOfSound = item.MediaPlayer.Duration; item.MediaTimer.Elapsed += (sender, eventArgs) => { ActivityContext.RunOnUiThread(() => { try { if (item.MediaTimer.Enabled) { if (item.MediaPlayer.CurrentPosition <= item.MediaPlayer.Duration) { soundViewHolder.DurationVoice.Text = Methods.AudioRecorderAndPlayer.GetTimeString(item.MediaPlayer.CurrentPosition); } else { soundViewHolder.DurationVoice.Text = Methods.AudioRecorderAndPlayer.GetTimeString(item.MediaPlayer.Duration); soundViewHolder.PlayButton.Tag = "Play"; //soundViewHolder.PlayButton.SetImageResource(item.ModelType == MessageModelType.LeftAudio ? Resource.Drawable.ic_play_dark_arrow : Resource.Drawable.ic_play_arrow); soundViewHolder.PlayButton.SetImageResource(Resource.Drawable.ic_play_dark_arrow); } } } catch (Exception e) { Console.WriteLine(e); soundViewHolder.PlayButton.Tag = "Play"; } }); }; item.MediaTimer.Start(); } catch (Exception e) { Console.WriteLine(e); } }; if (mediaFile.Contains("http")) { item.MediaPlayer.SetDataSource(ActivityContext, Uri.Parse(mediaFile)); item.MediaPlayer.PrepareAsync(); } else { File file2 = new File(mediaFile); var photoUri = FileProvider.GetUriForFile(ActivityContext, ActivityContext.PackageName + ".fileprovider", file2); item.MediaPlayer.SetDataSource(ActivityContext, photoUri); item.MediaPlayer.Prepare(); } item.SoundViewHolder = soundViewHolder; } else { if (soundViewHolder.PlayButton.Tag.ToString() == "Play") { soundViewHolder.PlayButton.Tag = "Pause"; soundViewHolder.PlayButton.SetImageResource(AppSettings.SetTabDarkTheme ? Resource.Drawable.ic_media_pause_light : Resource.Drawable.ic_media_pause_dark); item.MediaIsPlaying = true; item.MediaPlayer?.Start(); if (item.MediaTimer != null) { item.MediaTimer.Enabled = true; item.MediaTimer.Start(); } } else if (soundViewHolder.PlayButton.Tag.ToString() == "Pause") { soundViewHolder.PlayButton.Tag = "Play"; //soundViewHolder.PlayButton.SetImageResource(item.ModelType == MessageModelType.LeftAudio ? Resource.Drawable.ic_play_dark_arrow : Resource.Drawable.ic_play_arrow); soundViewHolder.PlayButton.SetImageResource(Resource.Drawable.ic_play_dark_arrow); item.MediaIsPlaying = false; item.MediaPlayer?.Pause(); if (item.MediaTimer != null) { item.MediaTimer.Enabled = false; item.MediaTimer.Stop(); } } } } catch (Exception e) { Console.WriteLine(e); } }; } if (item.SoundViewHolder == null) { item.SoundViewHolder = soundViewHolder; } } catch (Exception e) { Console.WriteLine(e); } }
private async Task SetStory(GetUserStoriesObject.StoryObject.Story story) { try { StoryId = story.Id; LastSeenTextView.Text = Methods.Time.ReplaceTime(story.TimeText); //image and video MediaFile = !story.Thumbnail.Contains("avatar") && story.Videos.Count == 0 ? story.Thumbnail : story.Videos[0].Filename; if (StoryVideoView == null) { InitVideoView(); } string caption = ""; if (!string.IsNullOrEmpty(story.Description)) { caption = story.Description; } else if (!string.IsNullOrEmpty(story.Title)) { caption = story.Title; } if (string.IsNullOrEmpty(caption) || string.IsNullOrWhiteSpace(caption)) { StoryaboutLayout.Visibility = ViewStates.Gone; } else { StoryaboutLayout.Visibility = ViewStates.Visible; CaptionStoryTextView.Text = Methods.FunString.DecodeString(caption); } if (StoryVideoView == null) { InitVideoView(); } var type = Methods.AttachmentFiles.Check_FileExtension(MediaFile); if (type == "Video") { //Show a progress //RunOnUiThread(() => { try { AndHUD.Shared.Show(this, GetText(Resource.String.Lbl_Loading)); }catch (Exception e) { Console.WriteLine(e); } }); var fileName = MediaFile.Split('/').Last(); MediaFile = ObeeNetworkTools.GetFile(DateTime.Now.Day.ToString(), Methods.Path.FolderDiskStory, fileName, MediaFile); StoryImageView.Visibility = ViewStates.Gone; StoryVideoView.Visibility = ViewStates.Visible; if (MediaFile.Contains("http")) { StoryVideoView.SetVideoURI(Uri.Parse(MediaFile)); StoryVideoView.Start(); } else { var file = Uri.FromFile(new File(MediaFile)); StoryVideoView.SetVideoPath(file.Path); StoryVideoView.Start(); } await Task.Delay(500); } else { StoryImageView.Visibility = ViewStates.Visible; StoryVideoView.Visibility = ViewStates.Gone; Glide.With(this).Load(MediaFile).Apply(new RequestOptions()).Into(StoryImageView); // GlideImageLoader.LoadImage(this,story.MediaFile, StoryImageView, ImageStyle.CenterCrop, ImagePlaceholders.Drawable); } } catch (Exception e) { Console.WriteLine(e); } }