按键时我需要播放声音或声音,当按键不再按下时,声音会立即停止。
我目前正在使用BASS.net声音库,声音就像这样调用:
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);
}
}
private void setupPlayer()
{
stream1 = Foo.GetStream1(path1.Text);
stream2 = Foo.GetStream2(path2.Text);
}
//when the key1 is down/something to handle when the key is down
Bass.BASS_Init(1, 44100, BASSInit.BASS_DEVICE_DEFAULT, this.Handle);
Bass.BASS_SetDevice(1);
Bass.BASS_ChannelPlay(Foo.GetStream1(path1.Text), false);
//when the key1 is up/something to handle when the key is up
Bass.BASS_StreamFree(Foo.GetStream1(path1.Text));
Bass.BASS_SetDevice(1);
Bass.BASS_Free();
//when the key2 is down/something to handle when the key is down
Bass.BASS_Init(2, 44100, BASSInit.BASS_DEVICE_DEFAULT, this.Handle);
Bass.BASS_SetDevice(2);
Bass.BASS_ChannelPlay(Foo.GetStream1(path2.Text), false);
//when the key2 is up/something to handle when the key is up
Bass.BASS_StreamFree(Foo.GetStream2(path2.Text));
Bass.BASS_SetDevice(2);
Bass.BASS_Free();
正如我所说,当按键播放声音时,当按键启动时声音停止。此外,您可以按下多个键,文件将独立于按下或释放的另一个键播放,换句话说,按下和释放键不应影响同时按下或释放的其他键。
答案 0 :(得分:0)
不确定这是否是您的目标,但.NET框架允许控制(窗口组件)事件。其中一个事件是OnKeyDown。在该方法中,您将获得KeyEventArgs e参数的视图。该参数可以告诉您通过e.KeyCode
按下了哪个键,然后您可以将该键与预设键进行比较,以确定要播放的音乐。话虽如此,在不同时间按住多个键将触发此事件的另一个实例,这将解决您的第二个请求。