首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PInvoke mciSendString

PInvoke mciSendString
EN

Stack Overflow用户
提问于 2013-12-05 08:53:02
回答 1查看 1K关注 0票数 0

我正在尝试使用这个函数(它需要),所以我编写了如下代码:

代码语言:javascript
复制
    [DllImport("winmm.dll")]
    private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
    private static void OnEvent(MyEvent ev)
    {
        string sound = Path.Combine(SoundFolder, ev.SoundName) + ".mp3";
        var command = string.Format("open {0} type mpegvideo alias sound", sound);
        mciSendString(command, null, 0, IntPtr.Zero);
        mciSendString("play all", null, 0, IntPtr.Zero);
        new NotificationPage(ev).Show();
    }

但它不播放任何东西,甚至是硬编码路径的文件。我做错了什么?

添加:我重写了方法,改为使用int,如果是long。但还是不起作用。甚至简单的代码:

代码语言:javascript
复制
    public MainWindow()
    {
        InitializeComponent();
        string sound = Path.Combine(SoundFolder, "NormalPriority") + ".mp3";
        if (!File.Exists(sound))
            throw new FileNotFoundException();
        var command = string.Format("open {0} type mpegvideo alias sound", sound);
        mciSendString(command, null, 0, IntPtr.Zero);
        mciSendString("play all", null, 0, IntPtr.Zero);
    }

所以这是一个WPF应用程序

原来看起来就像

代码语言:javascript
复制
    private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
    {
        _notificator = new Notificator(Directory.GetCurrentDirectory());
        _notificator.EventStarted += (o, ev) => Dispatcher.Invoke(() => OnEvent(ev));
    ...

我在C++ 2上写的

代码语言:javascript
复制
#include <windows.h>
#include <mmsystem.h>
#pragma comment (lib, "winmm.lib")
#include <string>


int main()
{
    int res = mciSendString(L"open D:\\song.mp3 type mpegvideo alias sound", NULL, 0, 0);
    printf("%i", res);
    return 0;
}

同样的误差277

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-05 09:23:19

p/invoke有一个错误,我可以看到。返回值应该是intuint类型,因为MCIERROR是32位整数。在C#中,long有64位宽。

除此之外,您调用mciSendString的方式也很好。实际上,即使在错误的返回类型声明下,您的代码在运行它时也能很好地播放mp3文件。

那么,为什么它对你失败呢?最明显的原因是多媒体功能依赖于消息循环的存在。可能调用此代码的线程不服务于消息队列。例如,您是从控制台应用程序调用的吗?

如果线程确实有一个消息队列,并正确地对其进行压缩,那么我不知道还有什么建议。下一步将是执行一些错误检查,您目前没有这样做。注意mciSendString返回的值,看看它们是否表示错误。

现在您已经发现mciSendString正在返回错误代码MCIERR_INTERNAL。在我看来你的问题是环境问题。您的代码对我来说非常好,所以您的音频设置/驱动程序似乎有问题,或者.mp3文件有问题。但是,我们无法做更多的工作来帮助您处理代码,因为问题不在您的代码中。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20395394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档