public static void LoadPreview(CSong song, float start = -1f) { if (song == null) { throw new ArgumentNullException("song"); } if (!IsPlayingPreview) { Pause(); CSound.SetGlobalVolume(CConfig.PreviewMusicVolume); } bool songChanged = _CurPlayer.SongID != song.ID; _PreviewPlayer.Load(song); //Change song position only if song is changed or near to end if (songChanged || _CurPlayer.Position + 30 < _CurPlayer.Length) { _PreviewStartHelperTask = new Task(() => { float length = _PreviewPlayer.Length; if (length < 1) { length = 30; // If length is unknow or invalid assume a length of 30s } if (start < 0) { start = (song.Preview.Source == EDataSource.None) ? length / 4f : song.Preview.StartTime; } if (start > length - 5f) { start = Math.Max(0f, Math.Min(length / 4f, length - 5f)); } if (start >= 0.5f) { start -= 0.5f; } _PreviewPlayer.Position = start; Play(); }); _CurPlayer = _PreviewPlayer; } else { _PreviewStartHelperTask.Dispose(); _PreviewStartHelperTask = null; _PreviewPlayer.Position = _CurPlayer.Position; _CurPlayer = _PreviewPlayer; Play(); } }
public static void StopPreview() { if (!IsPlayingPreview) { return; } Stop(); _CurPlayer = _BGPlayer; if (_MusicSource != EBackgroundMusicSource.TR_CONFIG_NO_OWN_MUSIC) { _CurPlayer.Load(CSongs.GetSong(_PreviewPlayer.SongID)); _CurPlayer.Position = _PreviewPlayer.Position; } CSound.SetGlobalVolume(CConfig.BackgroundMusicVolume); }
/// <summary> /// Initializes the background music with values from config /// Has to be called before any other method is used /// </summary> public static void Init() { if (_Initialized) { return; } List <string> soundFiles = CHelper.ListSoundFiles(CSettings.FolderNameBackgroundMusic, true, true); foreach (string path in soundFiles) { _BGMusicFiles.Add(new CPlaylistElement(path)); } _CurPlayer = _BGPlayer; //Set a default to have a consistent starting point, use SetMusicSource afterwards _MusicSource = EBackgroundMusicSource.TR_CONFIG_NO_OWN_MUSIC; _AddBackgroundMusic(); SetMusicSource(CConfig.Config.Sound.BackgroundMusicSource); _Initialized = true; }