Question

I'm trying to use this function (it requires), so i'm writing a code like this:

    [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();
    }

but it doesn't play anything, even files with hardcoded path. What am I doing wrong?


Added: i rewrote method to use int instead if long. But it still not working. Even simple code:

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

so it's a WPF app

originaly it looks like

    private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
    {
        _notificator = new Notificator(Directory.GetCurrentDirectory());
        _notificator.EventStarted += (o, ev) => Dispatcher.Invoke(() => OnEvent(ev));
    ...

i wrote it on C++ 2

#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;
}

same error 277

Was it helpful?

Solution

The p/invoke has one error that I can see. The return value should be of type int or uint since MCIERROR is a 32 bit integer. In C# long is 64 bits wide.

Beyond that, the way you are calling mciSendString is fine. In fact, your code plays an mp3 file just fine when I run it, even with the erroneous return type declaration.

So, why does it fail for you? The most obvious reason is that the multimedia functions rely on the presence of a message loop. Perhaps the thread which calls this code does not service a message queue. Are you making the call from a console application for instance?

If the thread does have a message queue, and pumps it properly, then I'm not sure what else to suggest. The next step would be to perform some error checking which you do not do at the moment. Take note of the values returned by mciSendString and see if they indicate errors.


You've now discovered that mciSendString is returning error code MCIERR_INTERNAL. To me that sounds like your problem is environmental. Your code works perfectly well for me so it seems that there is a problem with your audio setup/drivers or a problem with the .mp3 file. But there's nothing more that we can do to help you with your code since the problem is not in your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top