public StoryPage(Story story) { InitializeComponent(); settingsPage = new Settings(); ImageButton button = new ImageButton() { Source = "pause.png", HeightRequest = 40, BorderColor = Color.Transparent, BackgroundColor = Color.Transparent, Margin = 20 }; ImageButton button2 = new ImageButton() { Source = "play.png", IsVisible = false, HeightRequest = 40, BorderColor = Color.Transparent, BackgroundColor = Color.Transparent, Margin = 20 }; ImageButton Expand = new ImageButton() { Source = "expand.png", HorizontalOptions = LayoutOptions.EndAndExpand, HeightRequest = 40, BorderColor = Color.Transparent, BackgroundColor = Color.Transparent, Margin = 20 }; ImageButton QuizButton = new ImageButton() { Source = "Quizzes.png", BackgroundColor = Color.Green, IsVisible = false }; Label displayLabel = new Label { Text = "0:00", FontFamily = Device.RuntimePlatform == Device.Android ? "Comic.ttf#Comic" : "Comic", Margin = 20 }; Slider slider = new Slider { Maximum = story.Duration.Seconds + (story.Duration.Minutes * 60), Minimum = 0, Value = 0, HorizontalOptions = LayoutOptions.FillAndExpand, HeightRequest = 50 // Controls size of area that can grab the slider }; Image storyImage = new Image() { Source = story.PictureCues[new TimeSpan(0, 0, 0)], HeightRequest = 150 }; var tapGestureRecognizer = new TapGestureRecognizer(); tapGestureRecognizer.Tapped += (s, e) => { if (oldContent != null) { Content = oldContent; storyImage.HeightRequest = 150; fullScreen = false; } }; storyImage.GestureRecognizers.Add(tapGestureRecognizer); Expand.Clicked += (sender, args) => { if (!fullScreen) { Content = storyImage; fullScreen = true; } }; player = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current; player.Load(story.AudioClip); bool audioFromTimer = false; bool playAudio = true; player.Play(); Device.StartTimer(new TimeSpan(0, 0, 1), () => { if (playAudio) { audioFromTimer = true; slider.Value += 1; } if (slider.Value == story.Duration.Seconds + (story.Duration.Minutes * 60)) { player.Stop(); if (story.QuizNum > 0) { ChangePage(story); } else { GoBack(); } return(false); } return(true); }); button.Clicked += (sender, args) => { player.Pause(); playAudio = false; button.IsVisible = false; button2.IsVisible = true; }; button2.Clicked += (sender, args) => { player.Seek(timeStamp.TotalSeconds); player.Play(); playAudio = true; button.IsVisible = true; button2.IsVisible = false; }; QuizButton.Clicked += (sender, args) => { Navigation.PushAsync(new QuizPage(story.Quizzes[quizNum], story.AudioClip)); QuizButton.IsVisible = false; button.IsVisible = false; button2.IsVisible = true; }; slider.ValueChanged += (sender, args) => { QuizButton.IsVisible = false; int minutes = (int)args.NewValue / 60; int seconds = (int)args.NewValue - (minutes * 60); Console.WriteLine(args.NewValue); Console.WriteLine(player.CurrentPosition); Console.WriteLine(args.NewValue); if (!audioFromTimer) { player.Seek(args.NewValue); } String second = seconds.ToString(); if (seconds < 10) { second = '0' + seconds.ToString(); } displayLabel.Text = String.Format("{0}:{1}", minutes, second); timeStamp = new TimeSpan(0, minutes, seconds); var savedTime = new TimeSpan(0, 0, 0); foreach (TimeSpan key in story.PictureCues.Keys) { if (key.TotalSeconds < args.NewValue) { savedTime = key; } else { break; } } storyImage.Source = story.PictureCues[savedTime]; quizNum = -1; for (int i = 0; i < story.QuizNum; i++) { if (timeStamp.CompareTo(story.Quizzes[i].PlayTime) >= 0) { quizNum++; } } for (int i = 0; i < story.QuizNum; i++) { if (timeStamp.Equals(story.Quizzes[i].PlayTime)) { player.Pause(); QuizButton.IsVisible = true; playAudio = false; button.IsVisible = false; button2.IsVisible = true; Content = oldContent; storyImage.HeightRequest = 150; fullScreen = false; } } audioFromTimer = false; }; StackLayout audio = new StackLayout { Orientation = StackOrientation.Horizontal, Children = { button, button2, displayLabel, QuizButton, Expand } }; TopStack.Children.Add(storyImage); TopStack.Children.Add(slider); TopStack.Children.Add(audio); oldContent = Content; }
public StoryPage(Story story) { Story = story; StoryId = story.StoryId; StorySet = story.StorySetAsEnum; //pull the corresponding images out of the database var realmFile = Realm.GetInstance(RealmConfiguration.DefaultConfiguration); StoryPages = realmFile.All <RealmObjects.StoryPart>().Where(x => x.StoryId.Equals(story.StoryId)) .OrderBy(x => x.Order).ToList(); //get the current user's ID - if we ever want multiple users per device, we'll have to store the user's id in RAM var userId = realmFile.All <User>().FirstOrDefault().UserId; //init user story transaction using (var writer = realmFile.BeginWrite()) { UserStoryTransaction = new UserStoryReads(); UserStoryTransaction.StoryId = StoryId; UserStoryTransaction.UserId = userId; UserStoryTransaction.StartReadTime = DateTime.UtcNow; //add to the db realmFile.Add <UserStoryReads>(UserStoryTransaction); writer.Commit(); } InitializeComponent(); PlayButton.Source = PAUSE_ICON; //set at the pause icon because this page auto-starts PlayButton.HeightRequest = 40; PlayButton.WidthRequest = 50; PlayButton.BorderColor = Color.Transparent; PlayButton.BackgroundColor = Color.Transparent; PlayButton.Margin = 20; QuizButton.Source = "Quizzes.png"; QuizButton.BackgroundColor = Color.Green; QuizButton.IsVisible = false; DurationLabel.Text = "0:00"; DurationLabel.FontFamily = Device.RuntimePlatform == Device.Android ? "Comic.ttf#Comic" : "Comic"; DurationLabel.Margin = 20; CurrentStoryPage = StoryPages.First(); //story content StoryImage.Source = CurrentStoryPage.Image; StoryImage.MinimumWidthRequest = DeviceDisplay.MainDisplayInfo.Width; StoryImage.Aspect = Aspect.AspectFit; player = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current; player.Load(story.AudioClip); //find the story duration if we haven't already if (story.DurationInSeconds <= 0) { using (var transaction = realmFile.BeginWrite()) { story.DurationInSeconds = player.Duration; transaction.Commit(); } } //kill the realm file to help with memory consumption realmFile.Dispose(); realmFile = null; //slider init StoryPageSlider.Maximum = story.DurationInSeconds; StoryPageSlider.Minimum = 0; StoryPageSlider.Value = 0; StoryPageSlider.HorizontalOptions = LayoutOptions.FillAndExpand; //StoryPageSlider.MinimumWidthRequest = DeviceDisplay.MainDisplayInfo.Width - (PlayButton.Width * 4); StoryPageSlider.HeightRequest = 50; // Controls size of area that can grab the slider //use drag completed instead of value changed to avoid "stuttering" audio StoryPageSlider.DragCompleted += UserDraggedSlider; //register action to be taken once the story ends player.PlaybackEnded += EndPlayback; player.Loop = false; //start the player in a different thread var playerThread = new Thread(new ThreadStart(() => { //this starts the audio player.Play(); })); playerThread.Start(); PlayButton.Clicked += (sender, args) => { //toggle play/pause if (player.IsPlaying) { player.Pause(); PlayButton.Source = PLAY_ICON; } else { player.Play(); PlayButton.Source = PAUSE_ICON; } }; RefreshStoryPagesTimer(); QuizButton.Clicked += (sender, args) => { //Navigation.PushAsync(new QuizPage(story.Quizzes[quizNum], story.AudioClip)); }; }
// Goes to the end of story page protected void ChangePage(Story story) { Navigation.PushAsync(new EndOfStory(story)); }