我正在制作一个应用程序,它在按下键时播放声音,当松开键时声音立即停止。
我发现的问题是,如果你按下一个键,它会播放它的前几毫秒,然后无限循环它,直到你释放这个键。
我目前使用的代码如下:
public class Foo
{
public static int GetStream1(string path)
{
return Bass.BASS_StreamCreateFile(path, 0, 0, BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_PRESCAN);
}
public static int GetStream2(string path)
{
return Bass.BASS_StreamCreateFile(path, 0, 0, BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_PRESCAN);
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
Bass.BASS_Init(1, 44100, BASSInit.BASS_DEVICE_DEFAULT, this.Handle);
Bass.BASS_Init(2, 44100, BASSInit.BASS_DEVICE_DEFAULT, this.Handle);
if (e.KeyCode == Keys.D1)
{
if (beatload1.Text == "Waiting 01.wav")
{
MessageBox.Show("No beat loaded");
return;
}
Beat1.Image = Beatpadpc.Properties.Resources.white_square_button;
try
{
Bass.BASS_SetDevice(1);
Bass.BASS_ChannelPlay(Foo.GetStream1(path1.Text), false);
}
catch (FileNotFoundException)
{
MessageBox.Show("File has been moved." + "\n" + "Please relocate it now!");
}
}
if (e.KeyCode == Keys.D2)
{
if (beatload2.Text == "Waiting 02.wav")
{
MessageBox.Show("No beat loaded");
return;
}
Beat2.Image = Beatpadpc.Properties.Resources.white_square_button;
try
{
Bass.BASS_SetDevice(2);
Bass.BASS_ChannelPlay(Foo.GetStream2(path2.Text), false);
}
catch (FileNotFoundException)
{
MessageBox.Show("File has been moved." + "\n" + "Please relocate it now!");
}
}
}
private void Window_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D1)
{
Beat1.Image = Beatpadpc.Properties.Resources.black_square_button;
Bass.BASS_StreamFree(Foo.GetStream1(path1.Text));
Bass.BASS_SetDevice(1);
Bass.BASS_Free();
}
if (e.KeyCode == Keys.D2)
{
Beat2.Image = Beatpadpc.Properties.Resources.black_square_button;
Bass.BASS_StreamFree(Foo.GetStream2(path2.Text));
Bass.BASS_SetDevice(2);
Bass.BASS_Free();
}
}
那么有没有办法同时播放 1 个或多个声音而不会永远循环播放?