Error when reading audio file?

2019-07-15 02:58发布

I have the following function that I am attempting to use to determine the length of an MP3 file:

public static string GetMP3DurationBackup(string Filename)
{
    string Duration = null;
    WMPLib.WindowsMediaPlayer w = new WMPLib.WindowsMediaPlayer();
    WMPLib.IWMPMedia m = w.newMedia(Filename);
    if (m != null)
    {
        Duration = m.durationString;
    }
    w.close();
    return Duration;
}

I have run into an issue where I get the following error:

Retrieving the COM class factory for component with CLSID {6BF52A52-394A-11D3-B153-00C04F79FAA6} failed due to the following error: 80040154..

when I call the above function from my web application (call below):

string test = MediaUtil.GetMP3DurationBackup(@"C:\Temp\Audio\bad.mp3");

But when I call it from a console application test harness I created (exact same call as above) it works fine. I have set the project that contains the function to target x86 in the Build properties, but that did not fix the issue.

Does anyone know why this would happen? Suggestions on where to start to debug this?

UPDATED FOR BOUNTY:

Ok, I've tried a number of things but I am still getting this error. Among other things I have tried the steps below which I felt were the most promising, but no dice:

  1. Went into my registry and confirmed that the value at: HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{6BF52A52-394A-11d3-B153-00C04F79FAA6}\InprocServer32 is refering to C:\WINDOWS\SysWOW64\wmp.dll
  2. Opened command prompt, navigated to C:\WINDOWS\SysWow64, ran: regsvr32.exe wmp.dll

I have created a console app test harness and I am able to reproduce the error if I run the test project in x64. If I switch it to x86 it works fine.

Does anyone have any idea of why the above would not resolve the issue? Suggestions on where to look next?

2条回答
Fickle 薄情
2楼-- · 2019-07-15 03:18

You say it doesn't work in x64, but you try to register the 32-bit version of wmp.dll (C:\Windows\SysWow64 contains 32-bit assemblies).

Try to register the x64 version of wmp.dll, which is located in C:\Windows\System32 on a 64-bit platform.

If you don't have this file then there probably is no 64bit Windows Media Player available for your platform. But there is a workaround:

Create a 32-bit console application that takes the mp3 filename as command line argument and outputs the duration to stdout using Console.WriteLine, then in the webapp, you call the console application and capture the output like in this example on MSDN

查看更多
小情绪 Triste *
3楼-- · 2019-07-15 03:22

Give this lib a whirl. Its fast and has no special requirements for software to be installed on the machine.

http://naudio.codeplex.com/

查看更多
登录 后发表回答