3
votes

My goal is to mix 2 audio files that is one voice and one background music. For mixing I am using NAudio. The problem is I am getting following exception while reading mp3 file using NAudio's Mp3FileReader.

DllImport cannot be used on user-defined methods

I am using NAudio in Windows Phone 8.1 Silverlight app project. I am not sure if NAudio is useable on WP Silverlight app if its useable then please help by providing a code example or suggest any other library or custom implementation to mix 2 audio files.

3
Have you looked into SharpDX.XAudio2? I think they have some Audio tools, but I'm not sure about if they support Silverlight.auo

3 Answers

4
votes

No, I'm afraid NAudio is not usable on Silverlight, as it makes lots of calls into Windows APIs which you can't do in Silverlight. You can take a look at the NAudio NLayer project which offers fully managed MP3 decoding. You could use that in conjunction with some of the more generic helper classes in NAudio like the MixingSampleProvider to perform the mixing you require. Of course the next issue would be what you want to do with that mixed audio. Silverlight does have a way of streaming user generated audio using the MediaElement but NAudio does not provide support for this directly.

3
votes

[DllImport] requires a substantial chunk of code in the CLR, nothing very subtle about the pinvoke marshaller. That's a problem on a phone, it runs a special version of the CLR named .NETCore. Probably better known today as the codebase that spun-off the CoreCLR open source project. Keeping it small required unsubtle choices, pinvoke fell on the floor.

So no, you'll have no use at all for NAudio. You'll have to dip into the built-in support for audio. The relevant oversight MSDN page is this one. This blog post is relevant.

As you can tell, XAudio2 is your ticket with direct support for mixing. There is a learning curve of course, the language is probably the first obstacle given the question tags. Get started with this sample to get the basics.

3
votes

Maybe you should consider taking the step to upgrade from WP8.1 Silverlight (WPS) project to WP8.1 (WinRT) one so that you have better access to audio APIs...

...or directly to a UWP/Win10 app (if you don't plan to release immediately to phones). If you choose that path, you could try maybe a free preview tool that converts WPS to UWP, just released from Microsoft and Mobilize.NET - https://blogs.windows.com/buildingapps/2015/09/17/initial-preview-of-silverlight-bridge-to-uwp/