4

MCIERR_INTERNALI am trying to make a simple Media player inside an app, but I've noticed that my code WILL NOT play music unless the file is a low bitrate around 192kbps or less. The issue is that most of my music is around 260-320kbps.

Here's my code, if there's something I can do to up the 'available' bitrate options let me know, otherwise I'll need a new DLL suggestion please!

class MusicPlayer
{
    [DllImport("winmm.dll")]
    private static extern long mciSendString(string lpstrCommand, StringBuilder lpstrReturnString, int uReturnLength, int hwndCallback);

    private static void checkMCIResult(long code)
    {
        int err = (int)(code & 0xffffffff);
        if (err != 0)
        {
            throw new Exception(string.Format("MCI error {0}", err));
        }
    }

    public void open(string file)
    {
        string command = "open \"" + file + "\" type MPEGVideo alias MyMp3";
        checkMCIResult(mciSendString(command, null, 0, 0));
    }

    public void play()
    {
        string command = "play MyMp3";
        mciSendString(command, null, 0, 0);
    }
    public void pause()
    {
        string command = "stop MyMp3";
        mciSendString(command, null, 0, 0);
    }
}

**EDIT: -Winform application

-using Windows 7 sp1

-Using Visulal Studio 2013 community edition

-From error catching I now see the error number is 289, -256 = 22: MCIERR_INTERNAL, not sure what that's all about

Medic3000
  • 786
  • 4
  • 20
  • 44
  • [Error number](https://msdn.microsoft.com/en-us/library/aa228215(v=vs.60).aspx) should be 277 for MCIERROR_INTERNAL. I'm here because of the very same error 277. Also don't subtract 256. That's silly, because see link. – Bitterblue Aug 10 '16 at 09:29

3 Answers3

6

This is not an inherent limitation in Windows, problems like these are environmental. A basic check-list:

  • Try this with a known-good MP3 file. That test file is encoded at 320 kbps. This helps eliminate a specific problem with your files, like a wonky DRM scheme that only permits playback on an approved player.
  • Make sure you run this code on an STA thread, the kind provided by a Winforms or WPF app. Not a console mode app, it requires the kind of code you find in this post.
  • Beware of having non-standard ACM drivers installed. There is a lot of junk out there, always treat "codec packs" with strong distrust. Review the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Drivers32 registry key, that's where ACM drivers are registered.
  • And last but certainly not least, you are blind as a bat as long as you are ignoring the return value of mciSendString(). When it fails, it produces an error code that tells you the reason.

A simple implementation of an error checker method:

    private static void checkMCIResult(long code) {
        int err = (int)(code & 0xffffffff);
        if (err != 0) {
            throw new Exception(string.Format("MCI error {0}", err));
        }
    }

Usage:

    public static void open(string file) {
        string command = "open \"" + file + "\" type MPEGVideo alias MyMp3";
        checkMCIResult(mciSendString(command, null, 0, 0));
    }

There are a lot of possible MCI errors, you'll find them listed in MMSystem.h file in the Windows SDK "include" directory on your machine. Like C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Include\MMSystem.h. Start at MCIERR_INVALID_DEVICE_ID, subtract 256 from the error code. Always mention your Windows and VS version btw.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • thank you! I'll note I'm using Visual C# 2013 Community, This is a windows7 sp1 OS, and I am making a winform application. I'll take heed of your advise to look at what sorts of error reporting I;m getting when the files fail to play. You'll get the bounty in 21hr when I'm allowed to give it out. Thanks! – Medic3000 Nov 20 '14 at 18:50
  • The **known-good MP3 file** solved it for me. My first test mp3 had a creative commons license. Silly world -.- – Bitterblue Aug 10 '16 at 09:34
1

Ok, so I figured this out (thanks to @Hans Passant), the error code I was recieving was 277, Turns out there are problems with MCI and mp3's which had a large ID3(v2.x) tag - Album art, lyrics etc.

Coincidentally the 2 files causing me the trouble had album art, and completely filled out ID3 data (including Lyrics). So I'll be switching over to some other audio library for my needs

Medic3000
  • 786
  • 4
  • 20
  • 44
  • 2
    If you figured this out thanks to Hans' answer, you should mark his answer as the accepted one. Especially since you put a bounty on it. – Chuu Nov 20 '14 at 19:25
  • fair enough, I'd planned to give the bounty to him regardless. – Medic3000 Nov 20 '14 at 19:26
0

A typical problem with Windows API playing or not playing specific MP3 files is the following. One of the standard MP3 reading/parsing components has a [presumably?, I am under impression 16KB is in question] fixed size buffer to read initially, then skip tags and get to the real bitstream. With a lot of tags, the parser is unable to read the data and rejects the file as non-playable.

When played through DirectShow (for C# code, this would typically be playback through DirectShow.NET open source library), it is suggested to use another stock Windows component capable to read MP3 files, which is free from tag problem: Windows Media ASF Reader filter.

See related:

Even though it's a long standing issue, Microsoft years ago decided to not update standard MPEG-1 stream splitter and the problem basically exists in all Windows versions.

Community
  • 1
  • 1
Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • you didnt answer the question, I don't know what DirectShow is, but I was using Winmm.dll for my audio playback. This is non-helpful for me – Medic3000 Nov 20 '14 at 22:25