首页 新闻 会员 周边 捐助

MCI 在一个线程中启动一个音频 在另一个线程中 终止它??如何做到啊?

0
悬赏园豆:50 [待解决问题]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Audio a = new Audio();
            Thread t1 = new Thread(new ThreadStart(a.Play));
            t1.IsBackground = false;
            t1.Start();

            Thread t2 = new Thread(new ThreadStart(a.Stop));
            t2.IsBackground = false;
            t2.Start();
        }
    }

    public class Audio
    {
        [DllImport("winmm.dll")]
        private static extern int mciSendString
            (
                string lpstrCommand,
                string lpstrReturnString,
                int uReturnLength,
                int hwndCallback
            );

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern int GetShortPathName
            (
                [MarshalAs(UnmanagedType.LPTStr)]    string path,
                 [MarshalAs(UnmanagedType.LPTStr)]    StringBuilder shortPath,
                 int shortPathLength
            );

        public Audio()
        {

        }

        public void Play()
        {
            string FileName = @"C:\Users\Administrator\Desktop\back0.wav";
            StringBuilder shortPathTemp = new StringBuilder(255);
            int result = GetShortPathName(FileName, shortPathTemp, shortPathTemp.Capacity);
            string ShortPath = shortPathTemp.ToString();
            mciSendString("open " + ShortPath + " alias song", "", 0, 0);
            mciSendString("play song", "", 0, 0);
        }

        public void Stop()
        {
            Thread.Sleep(3000);
            mciSendString("stop song", "", 0, 0);
        }
    }
}

 

代码如上所示, 我想在 t2 线程中中止 t1 线程正在播放的音乐,该如何做到。求指导。

MCI
alleyoop的主页 alleyoop | 初学一级 | 园豆:152
提问于:2012-06-17 10:40
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册