How to play mp3 in WPF Without using MediaElement? - c#

I Want to play a song in the background of my WPF Application, but i prefer not using
MediaElement Because it requires MediaPlayer 10 to run, and i can't have control on whether the user will have it or not.
What are my options?

If you do not want to use MediaElement, you can use NAudio to play an MP3.

There are multiple ways to play a Mp3 File in .Net. Try IrrKlang for example which is free for non-comercial usage.
You could also use the C# port of JavaLayer.

Related

Audio Recording in Background is not working C# UWP

I've checked Background Audio Stop Issue , added
<uap3:Capability Name="backgroundMediaPlayback"/>
in Package.appxmanifest file. For playing m4a file used MediaPlayer class, it works perfectly in background. but for recording audio it is not working in background. It is being muted. Used AudioGraph to recording.
Do i need to handle externally for recording?
Didn't find any solution for this. It seems it is system limitation right now.
We cannot record audio in background on UWP platform.
Remember: when we can’t do something directly by using UWP, we can always create an hybrid UWP/Win32 app, using the UWP bridge.
Best regards

Set webbrowser audio to stream to media player so it can be used in background on windows phone 8

I am making a netradio app on windows phone, my problem is that netradio uses the RTSP audio format and native media player in Windows Phone does not support RTSP.
As a workaround, I have to navigate to a webpage that then handles the audio.
This works fine, and plays the radio, but the problem is that as soon as the app is put in the background the music stops.
Is there a way to keep the webbrowser audio playing?
You could use the BackgroundAudioPlayer within your solution as another project where you could go through a sample here.
And also the sample from Codeplex you can try the Windows Phone Streaming Media.
https://phonesm.codeplex.com/
Hope it helps!
There is no native RTSP support with BackgroundAudioPlayer. You would need to make your own MediaStreamSource implementation to be able to use the stream. At least I didn't find any public 3rd party solutions. Check this http://social.msdn.microsoft.com/Forums/sqlserver/en-US/e052ea29-53cf-4ebb-8558-742b67fc72ad/rtsp-support-in-wp8.
If you are up for the task of writing your own MediaStreamSource implementation, you can use this as starting point and study the RTSP protocol here.

Play two sounds parallel in background agent

I have a mp3 file. I have a background sound file like rain sound or some fire sound. I want to play these two files parallel in the app as well as in background. So that it looks like a single music is playing a story with a background. Please provide any guidance to achieve this.
AFAIK there are four ways to play audio in WP: BackgroundAudioPlayer, MediaElement, SoundEffect, Microsoft Media Foundation. As for your question:
BackgroundAudioPlayer - it won't work, as there is only one Instance of BAP on the phone. So to play a different file, you have to change Track's Source, so it won't handle two at the same time.
MediaElement - it's also a bad idea (it won't also run with BAP simultanously), because as MSDN says:
When a MediaElement control plays audio or video content, any background sounds or media already playing are halted. The app launches the playback experience when the user taps the control. Only one MediaElement control can operate at a time.
you can try to play many sounds using SoundEffect (thougt as Documentation says it can be only a wave file), I won't post code here, because there are already many examples: one, two, three
as for Microsoft Media Foundation: Walkthrough, Programming Guide, Supported file formats

C# playing multiple audio files at once from your resources?

I'd like to be able to press a button to play a sound of a button being pressed while music is already playing. SoundPlayer is no help because it stops the music to play the sound.
I have added the sounds to my Resources (WindowsFormsApplication2.Resources.Properties.button) and I don't want to have to type in the file location.
Answer from MerickOWA
You CANNOT play two sounds at once using SoundPlayer.
SoundPlayer is using the native WINAPI PlaySound function to accomplish the task which has no support for playing simultaneous sounds. Creating multiple instances of SoundPlayer won't help.
There are many options most of which involve implementing a lot of the low level API to window's native audio library or DirectSound (note neither are C# and require alot of interop code)
The simplest option would be to depend on windows media player to play the audio for you.
Add a reference to "C:\Windows\System32\wmp.dll"
then use
var player = new WMPLib.WindowsMediaPlayer();
player.URL = #"..\..\bin\debug\tribal dance.wav";
Note: play starts immediately after setting the URL property
The down side of this approach is your reliance on media player for your application to work properly. On the upside, you can use any file format media player supports.

How can I play a .wav file using SharpDX DirectSound in a C# WPF project?

I want to be able to precisely control the timing of .wav files played in my program. I also want to be able to play more than one .wav file at the same time. The SoundPlayer was not good enough because I can't play two sounds at once, and even when I play different sounds consecutively, there is about a 1/8th second delay between each sound, which is unacceptable for what I am doing. I cannot find a way to add a Device using SharpDX.
You have a directsound sample in sharpdx, should get you started.
Also you can have a look at NAudio , which should also fit your needs and be a bit easier to use.

Categories