Media player c# playing from network - c#

I´m trying to play a file located on network at address:
string filePath = #"\\192.168.xx.xx\folder\folder2\Audio\audio.wav";
and trying to play it in MediaPlayer.MediaPlayer player like this:
m_player = new MediaPlayer();
m_player.Stop();
m_player.Open(new Uri(path));
m_player.Play();
It doesn't return any exception, but it also does not play the sound.
When I copy the file on a local disk and try to play it, it works fine.
Any ideas where the problem could be?

Doing some Google says, that you should try a relative Uri.
m_player = new MediaPlayer();
m_player.Stop();
m_player.Open(new Uri(path, UriKind.Relative));
m_player.Play();
Otherwise have a look at this example, which opens a stream and sets the stream to the MediaPlayer.

The SoundPlayer class can do this. It looks like all you have to do is set its Stream property to the stream, then call Play.

Related

SoundPlayer and playing a seamless succession of sounds

What I'm trying to do is play a looping wav, but with a specific loop start position like the samples used in music module files (.it, s3m. .mod, etc.). SoundPlayer doesn't have this feature, so I created two separate files: the original wav and the wav with the beginning cut off to where the loop starts. I want to play the two files in succession, seamlessly, first the original wav file with PlaySync() then the loop section with PlayLooping().
Unfortunately after I PlaySync() the first wav, there's a split second interruption between it and when PlayLooping() begins to play back the loop wav.
private static void PlayThread(SoundPlayer soundPlayer, byte[] wav, byte[] loop)
{
MemoryStream stream1 = new MemoryStream(wav);
MemoryStream stream2 = new MemoryStream(loop);
soundPlayer.Stream = stream1;
soundPlayer.PlaySync();
// here is where the split second interruption occurs
soundPlayer.Stream = stream2;
soundPlayer.PlayLooping();
}
This may not be noticeable when playing two separate distinct sounds, but is very noticeable when trying to pull off the looping mechanism I want. The sound wavs are instrument samples, very small (usually no more than 0x1000 bytes).
Instead of attempting to play multiple SoundPlayer objects consecutively, try concatenating the .wav files on the fly, and loading the merged .wav file into one SoundPlayer object.
Download this WaveIO class, add it to your project, and try the following code where you want to seamlessly play multiple sounds in a row:
string[] files = new string[3] { #"filepath1.wav", #"filepath2.wav", #"filepath3.wav" };
WaveIO wa = new WaveIO();
wa.Merge(files, #"tempfile.wav");
System.Media.SoundPlayer sp = new System.Media.SoundPlayer(#"tempfile.wav");
sp.Play();
I don't know anything about SoundPlayer, but perhaps eliminate lines 3+4 and instead write your second wave to the first stream instead of creating a new stream. That way you are in essence adding more data to the stream for it to play. Assuming you can write to it faster than it can play it, which you should be able to. I'm not sure off the top of my head if a MemoryStream will allow simultaneous read/write. I have done something similar but dont have the code on hand and it might have been slightly different.
Also, is Play() blocking? I.e. does the next line not run until it is done playing. If so, then the pause is when line 3 is reading the wav file. So you need to move .Play to another thread so that you can begin loading the next wave while the first wave is playing. Even if you find that you can't write to the first memory stream, and have to create a new memory stream, at least you will have the wav file loaded into the memory stream and ready to play as soon as the first finishes. This will significantly reduce the pause.
Consider media palyers that have a playlist of MP3s. When one MP3 is close to being finished, the media player usually will go ahead and load the next mp3 from disk and load it into memory before the first is finished playing.
Here's what my test looked like:
string sMediaPath = #"C:\Windows\Media";
SoundPlayer soundPlayer = new SoundPlayer();
soundPlayer.Stream = File.OpenRead(Path.Combine(sMediaPath, "chimes.wav"));
soundPlayer.PlaySync();
soundPlayer.Stream = File.OpenRead(Path.Combine(sMediaPath, "chord.wav"));
soundPlayer.PlaySync();
I found that this test worked exactly as you wanted - with no abrupt stops between the files. This could be due to other reasons though, most likely to do with the files themselves
Bit rate: what is the sampling rate of the file?
Is the sound file interleaved (stereo)?
How large is the sound file?
Perhaps preloading your sounds before playing them might make a difference?
string sMediaPath = #"C:\Windows\Media";
SoundPlayer soundPlayer = new SoundPlayer();
List<Stream> oSoundStreams = new List<Stream>();
oSoundStreams.Add(File.OpenRead(Path.Combine(sMediaPath, "chimes.wav")));
oSoundStreams.Add(File.OpenRead(Path.Combine(sMediaPath, "chord.wav")));
soundPlayer.Stream = oSoundStreams[0];
soundPlayer.PlaySync();
soundPlayer.Stream = oSoundStreams[1];
soundPlayer.PlaySync();
Edit:
By creating one SoundPlayer object for each sound to be played, it seems possible to bring down the short gap you're experiencing:
string sMediaPath = #"C:\Users\Foogle\Desktop\";
SoundPlayer soundPlayer1 = new SoundPlayer();
SoundPlayer soundPlayer2 = new SoundPlayer();
soundPlayer1.Stream = File.OpenRead(Path.Combine(sMediaPath, "W.wav"));
soundPlayer1.Load();
soundPlayer2.Stream = File.OpenRead(Path.Combine(sMediaPath, "P.wav"));
soundPlayer2.Load();
for (int i = 0; i < 10; i++)
{
soundPlayer1.PlaySync();
soundPlayer2.PlaySync();
}
Perhaps you could create a collection of SoundPlayer objects and play them as required?
Hope this helps.
Cheers!

How do I get the bytestream of a sound file from a server to be saved as a SoundEffect or SoundEffectInstance?

I'm trying to do something very basic, but for some reason it's just not clicking.
This is the code I have to get a sound from the resources and save them as a stream so I can use the sounds for nefarious purposes.
/// Loads a wav file into an XNA Framework SoundEffect.
private void LoadSound(String SoundFilePath, out SoundEffect Sound)
{
// For error checking, assume we'll fail to load the file.
Sound = null;
try
{
// Holds informations about a file stream.
//StreamResourceInfo SoundFileInfo = App.GetResourceStream(new Uri(SoundFilePath, UriKind.Relative));
// Create the SoundEffect from the Stream
Sound = SoundEffect.FromStream(SoundFileInfo.stream);
soundEffectInstance = Sound.CreateInstance();
soundEffectInstance.IsLooped = true;
soundEffectInstance.Play();
}
catch (NullReferenceException)
{
// Display an error message
MessageBox.Show("Couldn't load sound " + SoundFilePath);
}
}
Trying to change this code so that the file comes from an aboslute path on my server instead of the internal resources, but nothing seems to work. What am I doing wrong? Thanks.
You could use DynamicSoundEffectInstance to play sound from byte array. So, save downloaded sound into a binary file, and use it when needed.
Maybe, it helps you

Make correct URI for WP Game Library

I am trying to create a library with sounds in it, but I cant get the URIs to work, if I use a online uri like
new Uri("http://www.archive.org/download/BrahmsViolinConcerto-Heifetz/03Iii.AllegroGiocosoMaNonTroppoVivace.mp3")
it works fine, so the issue is linking correctly to my folders in my project
My in my WP Game Librarys folder I have \Sounds\letters and in that folder is a sound named a.wma
My Method for loading this is
public void PlayLetter(string letter)
{
try
{
Initialize();
FrameworkDispatcher.Update();
var uri = new Uri(#"/Sounds/letters/" + letter + ".wma", UriKind.Relative);
var song = Song.FromUri("sound", uri);
MediaPlayer.Play(song);
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}
And I of course give it string "a" as a parameter when it fails
I have also included the sound file in my project like
I just get a
A first chance exception of type 'System.InvalidOperationException' occurred in Microsoft.Xna.Framework.dll
But its an uri problem I am certain as I tried a online URI that worked just fine
Also I am in doubt of 2 things, is MediaPlayer the right thing to use in a game? And can a library play sounds (Or even contain them)
The typical thing in XNA would be to use a SoundEffectInstance:
http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.audio.soundeffectinstance.aspx
Unfortunately SoundEffectInstance only works with wav files. If you want to play back longer music files - you can use a MediaElement - but that allows for playback of a single compressed audio file at a time only. Another option might be to play compressed from the MediaLibrary using the MediaPlayer class. You could also save your own compressed audio file in the MediaLibrary to play it from there. See:
http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.media.medialibrary.songs.aspx

How would I use a pack uri resource with media player?

I have some very simple code which just needs to play a sound with a specified volume, as follows :
var mp = new MediaPlayer();
mp.Open(uri);
mp.Volume = volume;
mp.Play();
The problem arises because this is part of a Prism module. I don't know where the executing assembly is going to be so I can't use a relative Uri or siteoforigin and MediaPlayer doesn't understand the pack Uri syntax.
I have a resource wav file at the root of my assembly called "notify.wav", but I have no way to pass it into MediaPlayer via a Uri and can't see any other way to load it.
How do I play the file?
Since the resource is embedded and the MediaPlayer doesn't support pack uri, you'll need to read the resource in as a stream and write it out to file.You should then be able to load the file into the player as necessary.
I would write the file to my applications directory so that once extracted from the assembly, you can just reference the file directly.
Hope this helps
First you should declare a variable that is string for your media folder's path. This variable holds the path. just like:
string url = #"C:\Users\Alico\Documents\visual studio 2010\Projects\WpfBrushesTest\WpfBrushesTest\Dido - Thank You.mp4";
and then
mp.Open(new Uri(url,UriKind.Relative));
I’m afraid Media player does not support pack URI.
Have you tried Directory.GetCurrentDirectory or Environment.CurrentDirectory?
Use ContentResolver class to retrieve the songs from the device and catch with Song.XML that include (TextViews for Title, Article, Album, etc). This XML is later embedded into the MainActivity XML(including ListView) using Adapter class and then you can call onClick(Song.XML) to play the songs (Setting URI ) on MainActivity XML, later you can set Seekber, Next, Previous and Play function.
If you want, I will give you full codes but I am trying to play a song from another Activity using a new UI design and class file, still searching.

Silverlight MediaElement video issue

Cant display the video in Silverlight.Which i saved in sql server and retrieve it from database as byte[] and subsequently convert it as Stream and put as SetSource of Media element.But cant display anything.
Plz help me.
Like this:
MediaElement SoundClip = new MediaElement();
SoundClip.SetSource(stream);
SoundClip.AutoPlay = false;
SoundClip.Width = 500;
SoundClip.Height = 500;
SoundClip.Stretch = Stretch.Fill;
this.LayoutRoot.Children.Add(SoundClip);
SoundClip.Play();
But does not work.
EDIT:
It is in wmv format.
I could not even play a wmv/wma file from local drive.Is there any issue with the PC i m using.It just runs the code but does play it.does not show any error.
Any suggestion?
What format is the media?
Can it be played directly as a WMV, WMA, MP3, or H.264 file?
Is the media file completely downloaded?
Monitor the HTTP traffic using Fiddler to see what's happening
Attach a MediaElement.MediaFailed event to the SoundClip to see the error (if any)

Categories

Resources