我构建了一个选择 wav 文件的示例程序,然后您可以以 2x 速度或 4x 速度播放所选文件
上一个应用程序的代码是:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace PlayWave
{
public partial class Form1 : Form
{
private byte[] B;
private OpenFileDialog open;
public Form1()
{
open = new OpenFileDialog();
open.Filter = "Wav Sound File (*.Wav)|*.wav";
InitializeComponent();
}
private void SelectFile_Click(object sender, EventArgs e)
{
if (!(open.ShowDialog() == DialogResult.OK))
{
return;
}
}
private void twoX_Click(object sender, EventArgs e)
{
B = File.ReadAllBytes(open.FileName);
int samplerate = BitConverter.ToInt32(B, 24) * 2;
Array.Copy(BitConverter.GetBytes(samplerate), 0, B, 24, 4);
using (System.Media.SoundPlayer SP = new System.Media.SoundPlayer(new MemoryStream(B)))
{
SP.Play();
}
}
private void fourX_Click(object sender, EventArgs e)
{
B = File.ReadAllBytes(open.FileName);
int samplerate = BitConverter.ToInt32(B, 24) * 4;
Array.Copy(BitConverter.GetBytes(samplerate), 0, B, 24, 4);
using (System.Media.SoundPlayer SP = new System.Media.SoundPlayer(new MemoryStream(B)))
{
SP.Play();
}
}
}
}
上面,我更改了文件的 (25,26,27,28) 字节的值,它代表波形文件的采样率,然后保存更改并使用 System.Media.SoundPlayer 和 MemoryStream 播放文件。
我的问题是,当我单击按钮以 3 次单击以 2 倍速度播放文件时,我的程序停止并出现错误消息,谁能告诉我为什么?