zoukankan      html  css  js  c++  java
  • 调用DirectX进行简单的多媒体编程系列(四)

    但上面的例子中,只能播放单一文件,如果要是多个文件选择多个声卡同时播放呢?想到使用多线程来实现,再次封装多线程播放类

     

    AudioPalyer类,有三个事件,当播放时触发OnAudioPlay事件,当循环播放时触发OnLoopingPlay事件,当正常播放时触发OnNormalPlay事件,代码如下:

     using System;

    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using QBAudioLib;
    using System.Threading;

    namespace QBAudioThread
    {
        
    public struct playInfo
        {
            
    public Guid devId;
            
    public string fileName;
            
    public PlayFlags thisFlag;
        }
        
    public class AudioEventArgs : EventArgs
        {
            
    public playInfo AudioPlayInfo;
        }
        
    public class AudioPlayer
        {
            QBAudioCtrl qbCtrl;
            DeviceStatus devStauts;
            playInfo thisInfo;
            
    public delegate void DefEventHandler(object sender,AudioEventArgs args);
            
    public event DefEventHandler OnAudioPlay;//开始播放事件
            public event EventHandler OnNormalPaly;//默认正常播放时事件
            public event EventHandler OnLoopingPaly;//循环播放时事件
            public AudioPlayer(playInfo iPlay)
            {
                thisInfo 
    = iPlay;
            }
            
    public void Audio_Play()
            {
                Monitor.Enter(
    this);
                qbCtrl 
    = new QBAudioCtrl(thisInfo.devId, thisInfo.fileName);
                qbCtrl.Dev_Play(thisInfo.thisFlag);
                devStauts 
    = qbCtrl.GetDeviceStatus();
                
    //开始播放激发的事件
                if (devStauts.isPlaying == true)
                {
                    AudioEventArgs thisArgs 
    = new AudioEventArgs();
                    thisArgs.AudioPlayInfo 
    = thisInfo;
                    OnAudioPlay(
    this, thisArgs);
                }
                
    if (devStauts.isLooping == false)
                {
    //默认播放激发的事件
                    OnNormalPaly(thisnew EventArgs());
                }
                
    else
                {
    //循环播放激发的事件
                    OnLoopingPaly(thisnew EventArgs());
                }
                Monitor.Exit(
    this);
                Thread.Sleep(
    1000);

            }
        }
    }

     界面实现:

     

    代码
    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;
    using System.Threading;


    namespace QBAudioThread
    {
        
    public partial class Form2 : Form
        {
            PlayFlags thisFlags;
            DataTable dt 
    = new DataTable();
            Thread threadPlay;

            
    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(
    new KeyValuePair<Guid, string>(dev.DevId, dev.Description));
                }
                cBox_Devices.SelectedIndex 
    = 0;

                
    this.thisFlags = PlayFlags.Default;


                dt.Columns.Add(
    "devId"typeof(Guid));
                dt.Columns.Add(
    "fileName"typeof(string));
                dt.Columns.Add(
    "thisFlag"typeof(PlayFlags));

                dataGridView1.DataSource 
    = dt;

            }

            
    private void btn_play_Click(object sender, EventArgs e)//播放
            {
                
    try
                {
                    
    int i = 0;
                    
    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                        
    if ((bool)row.Cells[0].FormattedValue == true)
                        {
                            playInfo thisPlay 
    = new playInfo();
                            thisPlay.devId 
    = (Guid)row.Cells["devId"].Value;
                            thisPlay.fileName 
    = row.Cells["fileName"].Value.ToString();
                            thisPlay.thisFlag 
    = (PlayFlags)row.Cells["thisFlag"].Value;
                            AudioPlayer myPlayer 
    = new AudioPlayer(thisPlay);
                            myPlayer.OnAudioPlay 
    += new AudioPlayer.DefEventHandler(myPlayer_OnAudioPlay);
                            myPlayer.OnLoopingPaly 
    += new EventHandler(myPlayer_OnLoopingPaly);
                            myPlayer.OnNormalPaly 
    += new EventHandler(myPlayer_OnNormalPaly);
                            threadPlay 
    = new Thread(new ThreadStart(myPlayer.Audio_Play));
                            threadPlay.Name 
    = "player_" + i.ToString();

                            threadPlay.Start();
                            threadPlay.IsBackground 
    = true;
                            i
    ++;

                        }
                    }
                }
                
    catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            
    void myPlayer_OnAudioPlay(object sender,AudioEventArgs args)
            {
                playInfo myplay 
    = (playInfo)args.AudioPlayInfo;
                
    string msg = myplay.devId.ToString() + "\r\n正在播放\r\n" + myplay.fileName + "\r\n";
                MessageBox.Show(msg);
            }



            
    void myPlayer_OnNormalPaly(object sender, EventArgs e)
            {
                MessageBox.Show(
    "Normal");

            }

            
    void myPlayer_OnLoopingPaly(object sender, EventArgs e)
            {
                
    //播放一分钟后终止线程
                MessageBox.Show("Looping");
            }




            
    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)//选择声卡
            {

            }




            
    private void radioButton1_CheckedChanged(object sender, EventArgs e)//选择默认播放
            {
                
    if (radioButton1.Checked)
                {
                    thisFlags 
    = PlayFlags.Default;
                }
            }

            
    private void radioButton2_CheckedChanged(object sender, EventArgs e)//选择循环播放
            {
                
    if (radioButton2.Checked)
                {
                    thisFlags 
    = PlayFlags.Looping;
                }
            }

            
    private void btn_add_Click(object sender, EventArgs e)//添加进列表
            {
                DataRow dr 
    = dt.NewRow();
                dr[
    "devId"= ((KeyValuePair<Guid, string>)cBox_Devices.SelectedItem).Key;
                dr[
    "fileName"= txt_File.Text.Trim();
                dr[
    "thisFlag"= thisFlags;
                dt.Rows.Add(dr);
                dataGridView1.DataSource 
    = dt;

            }

            
    private void btn_clear_Click(object sender, EventArgs e)//清除列表
            {
                dt.Rows.Clear();
                dataGridView1.DataSource 
    = dt;
            }


        }
    }

    全文文档下载:/Files/xnxylf/调用DirectX进行简单的多媒体编程系列.doc

    代码下载:/Files/xnxylf/QBAudioSolution.rar

  • 相关阅读:
    kettle的转换组件
    kettle的输出组件
    js的touch事件的实际引用
    touchstart,touchmove,touchend触摸事件的小小实践心得
    Android使用ViewPager实现左右循环滑动及轮播效果
    jQuery漂亮图标的垂直导航菜单
    c#使用WebClient登录网站抓取登录后的网页
    从网页抓取数据的一般方法
    c#通过oledb获取excel文件表结构信息
    c#中高效的excel导入sqlserver的方法
  • 原文地址:https://www.cnblogs.com/xnxylf/p/1616890.html
Copyright © 2011-2022 走看看