0

尝试使用 播放 mp3 文件mciSendString时,请通过以下命令:

open "{FileName}" [type mpegvideo] alias {AliasName}//尝试了有和没有type mpegvideo

play {AliasName}

我得到错误MCIERR_CANNOT_LOAD_DRIVER : 'Unknown problem while loading the specified device driver'

在这篇文章中读到您需要安装 MP3 编解码器,但我确实有一个,所以这不是问题。

在四处寻找之后,试图找出问题所在,我偶然发现了这个项目,它是一个使用的音频播放器mciSendString,并决定尝试一下,看看是否会出现同样的问题,有趣的是它工作得很好,可以播放 mp3 文件。 ..那是什么问题,为什么在我的项目中不起作用。

这是代码(这只是测试代码,如果没有足够的异常处理,请见谅):

using System;
using System.IO;
using System.Runtime.InteropServices;

namespace Test
{
    unsafe class Program
    {
        [DllImport("winmm.dll", SetLastError = true)]
        public static extern bool mciGetErrorString([In] int error, [In, Out] char[] buffer, [In] int bufferCount);

        [DllImport("winmm.dll", SetLastError = true)]
        public static extern int mciSendString([In] string command, [Optional, In, Out] char[] returnBuffer, [Optional, In] int returnBufferCount, [Optional, In] IntPtr hNotifyWindow);

        static void Main(string[] args)
        {
            Play(@"D:\Audio\simple_beat.mp3");

            Console.ReadLine();

            Close();
        }

        static void Play(string fileName)
        {
            Close();

            if (!string.IsNullOrEmpty(fileName) && File.Exists(fileName))
            {
                int error = mciSendString($"open \"{fileName}\" type mpegvideo alias RandomAudio", null, 0, IntPtr.Zero);

                if (error != 0)
                {
                    error = mciSendString($"open \"{fileName}\" alias RandomAudio", null, 0, IntPtr.Zero);

                    if (error != 0)
                    {
                        throw new MciException(error);
                    }
                }
                error = mciSendString($"play RandomAudio", null, 0, IntPtr.Zero);

                if (error != 0)
                {
                    Close();

                    throw new MciException(error);
                }
            }
        }

        static void Close()
        {
            var error = mciSendString($"close RandomAudio", null, 0, IntPtr.Zero);

            if (error != 0)
            {
                throw new MciException(error);
            }
        }

        class MciException : SystemException
        {
            public MciException(int error)
            {
                var buffer = new char[128];

                if (mciGetErrorString(error, buffer, 128))
                {
                    _message = new string(buffer);

                    return;
                }
                _message = "An unknown error has occured.";
            }

            public override string Message
            {
                get
                {
                    return _message;
                }
            }

            private string _message;
        }
    }
}
4

3 回答 3

1

如果您尝试在控制台项目下运行,您可以创建一个窗口句柄并将其分配给您的类。一个 VB 示例片段:

Public Class AudioPlayer : Inherits NativeWindow : Implements IDisposable
    Private piFormHandle As Integer = 0

    Sub New
        Me.CreateHandle(New CreateParams)
        piFormHandle = Me.Handle.ToInt32
    End Sub

    Public Function Play()
        mciSendString("play MyAlias from 0 notify", Nothing, 0, piFormHandle)
    End Sub

    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                Me.DestroyHandle()

我使用“通知”和句柄,所以我可以捕获 MM_MCINOTIFY 并检测文件的结尾:

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

    Const MM_MCINOTIFY As Integer = &H3B9
    Const MCI_NOTIFY_SUCCESSFUL As Integer = &H1

    Select Case (m.Msg)
        Case MM_MCINOTIFY
            Select Case m.WParam.ToInt32()
                Case MCI_NOTIFY_SUCCESSFUL
                    ' Close device, throw events...
于 2020-12-21T22:11:01.357 回答
0

发现问题出在哪里,mciSendString无法在控制台应用程序中打开和播放 MP3 文件,但如果应用程序是 winform,它将播放它们。

因此,如果您想通过播放 MP3,mciSendString您需要创建一个 winform 应用程序,如果您需要控制台而不是表单,只需将表单大小设置为零并用于AllocConsole创建控制台。

于 2019-07-28T01:52:21.120 回答
0

你的 mp3 文件在一些 USB 中?因为我不能在 USB 中播放 mp3 文件,但是当它是 HD 时我可以..

于 2020-08-18T02:00:59.790 回答