Esempio n. 1
0
            public void RequestStop()
            {
                if (this.IsStopRequested)
                {
                    // stop
                    return;
                }

                if (!ComponentMusicSource.IsReady)
                {
                    // music resource is not loaded and stop requested - simply stop right now
                    ComponentMusicSource.IsLooped = false;
                    ComponentMusicSource.Stop();
                    return;
                }

                if (this.Playlist != CurrentPlaylist)
                {
                    // playlist changed, in that case a special fade out duration applies
                    this.CurrentFadeOutDuration = Math.Min(this.CurrentFadeOutDuration,
                                                           this.Playlist.FadeOutDurationOnPlaylistChange);
                }

                this.stopAtPosition = ComponentMusicSource.Position + this.CurrentFadeOutDuration;
                if (this.stopAtPosition > ComponentMusicSource.Duration)
                {
                    // clamp to duration
                    this.stopAtPosition = ComponentMusicSource.Duration;
                }

                ComponentMusicSource.IsLooped = false;
            }
Esempio n. 2
0
            public CurrentMusicTrack(MusicTrack musicTrack, ProtoPlaylist playlist)
            {
                this.Playlist   = playlist;
                this.MusicTrack = musicTrack;
                // ensure the track is changed
                ComponentMusicSource.MusicResource = MusicResource.NoMusic;
                ComponentMusicSource.MusicResource = musicTrack.MusicResource;
                ComponentMusicSource.IsLooped      = musicTrack.IsLooped;
                this.CurrentFadeInDuration         = musicTrack.FadeInDuration;
                this.CurrentFadeOutDuration        = musicTrack.FadeOutDuration;

                if (MusicTrackLastStopTimeManager.TryGetLastStopTime(musicTrack.MusicResource, out var lastStopTime))
                {
                    this.startAtPosition = lastStopTime;
                    ComponentMusicSource.Seek(lastStopTime);
                    Logger.Important($"Resume music {musicTrack.MusicResource} from {lastStopTime}");
                }
                else
                {
                    this.startAtPosition = null;
                }

                this.Update();
                ComponentMusicSource.Play();
            }
Esempio n. 3
0
            public void Update()
            {
                if (!ComponentMusicSource.IsReady)
                {
                    ComponentMusicSource.Volume = 0;
                    return;
                }

                if (this.IsStopRequested &&
                    ComponentMusicSource.Position > (this.stopAtPosition ?? ComponentMusicSource.Duration))
                {
                    if (this.stopAtPosition.HasValue)
                    {
                        if (this.MusicTrack.IsLooped
                            // If track is not looped, check whether there is enough track time remains
                            // to continue playing the track on the resume.
                            // Enough time: at least 10 seconds plus track fade in and out durations.
                            || ((ComponentMusicSource.Duration - this.stopAtPosition.Value)
                                > (10
                                   + this.MusicTrack.FadeInDuration
                                   + this.MusicTrack.FadeOutDuration)))
                        {
                            Logger.Important(
                                $"Stop music and remember stop time {this.MusicTrack.MusicResource} at {this.stopAtPosition.Value}");
                            MusicPlaylistLastStopTimeManager.RememberLastTrack(this.Playlist,
                                                                               this.MusicTrack);
                            MusicTrackLastStopTimeManager.RememberLastTrack(this.MusicTrack.MusicResource,
                                                                            this.stopAtPosition.Value);
                        }
                    }

                    ComponentMusicSource.Stop();

                    return;
                }

                this.ApplyFade();
            }