Lilf

落木
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

调用DirectX进行简单的多媒体编程系列(三)

Posted on 2009-12-04 11:48  落木  阅读(321)  评论(0编辑  收藏  举报

以上把基本的类写好了,下面就可以开始编写前台代码了。先来个截图


 直接上代码吧

 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);
            }
        }
    }
}