将卷添加到媒体文件

时间:2017-01-21 21:21:33

标签: c# winforms

我制作了一个能够播放这样的mp3文件的程序:

class Mp3Player : IDisposable
{
    public bool Repeat { get; set; }

    public Mp3Player(string fileName)
    {
        const string FORMAT = @"open ""{0}"" type mpegvideo alias MediaFile";
        string command = String.Format(FORMAT, fileName);
        mciSendString(command, null, 0, IntPtr.Zero);
    }

    public void Play()
    {
        string command = "play MediaFile";
        if (Repeat) command += " REPAT";
        mciSendString(command, null, 0, IntPtr.Zero);
    }

    public void Stop()
    {
        string command = "stop MediaFile";
        mciSendString(command, null, 0, IntPtr.Zero);
    }

    [DllImport("winmm.dll")]
    private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);

    public void Dispose()
    {
        string command = "close MediaFile";
        mciSendString(command, null, 0, IntPtr.Zero);
    }
}

我想将音量栏添加到我的媒体文件中:

如果我走向右边使声音更响,否则我会向右走。

1 个答案:

答案 0 :(得分:0)

要设置音量,请尝试这样的事情:

 public void SetVolume(int volume)
 {
    var command = "setaudio MediaFile volume to " + volume.ToString();
    mciSendString(command, null, 0, IntPtr.Zero);
 }

您可以在SetVolume

中使用 0到1000 之间的值

要在应用程序中使用它,您需要在更改音量控制值时调用set volume。

让我们假设在玩家表单上你有Trackbar控件然后你需要使用滚动事件。例如:

private void trackBarVolume_Scroll(object sender, EventArgs e)
{
     _mp3Player.SetVolume(trackBarVolume.Value);
}

填写表格类:

    public partial class Form1 : Form
    {
        private Mp3Player _mp3Player = new Mp3Player(@"C:\music.mp3");

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _mp3Player.Play();
        }


        private void trackBarVolume_Scroll(object sender, EventArgs e)
        {
            _mp3Player.SetVolume(trackBarVolume.Value);
        }
    }

屏幕:

form with trackbar control