以上把基本的类写好了,下面就可以开始编写前台代码了。先来个截图
直接上代码吧
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 QBAudioLib;
namespace QBAudio
{
public partial class Form2 : Form
{
QBAudioCtrl qbCtrl;
DeviceStatus devStauts;
PlayFlags thisFlags;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)//初始化
{
//获得所有的音频设备
List<DeviceInfo> devInfo = QBDeviceHelper.GetDeviceList();
foreach (DeviceInfo dev in devInfo)
{
cBox_Devices.Items.Add(dev.DevId);
}
this.thisFlags = PlayFlags.Default;
}
private void btn_play_Click(object sender, EventArgs e)//播放
{
try
{
qbCtrl.Dev_Play(this.thisFlags);
devStauts = qbCtrl.GetDeviceStatus();
lbl_status.Text = "playing:" + devStauts.isPlaying.ToString() + "---looping:" + devStauts.isLooping + "---Terminated:" + devStauts.isTerminated;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btn_stop_Click(object sender, EventArgs e)//停止
{
try
{
qbCtrl.Dev_Stop();
devStauts = qbCtrl.GetDeviceStatus();
lbl_status.Text = "playing:" + devStauts.isPlaying.ToString() + "---looping:" + devStauts.isLooping + "---Terminated:" + devStauts.isTerminated;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btn_File_Click(object sender, EventArgs e)//选择播放文件
{
OpenFileDialog filedg = new OpenFileDialog();
filedg.Filter = "音视频文件(*.wav)|*.wav";
if (filedg.ShowDialog() == DialogResult.OK)
{
if (filedg.FileName != "" || filedg.FileName != string.Empty)
{
txt_File.Text = filedg.FileName;
}
}
}
private void cBox_Devices_SelectedIndexChanged(object sender, EventArgs e)//选择声卡
{
try
{
//thisDev = (DeviceInfo)cBox_Devices.SelectedItem;
Guid devID = (Guid)cBox_Devices.SelectedItem;
string fileName = txt_File.Text.Trim();
if (fileName == null || fileName == string.Empty)
{
MessageBox.Show("请选择播放的文件...");
return;
}
qbCtrl = new QBAudioCtrl(devID, fileName);
devStauts = qbCtrl.GetDeviceStatus();
lbl_status.Text = "playing:" + devStauts.isPlaying.ToString() + "---looping:" + devStauts.isLooping + "---Terminated:" + devStauts.isTerminated;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btn_pause_Click(object sender, EventArgs e)//暂停
{
try
{
qbCtrl.Dev_Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)//设置正常播放
{
if (radioButton1.Checked)
{
thisFlags = PlayFlags.Default;
btn_play_Click(sender, e);
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)//设置循环播放
{
if (radioButton2.Checked)
{
thisFlags = PlayFlags.Looping;
btn_play_Click(sender, e);
}
}
}
}
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QBAudioLib;
namespace QBAudio
{
public partial class Form2 : Form
{
QBAudioCtrl qbCtrl;
DeviceStatus devStauts;
PlayFlags thisFlags;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)//初始化
{
//获得所有的音频设备
List<DeviceInfo> devInfo = QBDeviceHelper.GetDeviceList();
foreach (DeviceInfo dev in devInfo)
{
cBox_Devices.Items.Add(dev.DevId);
}
this.thisFlags = PlayFlags.Default;
}
private void btn_play_Click(object sender, EventArgs e)//播放
{
try
{
qbCtrl.Dev_Play(this.thisFlags);
devStauts = qbCtrl.GetDeviceStatus();
lbl_status.Text = "playing:" + devStauts.isPlaying.ToString() + "---looping:" + devStauts.isLooping + "---Terminated:" + devStauts.isTerminated;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btn_stop_Click(object sender, EventArgs e)//停止
{
try
{
qbCtrl.Dev_Stop();
devStauts = qbCtrl.GetDeviceStatus();
lbl_status.Text = "playing:" + devStauts.isPlaying.ToString() + "---looping:" + devStauts.isLooping + "---Terminated:" + devStauts.isTerminated;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btn_File_Click(object sender, EventArgs e)//选择播放文件
{
OpenFileDialog filedg = new OpenFileDialog();
filedg.Filter = "音视频文件(*.wav)|*.wav";
if (filedg.ShowDialog() == DialogResult.OK)
{
if (filedg.FileName != "" || filedg.FileName != string.Empty)
{
txt_File.Text = filedg.FileName;
}
}
}
private void cBox_Devices_SelectedIndexChanged(object sender, EventArgs e)//选择声卡
{
try
{
//thisDev = (DeviceInfo)cBox_Devices.SelectedItem;
Guid devID = (Guid)cBox_Devices.SelectedItem;
string fileName = txt_File.Text.Trim();
if (fileName == null || fileName == string.Empty)
{
MessageBox.Show("请选择播放的文件...");
return;
}
qbCtrl = new QBAudioCtrl(devID, fileName);
devStauts = qbCtrl.GetDeviceStatus();
lbl_status.Text = "playing:" + devStauts.isPlaying.ToString() + "---looping:" + devStauts.isLooping + "---Terminated:" + devStauts.isTerminated;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btn_pause_Click(object sender, EventArgs e)//暂停
{
try
{
qbCtrl.Dev_Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)//设置正常播放
{
if (radioButton1.Checked)
{
thisFlags = PlayFlags.Default;
btn_play_Click(sender, e);
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)//设置循环播放
{
if (radioButton2.Checked)
{
thisFlags = PlayFlags.Looping;
btn_play_Click(sender, e);
}
}
}
}