private bool CreateWaveStream() { OptReturn optReturn; if (mWaveStream == null) { return(false); } try { WaveStream waveStream = new CustomRateWaveStream(mWaveStream, mRate); var waveChannel = new SampleChannel(waveStream, true); var postVolumeMeter = new MeteringSampleProvider(waveChannel); mSampleProvider = postVolumeMeter; return(true); } catch (Exception ex) { optReturn = new OptReturn(); optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; ShowException(optReturn); return(false); } }
private void BtnStop_Click() { OptReturn optReturn; try { if (mWaveOut != null && mWaveStream != null) { TimeSpan ts = mWaveStream.CurrentTime; mWaveStream.Position = 0; mWaveOut.Stop(); mTimer.Stop(); LbCurrentTime = "00:00:00"; OnPlayerEventEvent(Defines.EVT_BTN_STOP, ts); } } catch (Exception ex) { optReturn = new OptReturn(); optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; ShowException(optReturn); } }
private void BtnOpen_Click() { OptReturn optReturn; try { OpenFileDialog opendDialog = new OpenFileDialog(); opendDialog.Filter = "Wave File(*.wav)|*.wav|MP3 File(*.mp3)|*.mp3|All File(*.*)|*.*"; var result = opendDialog.ShowDialog(); if (result == true) { string filePath = opendDialog.FileName; if (!System.IO.File.Exists(filePath)) { optReturn = new OptReturn(); optReturn.Code = Defines.RET_FILE_NOT_EXIST; optReturn.Message = filePath; ShowException(optReturn); return; } mUrl = filePath; Play(); } } catch (Exception ex) { optReturn = new OptReturn(); optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; ShowException(optReturn); } }
private void BtnFaster_Click() { OptReturn optReturn; try { if (mRate < 5.0) { mRate = mRate + 0.1; } if (!CreateWaveStream() || !CreateWaveOut()) { return; } if (mWaveOut != null) { mWaveOut.Play(); mTimer.Start(); } LbRate = mRate.ToString("0.0"); } catch (Exception ex) { optReturn = new OptReturn(); optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; ShowException(optReturn); } }
void BtnSlower_Click(object sender, RoutedEventArgs e) { try { if (mRate > 0.1) { mRate = mRate - 0.1; } if (!CreateWaveStream() || !CreateWaveOut()) { return; } if (mWaveOut != null) { mWaveOut.Play(); mTimer.Start(); } LbRate.Content = mRate.ToString("0.0"); } catch (Exception ex) { OptReturn optReturn = new OptReturn(); optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; ShowException(optReturn); } }
/// <summary> /// 绘制波形图 /// </summary> /// <returns></returns> public OptReturn Draw() { OptReturn optReturn = new OptReturn(); optReturn.Result = true; optReturn.Code = Defines.RET_SUCCESS; switch (mSourceType) { case WaveStreamType.LocalFile: if (!Path.IsPathRooted(mSrcPath)) { mSrcPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, mSrcPath); } if (!File.Exists(mSrcPath)) { optReturn.Result = false; optReturn.Code = Defines.RET_FILE_NOT_EXIST; optReturn.Message = string.Format("Source wave file not exist.\tpath:{0}", mSrcPath); return(optReturn); } try { WaveStream waveStream = new WaveFileReader(File.OpenRead(mSrcPath)); return(Draw(waveStream)); } catch (Exception ex) { optReturn.Result = false; optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; return(optReturn); } case WaveStreamType.NetworkStream: try { WaveStream waveStream = new NetWorkWaveReader(mSrcPath); return(Draw(waveStream)); } catch (Exception ex) { optReturn.Result = false; optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; return(optReturn); } default: optReturn.Result = false; optReturn.Code = Defines.RET_FAIL; optReturn.Message = string.Format("Media source type invalid.\t{0}", mSourceType); return(optReturn); } }
public void Play(WaveStream waveStream) { OptReturn optReturn; try { var reader = waveStream; if (reader.WaveFormat.Encoding != WaveFormatEncoding.Pcm && reader.WaveFormat.Encoding != WaveFormatEncoding.IeeeFloat) { reader = WaveFormatConversionStream.CreatePcmStream(reader); reader = new BlockAlignReductionStream(reader); } if (mWaveStream != null) { mWaveStream.Dispose(); } mWaveStream = reader; OnPlayerEventEvent(Defines.EVT_MEDIAOPENED, mWaveStream.TotalTime); if (!CreateWaveImage(mWaveStream)) { return; } LbTotalTime = GetTimeString(mWaveStream.TotalTime); LbCurrentTime = "00:00:00"; if (mSliderPosition != null) { mSliderPosition.Minimum = 0; mSliderPosition.Maximum = mWaveStream.TotalTime.TotalSeconds; } if (!CreateWaveOut()) { return; } if (mWaveOut != null) { mWaveOut.Play(); mTimer.Start(); OnPlayerEventEvent(Defines.EVT_PLAYBACKSTARTED, mUrl); } } catch (Exception ex) { optReturn = new OptReturn(); optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; ShowException(optReturn); } }
/// <summary> /// 使用指定的音频流绘制波形图 /// </summary> /// <param name="stream"></param> /// <returns></returns> public OptReturn Draw(WaveStream stream) { OptReturn optReturn = new OptReturn(); optReturn.Result = true; optReturn.Code = Defines.RET_SUCCESS; try { WaveStream reader = stream; reader.Position = 0; if (reader.WaveFormat.Encoding != WaveFormatEncoding.Pcm && reader.WaveFormat.Encoding != WaveFormatEncoding.IeeeFloat) { reader = WaveFormatConversionStream.CreatePcmStream(reader); reader = new BlockAlignReductionStream(reader); } InitDraw(); var waveChannel = new SampleChannel(reader, true); int samples = (int)(reader.TotalTime.TotalSeconds) * reader.WaveFormat.SampleRate / mPngWidth; var postVolumeMeter = new MeteringSampleProvider(waveChannel, samples); postVolumeMeter.StreamVolume += postVolumeMeter_StreamVolume; var waveProvider = new SampleToWaveProvider(postVolumeMeter); while (true) { byte[] buffer = new byte[waveProvider.WaveFormat.AverageBytesPerSecond]; int readcount = waveProvider.Read(buffer, 0, waveProvider.WaveFormat.AverageBytesPerSecond); if (readcount <= 0) { break; } } stream.Position = 0; if (mIsSaveFile) { return(GenerateImageFile()); } Bitmap[] bitmaps = new Bitmap[2]; bitmaps[0] = mBitmap; bitmaps[1] = mBitmap1; optReturn.Data = bitmaps; return(optReturn); } catch (Exception ex) { optReturn.Result = false; optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; return(optReturn); } }
void BtnClose_Click(object sender, RoutedEventArgs e) { try { SubClosing(); SubPlayerEvent(Defines.EVT_BTN_CLOSE, null); Close(); } catch (Exception ex) { OptReturn optReturn = new OptReturn(); optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; ShowException(optReturn); } }
void SliderPosition_LostMouseCapture(object sender, MouseEventArgs e) { try { if (mWaveStream != null) { mWaveStream.CurrentTime = TimeSpan.FromSeconds((int)SliderPosition.Value); } } catch (Exception ex) { OptReturn optReturn = new OptReturn(); optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; ShowException(optReturn); } }
private void BtnClose_Click() { OptReturn optReturn; try { OnPlayerEventEvent(Defines.EVT_BTN_CLOSE, null); Close(); } catch (Exception ex) { optReturn = new OptReturn(); optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; ShowException(optReturn); } }
private bool CreateWaveImage(WaveStream waveStream) { OptReturn optReturn; if (waveStream == null) { return(false); } try { WavePainter wavePainter = new WavePainter(); wavePainter.VolumnNum = 2; wavePainter.IsSaveFile = false; optReturn = wavePainter.Draw(waveStream); if (!optReturn.Result) { ShowException(optReturn); return(false); } var data = optReturn.Data as Bitmap[]; if (data != null) { var bitmap = data[0]; var imgsource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); if (mImageLeft != null) { mImageLeft.Source = imgsource; } bitmap = data[1]; imgsource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); if (mImageRight != null) { mImageRight.Source = imgsource; } } return(true); } catch (Exception ex) { optReturn = new OptReturn(); optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; ShowException(optReturn); return(false); } }
private OptReturn GenerateImageFile() { OptReturn optReturn = new OptReturn(); optReturn.Result = true; optReturn.Code = Defines.RET_SUCCESS; try { if (string.IsNullOrEmpty(mBitmapPath)) { optReturn.Result = false; optReturn.Code = Defines.RET_STRING_EMPTY; optReturn.Message = string.Format("BitmapPath empty."); return(optReturn); } if (File.Exists(mBitmapPath)) { File.Delete(mBitmapPath); } mBitmap.Save(mBitmapPath); if (mVolumnNum == 2) { if (string.IsNullOrEmpty(mBitmapPath1)) { optReturn.Result = false; optReturn.Code = Defines.RET_STRING_EMPTY; optReturn.Message = string.Format("BitmapPath empty."); return(optReturn); } if (File.Exists(mBitmapPath1)) { File.Delete(mBitmapPath1); } mBitmap1.Save(mBitmapPath1); } return(optReturn); } catch (Exception ex) { optReturn.Result = false; optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; return(optReturn); } }
/// <summary> /// 设定播放位置 /// </summary> /// <param name="ts"></param> public void SetPosition(TimeSpan ts) { OptReturn optReturn; try { if (mWaveStream != null) { mWaveStream.CurrentTime = ts; } } catch (Exception ex) { optReturn = new OptReturn(); optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; ShowException(optReturn); } }
public void SetPosition(TimeSpan ts) { OptReturn optReturn; try { int intValue = (int)ts.TotalSeconds; if (mWaveStream != null) { mWaveStream.CurrentTime = TimeSpan.FromSeconds(intValue); } } catch (Exception ex) { optReturn = new OptReturn(); optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; ShowException(optReturn); } }
void BtnPause_Click(object sender, RoutedEventArgs e) { try { if (mWaveOut != null && mWaveStream != null && mWaveOut.PlaybackState == PlaybackState.Playing) { mWaveOut.Pause(); mTimer.Stop(); } } catch (Exception ex) { OptReturn optReturn = new OptReturn(); optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; ShowException(optReturn); } }
void SliderVolume_LostMouseCapture(object sender, MouseEventArgs e) { try { double value = SliderVolume.Value; mVolumn = (float)Convert.ToDouble((value / 100).ToString("0.00")); if (mWaveOut != null) { mWaveOut.Volume = mVolumn; } LbVolume.Content = mVolumn.ToString("0.0"); } catch (Exception ex) { OptReturn optReturn = new OptReturn(); optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; ShowException(optReturn); } }
private void BtnPlay_Click() { OptReturn optReturn; try { if (mWaveOut != null && mWaveStream != null && mWaveOut.PlaybackState == PlaybackState.Paused) { mWaveOut.Play(); mTimer.Start(); } else if (mWaveOut != null && mWaveOut.PlaybackState == PlaybackState.Playing) { } else { if (mWaveStream != null) { //if (!CreateWaveStream() || !CreateWaveOut()) //{ // return; //} if (mWaveOut != null) { mWaveOut.Play(); mTimer.Start(); } } } OnPlayerEventEvent(Defines.EVT_BTN_PLAY, mUrl); } catch (Exception ex) { optReturn = new OptReturn(); optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; ShowException(optReturn); } }
public TimeSpan GetCurrentPosition() { OptReturn optReturn; try { if (mWaveStream != null) { TimeSpan ts = mWaveStream.CurrentTime; return(ts); } } catch (Exception ex) { optReturn = new OptReturn(); optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; ShowException(optReturn); } return(TimeSpan.FromSeconds(0)); }
private void BtnPause_Click() { OptReturn optReturn; try { if (mWaveOut != null && mWaveStream != null && mWaveOut.PlaybackState == PlaybackState.Playing) { mWaveOut.Pause(); mTimer.Stop(); } } catch (Exception ex) { optReturn = new OptReturn(); optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; ShowException(optReturn); } }
private bool CreateWaveOut() { OptReturn optReturn; if (mWaveStream == null) { return(false); } if (mSampleProvider == null) { return(false); } if (mWaveOut != null) { try { mWaveOut.Stop(); mWaveOut.Dispose(); mWaveOut = null; } catch { } } try { mWaveOut = new WaveOut(); mWaveOut.PlaybackStopped += mWaveOut_PlaybackStopped; mWaveOut.Init(new SampleToWaveProvider(mSampleProvider)); mWaveOut.ChannelMode = mChannelMode; return(true); } catch (Exception ex) { optReturn = new OptReturn(); optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; ShowException(optReturn); return(false); } }
void BtnChannelMode_Click(object sender, RoutedEventArgs e) { try { if (mChannelMode == WaveOutChannelMode.Default) { mChannelMode = WaveOutChannelMode.Left; BtnChannelMode.Content = "L"; ImgRight.Opacity = 0.5; ImgLeft.Opacity = 1; } else if (mChannelMode == WaveOutChannelMode.Left) { mChannelMode = WaveOutChannelMode.Right; BtnChannelMode.Content = "R"; ImgLeft.Opacity = 0.5; ImgRight.Opacity = 1; } else { mChannelMode = WaveOutChannelMode.Default; BtnChannelMode.Content = "D"; ImgLeft.Opacity = 1; ImgRight.Opacity = 1; } if (mWaveOut != null) { mWaveOut.ChannelMode = mChannelMode; } } catch (Exception ex) { OptReturn optReturn = new OptReturn(); optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; ShowException(optReturn); } }
void BtnStop_Click(object sender, RoutedEventArgs e) { try { if (mWaveOut != null && mWaveStream != null) { TimeSpan ts = mWaveStream.CurrentTime; mWaveStream.Position = 0; mWaveOut.Stop(); mTimer.Stop(); LbCurrentTime.Content = "00:00:00"; SubPlayerEvent(Defines.EVT_BTN_STOP, ts); } } catch (Exception ex) { OptReturn optReturn = new OptReturn(); optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; ShowException(optReturn); } }
private void BtnChannelMode_Click() { OptReturn optReturn; try { var btnChannelMode = mListRightButtons.FirstOrDefault(b => b.Name == "ChannelMode"); if (btnChannelMode == null) { return; } if (mChannelMode == WaveOutChannelMode.Default) { mChannelMode = WaveOutChannelMode.Left; btnChannelMode.Display = "L"; if (mImageLeft != null) { mImageLeft.Opacity = 1; } if (mImageRight != null) { mImageRight.Opacity = 0.5; } } else if (mChannelMode == WaveOutChannelMode.Left) { mChannelMode = WaveOutChannelMode.Right; btnChannelMode.Display = "R"; if (mImageLeft != null) { mImageLeft.Opacity = 0.5; } if (mImageRight != null) { mImageRight.Opacity = 1; } } else { mChannelMode = WaveOutChannelMode.Default; btnChannelMode.Display = "D"; if (mImageLeft != null) { mImageLeft.Opacity = 1; } if (mImageRight != null) { mImageRight.Opacity = 1; } } if (mWaveOut != null) { mWaveOut.ChannelMode = mChannelMode; } } catch (Exception ex) { optReturn = new OptReturn(); optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; ShowException(optReturn); } }
private void ShowException(OptReturn optReturn) { Dispatcher.Invoke(new Action(() => OnPlayerEventEvent(Defines.EVT_EXCEPTION, optReturn))); }
public void Play() { OptReturn optReturn; if (string.IsNullOrEmpty(mUrl)) { optReturn = new OptReturn(); optReturn.Code = Defines.RET_STRING_EMPTY; optReturn.Message = string.Format(("Url is empty")); ShowException(optReturn); return; } try { WaveStream reader; if (mUrl.StartsWith("http", StringComparison.InvariantCultureIgnoreCase)) { if (mUrl.EndsWith(".mp3", StringComparison.InvariantCultureIgnoreCase)) { reader = new Mp3NetworkStream(mUrl); } else { reader = new NetWorkWaveReader(mUrl); if (reader.WaveFormat.Encoding != WaveFormatEncoding.Pcm && reader.WaveFormat.Encoding != WaveFormatEncoding.IeeeFloat) { reader = WaveFormatConversionStream.CreatePcmStream(reader); reader = new BlockAlignReductionStream(reader); } } } else { if (mUrl.EndsWith(".mp3", StringComparison.InvariantCultureIgnoreCase)) { reader = new Mp3FileReader(mUrl); } else { reader = new WaveFileReader(mUrl); if (reader.WaveFormat.Encoding != WaveFormatEncoding.Pcm && reader.WaveFormat.Encoding != WaveFormatEncoding.IeeeFloat) { reader = WaveFormatConversionStream.CreatePcmStream(reader); reader = new BlockAlignReductionStream(reader); } } } if (mWaveStream != null) { mWaveStream.Dispose(); } mWaveStream = reader; OnPlayerEventEvent(Defines.EVT_MEDIAOPENED, mWaveStream.TotalTime); if (!CreateWaveImage(mWaveStream)) { return; } LbTotalTime = GetTimeString(mWaveStream.TotalTime); LbCurrentTime = "00:00:00"; if (mSliderPosition != null) { mSliderPosition.Minimum = 0; mSliderPosition.Maximum = mWaveStream.TotalTime.TotalSeconds; } if (!CreateWaveStream()) { return; } if (!CreateWaveOut()) { return; } if (mWaveOut != null) { mWaveOut.Play(); mTimer.Start(); OnPlayerEventEvent(Defines.EVT_PLAYBACKSTARTED, mUrl); } } catch (Exception ex) { optReturn = new OptReturn(); optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; ShowException(optReturn); } }