/// <summary> /// 打开文件 /// </summary> /// <param name="filePath">文件路径</param> public bool OpenFile(string filePath) { //如果播放文件一样,没必要继续操作 if (_currentFilePath == filePath) { return(false); } //如果播放文件不存在直接退出 if (!File.Exists(filePath)) { return(false); } StopAndCloseStream(); try { _waveOutDevice = new WaveOut() { DesiredLatency = 100 }; if (GetTypeWithExtension(filePath) == AudioFileType.Mp3) { _activeStream = new Mp3FileReader(filePath); } else if (GetTypeWithExtension(filePath) == AudioFileType.Wav) { _activeStream = new WaveFileReader(filePath); } else { return(false); } _inputStream = new WaveChannel32(_activeStream); _sampleAggregator = new SampleAggregator(_fftDataSize); _inputStream.Sample += InputStreamOnSampleChanged; _waveOutDevice.Init(_inputStream); _currentFilePath = filePath; RefreshPlayingState(); return(true); } catch (Exception e) { _activeStream = null; Debug.WriteLine(e); throw; } }
public void OpenFile(string path) { Stop(); if (ActiveStream != null) { SelectionBegin = TimeSpan.Zero; SelectionEnd = TimeSpan.Zero; ChannelPosition = 0; } StopAndCloseStream(); if (System.IO.File.Exists(path)) { try { waveOutDevice = new WaveOut() { DesiredLatency = 100 }; var extension = Path.GetExtension(path); if (extension == null) { Debug.WriteLine(string.Format("只能播放有标准后缀名的文件,此文件{0}不标准", path)); return; } //判断是MP3文件还是Wav if (extension.Equals(".mp3", StringComparison.InvariantCultureIgnoreCase)) { ActiveStream = new Mp3FileReader(path); inputStream = new WaveChannel32(ActiveStream); sampleAggregator = new SampleAggregator(fftDataSize); inputStream.Sample += inputStream_Sample; waveOutDevice.Init(inputStream); ChannelLength = inputStream.TotalTime.TotalSeconds; //FileTag = TagLib.File.Create(path); FileTag获取专辑图片,不需要专辑图片 //GenerateWaveformData(path); CanPlay = true; } else if (extension.Equals(".wav", StringComparison.InvariantCultureIgnoreCase)) { ActiveStream = new WaveFileReader(path); inputStream = new WaveChannel32(ActiveStream); sampleAggregator = new SampleAggregator(fftDataSize); inputStream.Sample += inputStream_Sample; waveOutDevice.Init(inputStream); ChannelLength = inputStream.TotalTime.TotalSeconds; //FileTag = TagLib.File.Create(path); FileTag获取专辑图片,不需要专辑图片 //GenerateWaveformData(path); CanPlay = true; } //ActiveStream = new Mp3FileReader(path); //inputStream = new WaveChannel32(ActiveStream); //sampleAggregator = new SampleAggregator(fftDataSize); //inputStream.Sample += inputStream_Sample; //waveOutDevice.Init(inputStream); //ChannelLength = inputStream.TotalTime.TotalSeconds; ////FileTag = TagLib.File.Create(path); FileTag获取专辑图片,不需要专辑图片 //GenerateWaveformData(path); //CanPlay = true; } catch { ActiveStream = null; CanPlay = false; } } }
private void waveformGenerateWorker_DoWork(object sender, DoWorkEventArgs e) { WaveformGenerationParams waveformParams = e.Argument as WaveformGenerationParams; //var extension = Path.GetExtension(waveformParams.Path); //if (extension == null) //{ // Debug.WriteLine(string.Format("只能播放有标准后缀名的文件,此文件{0}不标准", path)); // return; //} ////判断是MP3文件还是Wav //if (extension.Equals(".mp3", StringComparison.InvariantCultureIgnoreCase)) //{ // ActiveStream = new Mp3FileReader(waveformParams.Path); // inputStream = new WaveChannel32(ActiveStream); // sampleAggregator = new SampleAggregator(fftDataSize); // inputStream.Sample += inputStream_Sample; // waveOutDevice.Init(inputStream); // ChannelLength = inputStream.TotalTime.TotalSeconds; // //FileTag = TagLib.File.Create(path); FileTag获取专辑图片,不需要专辑图片 // GenerateWaveformData(waveformParams.Path); // CanPlay = true; //} //else if (extension.Equals(".wav", StringComparison.InvariantCultureIgnoreCase)) //{ // ActiveStream = new WaveFileReader(waveformParams.Path); // inputStream = new WaveChannel32(ActiveStream); // sampleAggregator = new SampleAggregator(fftDataSize); // inputStream.Sample += inputStream_Sample; // waveOutDevice.Init(inputStream); // ChannelLength = inputStream.TotalTime.TotalSeconds; // //FileTag = TagLib.File.Create(path); FileTag获取专辑图片,不需要专辑图片 // GenerateWaveformData(waveformParams.Path); // CanPlay = true; //} WaveFileReader waveformMp3Stream = new WaveFileReader(waveformParams.Path); WaveChannel32 waveformInputStream = new WaveChannel32(waveformMp3Stream); waveformInputStream.Sample += waveStream_Sample; int frameLength = fftDataSize; int frameCount = (int)((double)waveformInputStream.Length / (double)frameLength); int waveformLength = frameCount * 2; byte[] readBuffer = new byte[frameLength]; waveformAggregator = new SampleAggregator(frameLength); float maxLeftPointLevel = float.MinValue; float maxRightPointLevel = float.MinValue; int currentPointIndex = 0; float[] waveformCompressedPoints = new float[waveformParams.Points]; List <float> waveformData = new List <float>(); List <int> waveMaxPointIndexes = new List <int>(); for (int i = 1; i <= waveformParams.Points; i++) { waveMaxPointIndexes.Add((int)Math.Round(waveformLength * ((double)i / (double)waveformParams.Points), 0)); } int readCount = 0; while (currentPointIndex * 2 < waveformParams.Points) { waveformInputStream.Read(readBuffer, 0, readBuffer.Length); waveformData.Add(waveformAggregator.LeftMaxVolume); waveformData.Add(waveformAggregator.RightMaxVolume); if (waveformAggregator.LeftMaxVolume > maxLeftPointLevel) { maxLeftPointLevel = waveformAggregator.LeftMaxVolume; } if (waveformAggregator.RightMaxVolume > maxRightPointLevel) { maxRightPointLevel = waveformAggregator.RightMaxVolume; } if (readCount > waveMaxPointIndexes[currentPointIndex]) { waveformCompressedPoints[(currentPointIndex * 2)] = maxLeftPointLevel; waveformCompressedPoints[(currentPointIndex * 2) + 1] = maxRightPointLevel; maxLeftPointLevel = float.MinValue; maxRightPointLevel = float.MinValue; currentPointIndex++; } if (readCount % 3000 == 0) { float[] clonedData = (float[])waveformCompressedPoints.Clone(); App.Current.Dispatcher.Invoke(new Action(() => { WaveformData = clonedData; })); } if (waveformGenerateWorker.CancellationPending) { e.Cancel = true; break; } readCount++; } float[] finalClonedData = (float[])waveformCompressedPoints.Clone(); App.Current.Dispatcher.Invoke(new Action(() => { fullLevelData = waveformData.ToArray(); WaveformData = finalClonedData; })); waveformInputStream.Close(); waveformInputStream.Dispose(); waveformInputStream = null; waveformMp3Stream.Close(); waveformMp3Stream.Dispose(); waveformMp3Stream = null; }