【问题标题】:Developing a MP3 Player using C#使用 C# 开发 MP3 播放器
【发布时间】:2014-04-03 10:19:01
【问题描述】:

我目前正在使用 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);
        }

    }
}

【问题讨论】:

    标签: c# winmm


    【解决方案1】:

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

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

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

    然后它随机播放一首歌,

    这里有一个示例代码:

    fileinfo MySongs() = MySongDirectoryString.getFiles();
    foreach (song in MySong)
    {
    string SongName = song.tostring();
    //code to play that song
    }
    

    【讨论】:

    • 要使其随机化,只需添加一个随机函数即可从类似于此 SongName.Value=MySongs[new Random().Next(0,MySongs.Length) ] 的数组中拉出一个随机项;
    【解决方案2】:

    我建议使用 Windows Media Player SDK。这是example

    【讨论】:

      【解决方案3】:

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

      //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 更强大。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多