Play sound from URI in c#.NET - c#

Is there any way I can play audio in my c#.NET application by using a soundcloud stream URI? I found a method to do this with windows 8 phones:
new AudioTrack(new Uri("[your soundcloud uri]", UriKind.Absolute), "[Track Name]", "[Artist]", "[Album]", null)
But I can't find a method to do this in desktop .NET framework. Could someone shine some light on this?

Since SoundCloud uses mp3 as streaming format, you may use WMPLib. First you need to add reference of it (Project -> References -> Add Reference -> COM -> Windows Media Player)
var wplayer = new WMPLib.WindowsMediaPlayer();
wplayer.URL = "http://somewebsite.com/somedir/somefile.mp3";
wplayer.controls.play();
But on computers without Windows Media Player (Like Windows N, KN), you may need third-party libraries to play soundcloud streams. For example, NAudio.
ps: btw, the play of WMPLib is async, so you need to register to StateChange event to know whether the music is ended. See the MSDN Article.
Reference: MSDN Article

Related

Creating a video from a collection of BitmapImage in UWP

I'm trying to come up with a method of creating a video file (or even an animated gif), using a collection of BitmapImage in a Windows 10 application (UWP). I noticed that the mobile extensions have a ScreenCapture class (I'm not sure if that would do what I need anyway), but I need this to work on desktop as a minimum. I've had a play with the MediaCapture class, but am unable to find any methods that allow me to record images from the screen, or manipulate the video directly.
Windows.Media.Capture.MediaCapture mc = new Windows.Media.Capture.MediaCapture();
mc.StartRecordToStreamAsync()
Is what I'm trying to do possible using UWP in C#, and if so, how?
You'll need MediaComposition API.
var composition = new MediaComposition();
Add clips (files)
composition.Clips.Add(await MediaClip.CreateFromImageFileAsync(someImageFile));
and in the end render to file
await composition.RenderToFileAsync(file);

Windows Phone 8 - Playing streamed audio (internet radio station)

I am trying to make an application that would play an audio stream (mp3) from the following URL http://icecast6.play.cz/radio1-128.mp3. I am struggling to get it working. Is there any default support for cases like these? That the content is continuously streamed?
I have tried to use Background Audio Agent and set Media Element but none of that has been working for me. Would Background Audio Streamer be an option for me? I would be happy for any similar example that I would use to my advantage.
Thank you
I found phonesm project on codeplex that provides great examples and functionality for implementing internet audio streaming.
You need to use BackgroundAudioPlayer by creating another project and adding its reference to your project.
Check this link:
http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202978%28v=vs.105%29.aspx
You can use Microsoft player framework.
For more details Player Framework

Send sound to different audio devices

Is it possible to play a sound on a playback device that is not set as default playback device?
I need to play multiple files simultaniously trough multiple output devices.
Does anyone knows a solution for .net? (C#)
Thanks!
you will have to go to lower level API. You can try find C# wrapper for Wasapi, ASIO, DirectSound or MediaFoundation. Maybe XNA framework could do the job, which is managed framework over directX
well I was playing around with similar issue.
XNA could not do send output to specific device.
I have ended out with http://naudio.codeplex.com/, which is exactly what you need. It's C# wrapper for Wasapi, ASIO, DirectSound libraries. It also includes many very helpful classes for converting, decoding, visualizing etc. For simple player, check out NAudioDemo sample project.
find out CreateWaveOut() method. This is where you should select your playbackdevice.
for example:
MMDevice device = new MMDeviceEnumerator()
.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.All)
.FirstOrDefault(d => d.ID == "yourplaybackdeviceid");
IWavePlayer waveOut = new WasapiOut(device, AudioClientShareMode.Shared, false, latency);
Download the naudio library from sourcecode tab not from Download->Release, it's not very actual and it's little buggy.
another good tool is bass.net
http://www.un4seen.com/

Playing 2 sounds together at same time

Designing a game for any laptop:
I want to play 2 sounds at the same time! A background music and a sound when pressing on a button!
Using System.Media won't allow me to play 2 sounds together. For background music, I used the following:
SoundPlayer sp = new SoundPlayer(#"..\..\bin\debug\tribal dance.wav");
sp.Play;
If I played another SoundPlayer, the previous one stops!
I want the background music to keep playing until the form is closed! And at the same time, if a button is clicked, another sound will be played with the music!
I also tried added the Windows Media Player to the toolbox and used it (allowing me 2 play 2 sounds at same time). That works fine but the path will give an error if I put it (#""..\..\bin\debug\tribal dance.wav");
and I need to put it that way in order to be read by any computer.
Any suggestions?
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.
Using (WMPLib) Windows Media Player Library you can do this easily. Keep Your file in Debug/Sound folder and follow the following code. This will run 2 file simultaneously. To Add WMPLib in your project add it by NuGet Package Manager.
WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
WMPLib.WindowsMediaPlayer wplayer2 = new WMPLib.WindowsMediaPlayer();
string play_string = "be.mp3";
wplayer.URL = #"SOUND/" + play_string;
wplayer.controls.play();
string play_string2 = "ba.mp3";
wplayer2.URL = #"SOUND/" + play_string2;
wplayer2.controls.play();
http://msdn.microsoft.com/en-us/library/dd562851%28v=VS.85%29.aspx this article may help
If you need absolute path to file you can use next code
string soundFile = Path.Combine(Directory.GetCurrentDirectory() + "file.wav");

How to interact with Windows Media Player in C#

I am looking for a way to interact with a standalone full version of Windows Media Player.
Mostly I need to know the Path of the currently played track.
The iTunes SDK makes this really easy but unfortunately there really isn't any way to do it with Windows Media Player, at least not in .Net(C#) without any heavy use of pinvoke, which I am not really comfortable with.
Thanks
Just to clearify: I don't want to embedded a new instance of Windows Media Player in my app, but instead control/read the "real" full version of Windows Media Player, started seperatly by the user
Just add a reference to wmp.dll (\windows\system32\wmp.dll)
using WMPLib;
And then you can instantiate a media player
var Player = new WindowsMediaPlayer();
// Load a playlist or file and then get the title
var title = Player.controls.currentItem.name;
See Creating the Windows Media Player Control Programmatically for more information
For remoting the Windows Media Player, you can use the IWMPRemoteMediaServices interface to control the stand alone Windows Media Player. And you should be able to read all the informations you want like title or filename from your WMP player object. Unfortunately there is no C# smaple code in the SDK included. You can get the files from here: http://d.hatena.ne.jp/punidama/20080227 Look for the file WmpRemote.zip
Originally it's from here: http://blogs.msdn.com/ericgu/archive/2005/06/22/431783.aspx
Then you have to cast to the WindowsMediaPlayer object:
RemotedWindowsMediaPlayer rm = new RemotedWindowsMediaPlayer();
WMPLib.WindowsMediaPlayer myPlayer = this.GetOcx() as WMPLib.WindowsMediaPlayer;
and there you go..
I had this https://social.msdn.microsoft.com/Forums/vstudio/en-US/dbd43d7e-f3a6-4087-be06-df17e76b635d/windows-media-player-remoting-in-c?forum=clr in my bookmarks but have NOT tested it in anyway. Just a pointer in the right direction. It's nothing official and will require a bit of digging, but you should get a fairly simple wrapper (which will still use PInvoke under the hood - but you won't see it) around Windows Media Player.
Hope that helps.
Oh, I misunderstood. I thought you were talking about controlling the currently running Windows Media Player instance. If you are hosting Windows Media Player yourself then WMPLib is certainly the better solution.
The best info I have seen on interacting with Windows Media Player is this article written by Stephen Toub.
He lists a whole load of different ways to play dvr-ms files (doesn't really matter what format they are for this though). The bit that is possibly of interest to you is about using a Media Player ActiveX Control, which you can add to the visual Studio toolbox by right-clicking and adding the Windows Media Player ActiveX COM Control. You can then embed the player into your app, and access various properties of Media Player, like the url:
WMPplayer.URL = stringPathToFile;
This solution is possibly not what you want because it's starting a new instance of Media Player (as far as I know), however it might point you in the right direction.

Categories

Resources