1

私は昼休み中に (非常に) ラフな MP3 プレーヤーを一緒にバッシュしようとしています。これまでのところ、ファイルを再生するためにそれを手に入れました。ランダムな曲を有効にするファイル名のリストを作成する方法に取り組んでいます。 、しかし、私は思わぬ障害にぶつかったと思います。

現在再生中の MP3 がいつ終了したかを知る方法はありますか? イベントか何か?現状では、再生するたびに停止するために可能でない限り、プレイリストなどを作成することはできないと思います。

以下にソース全体を添付しました。自由に選択して、フィードバックをお寄せください。乾杯.

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);
        }
    }
}
}
4

1 に答える 1

2

ファイルを開くために mcisendstring を呼び出すと、mcisendstring コマンドから通知を取得できます。フォームのハンドルを送信し、フォームの wndproc メソッドをオーバーライドすると、次のように MCI サンプル コードから通知を取得できます。

 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);
}
于 2010-03-02T16:50:04.660 に答える