• 大小: 6.95MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-10-01
  • 语言: C#
  • 标签: 控制音量  winmm.dll  

资源简介

C#控制 指定系统音频输出设备播放音乐,C#控制音量大小,C# 调用 winmm.dll

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace WaveLib
{
    public class WaveMain
    {
        private WaveOutPlayer m_Player;
        private Stream m_AudioStream;
        private WaveFormat m_Format;
        private int _DevId;

        public int DevId
        {
            get { return _DevId; }
            set { _DevId = value; }
        }
        public WaveMain()
        {
            CloseFile();
            try
            {
                WaveStream S = new WaveStream(“Data/ringin.wav“);
                if (S.Length <= 0)
                    throw new Exception(“Invalid WAV file“);
                m_Format = S.Format;
                if (m_Format.wFormatTag != (short)WaveFormats.Pcm && m_Format.wFormatTag != (short)WaveFormats.Float)
                    throw new Exception(“Olny PCM files are supported“);
                m_AudioStream = S;
            }
            catch (Exception e)
            {
                CloseFile();
            }
        }
        private void Filler(IntPtr data int size)
        {
            byte[] b = new byte[size];
            if (m_AudioStream != null)
            {
                int pos = 0;
                while (pos < size)
                {
                    int toget = size - pos;
                    int got = m_AudioStream.Read(b pos toget);
                    if (got < toget)
                        m_AudioStream.Position = 0; // loop if the file ends
                    pos += got;
                }
            }
            else
            {
                for (int i = 0; i < b.Length; i++)
                    b[i] = 0;
            }
            System.Runtime.InteropServices.Marshal.Copy(b 0 data size);
        }
        public void Play()
        {
            Stop();
            if (m_AudioStream != null)
            {
                m_AudioStream.Position = 0;
                m_Player = new WaveOutPlayer(DevId m_Format 16384 3 new BufferFillEventHandler(Filler));
            }
        }

        public void Stop()
        {
            if (m_Player != null)
                try
                {
                    m_Player.Dispose();
                }
                finally
                {
                    m_Player = null;
                }
        }
        private void CloseFile()
        {
            Stop();
            if (m_AudioStream != null)
                try
                {
                    m_AudioStream.Close();
                }
                finally
                {
                    m_AudioStream = null;
                }
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2013-01-04 14:42  振铃测试\
     目录           0  2013-01-07 13:38  振铃测试\WaveLib\
     目录           0  2013-01-04 14:42  振铃测试\WaveLib\bin\
     目录           0  2013-01-04 10:47  振铃测试\WaveLib\bin\Debug\
     目录           0  2013-01-04 14:42  振铃测试\WaveLib\bin\Release\
     文件       14848  2013-01-07 13:38  振铃测试\WaveLib\bin\Release\WaveLib.dll
     文件       38400  2013-01-07 13:38  振铃测试\WaveLib\bin\Release\WaveLib.pdb
     目录           0  2013-01-04 14:42  振铃测试\WaveLib\obj\
     目录           0  2013-01-04 14:42  振铃测试\WaveLib\obj\Debug\
     文件        5651  2013-01-04 10:47  振铃测试\WaveLib\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     目录           0  2013-01-04 10:47  振铃测试\WaveLib\obj\Debug\TempPE\
     目录           0  2013-01-07 13:38  振铃测试\WaveLib\obj\Release\
     文件        5655  2013-01-04 14:38  振铃测试\WaveLib\obj\Release\DesignTimeResolveAssemblyReferencesInput.cache
     文件      100112  2013-01-07 12:49  振铃测试\WaveLib\obj\Release\ResolveAssemblyReference.cache
     目录           0  2013-01-04 10:47  振铃测试\WaveLib\obj\Release\TempPE\
     文件         743  2013-01-04 15:04  振铃测试\WaveLib\obj\Release\WaveLib.csproj.FileListAbsolute.txt
     文件       14848  2013-01-07 13:38  振铃测试\WaveLib\obj\Release\WaveLib.dll
     文件       38400  2013-01-07 13:38  振铃测试\WaveLib\obj\Release\WaveLib.pdb
     目录           0  2013-01-04 14:42  振铃测试\WaveLib\Properties\
     文件        1364  2013-01-04 10:47  振铃测试\WaveLib\Properties\AssemblyInfo.cs
     文件        2378  2013-01-04 15:04  振铃测试\WaveLib\WaveLib.csproj
     文件        2761  2013-01-04 11:20  振铃测试\WaveLib\WaveMain.cs
     文件        5333  2013-01-07 13:38  振铃测试\WaveLib\WaveNative.cs
     文件        8264  2013-01-04 14:26  振铃测试\WaveLib\WaveOut.cs
     文件        4574  2013-01-04 11:15  振铃测试\WaveLib\WaveStream.cs
     目录           0  2013-01-07 15:05  振铃测试\WaveTest\
     目录           0  2013-01-04 14:57  振铃测试\WaveTest\bin\
     目录           0  2013-01-04 14:56  振铃测试\WaveTest\bin\Debug\
     目录           0  2013-01-04 14:56  振铃测试\WaveTest\bin\Debug\Data\
     文件     2155792  2013-01-04 14:55  振铃测试\WaveTest\bin\Debug\Data\ringin.wav
     文件       15360  2013-01-04 14:56  振铃测试\WaveTest\bin\Debug\WaveLib.dll
............此处省略51个文件信息

评论

共有 条评论