public MusicPlayer(string filePath) { FileInfo fi = new FileInfo(filePath); if (!fi.Exists) { throw new FileNotFoundException("找不到音乐文件…", fi.FullName); } _device = new WaveOutEvent { DesiredLatency = 80 }; _device.PlaybackStopped += (sender, args) => { PlayStatus = PlayStatusEnum.Stopped; }; _audioFile = new MyAudioFileReader(filePath); _device.Init(_audioFile); PlayStatus = PlayStatusEnum.Ready; Task.Run(() => { while (!_cts.IsCancellationRequested) { if (PlayStatus != PlayStatusEnum.NotInitialized && _audioFile != null) { _audioFile.Volume = Core.Config.Volume.Main * Core.Config.Volume.Music; PlayTime = (int)_audioFile?.CurrentTime.TotalMilliseconds; } Thread.Sleep(10); } }); }
public void Dispose() { _cts.Cancel(); _device?.Dispose(); _device = null; _audioFile?.Dispose(); _audioFile = null; _cts?.Dispose(); }