WINMM.DLL使用C#MP3播放器 [英] C# MP3 Player using winmm.dll

查看:178
本文介绍了WINMM.DLL使用C#MP3播放器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的午餐时间来砸在一起(非常)粗略的MP3播放器,到目前为止,我找到了发挥的文件,我正在建设的文件名的列表的方法使随机的歌曲,但我想我刚刚遇到了障碍。



有没有时,当前播放的MP3已经完成知道的一种方式?事件或一些这样?按照现在的情况,我不认为我能有播放列表等,除非这是可能的,因为每播放后停止。



我attatched整,低于源,随意拆开捡起来,给我你的任何反馈,欢呼声

 使用系统; 
:使用System.IO;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;使用System.Windows.Forms的
;使用System.Runtime.InteropServices
;

命名空间X
{
公共部分Form1类:表格
{
名单,LT;字符串>名称=新的List<串GT;();
StreamReader的读者= File.OpenText(@C:\X.txt);
串线;
打开文件对话框OFD =新的OpenFileDialog();
StringBuilder的缓冲=新的StringBuilder(128);
串CommandString中;
函数[DllImport(WINMM.DLL)]
私人静态外部长mciSendString(字符串lpstrCommand,StringBuilder的lpstrReturnString,诠释uReturnLength,诠释hwndCallback);
公共Form1中()
{
的InitializeComponent();
,而((行= reader.ReadLine())!= NULL)
{
如果(line.Trim()!=)
{
名称。新增(line.Trim());
}
}
}

私人无效btnplay_Click(对象发件人,EventArgs五)
{
如果(ofd.FileName == )
{
如果(ofd.ShowDialog()== DialogResult.OK)
{
ofd.Filter =MP3文件| * .MP3
CommandString中=打开+\+ ofd.FileName +\+型MPEGVideo别名Mp3File;
mciSendString(CommandString中,NULL,0,0);
CommandString中=玩Mp3File
mciSendString(CommandString中,NULL,0,0);
}
}

,否则
{
CommandString中=玩Mp3File
mciSendString(CommandString中,NULL,0,0);
}
}

私人无效btnpause_Click(对象发件人,EventArgs五)
{
CommandString中=暂停mp3file
mciSendString(CommandString中,NULL,0,0);
}

私人无效btnbrowse_Click(对象发件人,EventArgs五)
{
ofd.Filter =MP3文件| * .MP3
如果(ofd.ShowDialog()== DialogResult.OK)
{
txtpath.Text = ofd.FileName;
CommandString中=亲密Mp3File
mciSendString(CommandString中,NULL,0,0);
CommandString中=打开+\+ ofd.FileName +\+型MPEGVideo别名Mp3File;
mciSendString(CommandString中,NULL,0,0);
}
}
}
}


解决方案

您可以从mcisendstring当您打开该文件调用mcisendstring命令只发送表单的处理得到通知,并覆盖窗体的WndProc方法则u可以从MCI示例代码的通知作为follow.`

 私人无效btnplay_Click(对象发件人,EventArgs五)
{
如果(OFD。文件名==)
{
如果(ofd.ShowDialog()== DialogResult.OK)
{
ofd.Filter =MP3文件| * .MP3
CommandString中=打开+\+ ofd.FileName +\+型MPEGVideo别名Mp3File;
mciSendString(CommandString中,NULL,0,this.Handle.ToInt64());
CommandString中=玩Mp3File
mciSendString(CommandString中,NULL,0,this.Handle.ToInt64());
}
}

,否则
{
CommandString中=玩Mp3File
mciSendString(CommandString中,NULL,0,this.Handle.ToInt64());
}
}

//声明nofify不变
公共const int的MM_MCINOTIFY = 953;

//覆盖形式
保护覆盖无效的WndProc(参考消息M)
{

中的WndProc函数,如果(m.Msg == MM_MCINOTIFY)
{
//该文件播放完毕,做什么
}
base.WndProc(REF米);
}


I'm trying to bash together a (very) rough MP3 player during my lunch hour, and so far I've got it to play the files, and I'm working on a way of building a list of filenames to enable random songs, but I think I've just hit a snag.

Is there a way of knowing when the currently playing MP3 has finished? An event or some such? As it stands I don't think I'd be able to have playlists etc unless this was possible due to it stopping after every playback.

I've attatched the whole source below, feel free to pick it apart and give me any feedback you may have, cheers.

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace X
{
public partial class Form1 : Form
{
    List<string> Names = new List<string>();
    StreamReader reader = File.OpenText(@"C:\X.txt");
    string line;
    OpenFileDialog ofd = new OpenFileDialog();
    StringBuilder buffer = new StringBuilder(128);
    string CommandString;
    [DllImport("winmm.dll")]
    private static extern long mciSendString(string lpstrCommand, StringBuilder lpstrReturnString, int uReturnLength, int hwndCallback);
    public Form1()
    {
        InitializeComponent();
        while ((line = reader.ReadLine()) != null)
        {
            if (line.Trim() != "")
            {
                Names.Add(line.Trim());
            }
        }
    }

    private void btnplay_Click(object sender, EventArgs e)
    {
        if (ofd.FileName == "")
        {
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                ofd.Filter = "MP3 Files|*.mp3";
                CommandString = "open " + "\"" + ofd.FileName + "\"" + " type MPEGVideo alias Mp3File";
                mciSendString(CommandString, null, 0, 0);
                CommandString = "play Mp3File";
                mciSendString(CommandString, null, 0, 0);
            }
        }

        else
        {
            CommandString = "play Mp3File";
            mciSendString(CommandString, null, 0, 0);
        }
    }

    private void btnpause_Click(object sender, EventArgs e)
    {
        CommandString = "pause mp3file";
        mciSendString(CommandString, null, 0, 0);
    }

    private void btnbrowse_Click(object sender, EventArgs e)
    {
        ofd.Filter = "Mp3 files |*.mp3";
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            txtpath.Text = ofd.FileName;
            CommandString = "close Mp3File";
            mciSendString(CommandString, null, 0, 0);
            CommandString = "open " + "\"" + ofd.FileName + "\"" + " type MPEGVideo alias Mp3File";
            mciSendString(CommandString, null, 0, 0);
        }
    }
}
}

解决方案

You can get notification from mcisendstring command when you calling mcisendstring for opening the file just send the handle of your form and override the wndproc method of your form then u can get the notify from MCI sample code as follow.`

 private void btnplay_Click(object sender, EventArgs e)
{
    if (ofd.FileName == "")
    {
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            ofd.Filter = "MP3 Files|*.mp3";
            CommandString = "open " + "\"" + ofd.FileName + "\"" + " type MPEGVideo alias Mp3File";
            mciSendString(CommandString, null, 0, this.Handle.ToInt64());
            CommandString = "play Mp3File";
            mciSendString(CommandString, null, 0, this.Handle.ToInt64());
        }
    }

    else
    {
        CommandString = "play Mp3File";
        mciSendString(CommandString, null, 0, this.Handle.ToInt64());
    }
}

// Declare the nofify constant
public const int MM_MCINOTIFY = 953;

// Override the WndProc function in the form
protected override void WndProc(ref Message m) 
{

    if (m.Msg == MM_MCINOTIFY)
    {
        // The file is done playing, do whatever
    }
    base.WndProc(ref m);
}

这篇关于WINMM.DLL使用C#MP3播放器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆