windows 读取整个mp3文件 显示到带时间轴的波形显示控件代码

时间: 2024-05-01 21:23:47 浏览: 13
以下是一个使用NAudio库来读取MP3文件并在WinForms应用程序中显示波形的示例代码: ``` using NAudio.Wave; using System; using System.Drawing; using System.Windows.Forms; namespace WaveformViewer { public partial class Form1 : Form { private WaveFileReader waveFileReader; private WaveOut waveOut; private Bitmap bitmap; private Graphics graphics; private int bytesPerSample = 2; private int samplesPerPixel = 128; private Pen pen = new Pen(Color.Green, 1); public Form1() { InitializeComponent(); } private void openToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "MP3 Files (*.mp3)|*.mp3"; if (openFileDialog.ShowDialog() == DialogResult.OK) { waveFileReader = new WaveFileReader(openFileDialog.FileName); waveOut = new WaveOut(); waveOut.Init(waveFileReader); trackBar.Maximum = (int)waveFileReader.TotalTime.TotalSeconds; bitmap = new Bitmap(pictureBox.Width, pictureBox.Height); graphics = Graphics.FromImage(bitmap); } } private void playToolStripMenuItem_Click(object sender, EventArgs e) { if (waveOut != null) { waveOut.Play(); timer.Start(); } } private void pauseToolStripMenuItem_Click(object sender, EventArgs e) { if (waveOut != null) { waveOut.Pause(); timer.Stop(); } } private void stopToolStripMenuItem_Click(object sender, EventArgs e) { if (waveOut != null) { waveOut.Stop(); timer.Stop(); waveFileReader.Position = 0; } } private void timer_Tick(object sender, EventArgs e) { TimeSpan currentTime = waveOut.GetPositionTimeSpan(); labelCurrentTime.Text = currentTime.ToString(@"mm\:ss"); trackBar.Value = (int)currentTime.TotalSeconds; int bytesToRead = (int)(waveFileReader.WaveFormat.SampleRate * bytesPerSample * samplesPerPixel); byte[] waveData = new byte[bytesToRead]; int bytesRead = waveFileReader.Read(waveData, 0, bytesToRead); if (bytesRead > 0) { DrawWaveform(waveData, bytesRead); pictureBox.Image = bitmap; } } private void DrawWaveform(byte[] waveData, int bytesRead) { int samples = bytesRead / bytesPerSample; int bytesPerPixel = bytesPerSample * samplesPerPixel; int pixels = bytesRead / bytesPerPixel; for (int i = 0; i < pixels; i++) { float max = 0; float min = 0; for (int j = 0; j < samplesPerPixel; j++) { float sample = 0; if (bytesPerSample == 2) { sample = BitConverter.ToInt16(waveData, i * bytesPerPixel + j * bytesPerSample) / 32768f; } else if (bytesPerSample == 4) { sample = BitConverter.ToInt32(waveData, i * bytesPerPixel + j * bytesPerSample) / 2147483648f; } if (sample > max) { max = sample; } else if (sample < min) { min = sample; } } int x = i * samplesPerPixel; int y = (int)((1f - max) * pictureBox.Height / 2); int height = (int)((max - min) * pictureBox.Height / 2); graphics.DrawLine(pen, x, y, x, y + height); } } private void trackBar_ValueChanged(object sender, EventArgs e) { if (waveOut != null) { waveOut.Pause(); waveFileReader.Position = trackBar.Value * waveFileReader.WaveFormat.SampleRate * bytesPerSample; waveOut.Play(); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (waveOut != null) { waveOut.Dispose(); } if (waveFileReader != null) { waveFileReader.Dispose(); } if (graphics != null) { graphics.Dispose(); } if (bitmap != null) { bitmap.Dispose(); } } } } ``` 这个应用程序包含一个菜单,用于打开、播放、暂停和停止MP3文件。它还包含一个带有时间轴的 `TrackBar` 控件,可以用来跳转到文件中的特定位置。当播放时,应用程序会定期从文件中读取一些数据,并将其显示为波形。`samplesPerPixel` 变量控制每个像素所表示的音频样本数,它越大,波形就越平滑。请注意,该示例代码仅适用于16位或32位PCM编码的音频文件,如果您要处理其他编码的文件,需要相应地修改代码。

相关推荐

最新推荐

recommend-type

C#实现读取DataSet数据并显示在ListView控件中的方法

主要介绍了C#实现读取DataSet数据并显示在ListView控件中的方法,涉及C#操作DataSet及ListView控件的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
recommend-type

Python3和pyqt5实现控件数据动态显示方式

今天小编就为大家分享一篇Python3和pyqt5实现控件数据动态显示方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

Java从数据库中读取Blob对象图片并显示的方法

主要介绍了Java从数据库中读取Blob对象图片并显示的方法,实例分析了Java读取数据库中Blob对象图片的技巧与操作方法,需要的朋友可以参考下
recommend-type

Android读取assets目录下的所有图片并显示的方法

主要介绍了Android读取assets目录下的所有图片并显示的方法,对读取assets文件夹的原理与方法做了详细的分析,并给出了读取图片文件的实例,非常具有参考借鉴价值,需要的朋友可以参考下
recommend-type

vue读取本地的excel文件并显示在网页上方法示例

主要介绍了vue读取本地的excel文件并显示在网页上方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。