0

我目前正在使用 C# 开发 MP3 播放器。我是初学者。我已经能够开发出具有最小功能的普通 MP3 播放器,例如打开文件、暂停、播放和停止。但问题是它播放了一些歌曲而没有播放一些歌曲。我也导入了 winmm.dll 文件。但是有些文件可以播放,而有些则没有。此外,任何人都可以建议我如何向其中添加一堆随机播放的歌曲?代码是:

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

namespace MP3Player
{
    class MusicPlayer
    {
        Boolean isPlay=false;
        [DllImport("winmm.dll")]
        private static extern long mciSendString(string lpstrCommand, StringBuilder  lpstrReturnString, int uReturnLength, int hwndCallback);
        public void open(String file)
        {
            string command = "open \"" + file + "\" type MPEGVideo alias MyMp3";
            mciSendString(command, null, 0, 0);
            isPlay = false;
        }

        public void play()
        {
            if (isPlay == false)
            {
                string command = "play MyMP3";
                mciSendString(command, null, 0, 0);
                isPlay = true;
            }

        }
        public void pause()
        {
            if (isPlay == true)
            {
                string command = "pause MyMP3";
                mciSendString(command, null, 0, 0);
                isPlay = false;
            }
        }
        public void stop()
        {
            string command = "stop MyMp3";
            mciSendString(command, null, 0, 0);
            isPlay = false;

            command = "close MyMp3";
            mciSendString(command, null, 0, 0);
        }

    }
}
4

3 回答 3

0

我不知道为什么您的应用程序不播放某些 mp3,我想我可以假设它是因为音频编码,但不要引用我的话。

至于你的“随机播放功能”

您可以尝试实现的是一个在目录中查找并获取该目录中所有 mp3 文件的数组,

然后它随机播放一首歌,

这是一个示例代码:

fileinfo MySongs() = MySongDirectoryString.getFiles();
foreach (song in MySong)
{
string SongName = song.tostring();
//code to play that song
}
于 2014-04-03T12:57:59.073 回答
0

供您随意演奏。使用堆栈(更容易跟踪进度)

//Retrieve a list of files from a directory.
var di = new DirectoryInfo("Path to folder");
//Get the files and order them randomly.
var listOfFiles = di.GetFiles().OrderBy(s=> Guid.NewGuid);
//Convert the list to a stack.
var stack = new Stack<FileInfo>(listOfFiles);

现在您可以使用您的堆栈和 Pop 方法来获取列表中的下一首随机歌曲。

//Usage
var current = stack.Pop();

至于不播放的问题,您可能希望对您的 mp3 文件使用 LAME 编码器/解码器,它比 windows dll 更强大。

于 2014-04-03T13:06:09.767 回答
0

我建议使用 Windows Media Player SDK。这是一个例子

于 2014-04-03T13:05:00.850 回答