Playing sounds: accessing length and position - c#

I'm using the System.Media.SoundPlayer for playing sounds in C# desktop application passing the sound location.
Everything works fine. But how can I access the total file length (in milliseconds) and the player position?
Theses properties are accessible?

The System.Media.SoundPlayer class is not suitable for providing this information.
http://msdn.microsoft.com/en-us/library/system.media.soundplayer.aspx
The SoundPlayer class provides a simple interface for loading and playing a .wav file. The SoundPlayer class supports loading a .wav file from a file path, a URL, a Stream that contains a .wav file, or an embedded resource that contains a .wav file.
You may want to look at some other libraries for audio playback, suggested here:
C# Audio Library

Related

Playing music from a link to resourses

I wanted to make looped background music for my game. I'm working in Microsoft Visual Studio 2010. In C# I wrote this:
InitializeComponent();
System.Media.SoundPlayer sp = new System.Media.SoundPlayer(#"MCStratV1.Properties.Resources.loop1.mp3");
//sp.PlayLooping();
sp.Play();
And I don't know how to do a link to resources. I'm new at C# so I don't know much about it.
Also I wrote // because I'm not sure it is correct.
There are two problems here.
First, if you look at the MSDN page for SoundPlayer you'll find that it says "Controls playback of a sound from a .wav file." So as a start you'll have to convert your mp3 to wav format.
Second, the constructor that takes a string wants the path to the wav file on disk, not the path of a resource. You'll either have to give the path to the file, or stream the file and pass the stream to another form of the constructor.

C# MediaElement Video , how to add video from resources?

i have the MediaElement and i want to play a video,
as i know the only way of doing so is to set this item Source
mediaElement1.Source = new Uri(fileName);
but now i have the resources file which i wanna play but i cant do so because it doesnt have a path.
so to make a long story short, i'm looking for a way to play video in the madiaElement from the resources file(without writing its byte first).
From first line in the "Remarks" section of the MediaElement docs: "When distributing media with your application, you cannot use a media file as a project resource. In your project file, you must instead set the media type to Content and set CopyToOutputDirectory to PreserveNewest or Always."
So you can't play video from a resource. You can play a file attached to a project as content.
Some alternate ways described here..
media-element-wont-play-a-video
Accessing the resources in the published application
Play embedded video resource as stream
How to play a video from WinForms resources in a axWindowsMediaPlayer?

need help playing mp3 files within .NET application?

My requirements are:
- play a mp3 file on button click
- provide a option to seek to a particular time
Almost all of the tutorials/resources that I came through, used ".wav" files to play the sound. But I need to play a ".mp3" file within the application, preferably without using any 3rd party library(NAudio, BASS, etc). I expect, it can be done using the available classes of the System.Media namespace.
What is the difference between a .wav file and .mp3 file? Are they encoded in different manner, and need different media plugins to be played?
So any help regarding mp3 files would be appreciated. Thanks.
I don't believe System.Media supports MP3 files. Assuming you are using Windows, a possible approach could be to use the Windows Media Player API by adding a COM reference to "Windows Media Player" in the Add Reference dialog. Then add a using directive:
using MP = MediaPlayer;
and then instantiate the class:
MP.MediaPlayer mediaPlayer = new MP.MediaPlayer();
You can then play music files by calling:
mediaPlayer.Open("filename.mp3");
This open method seems to automatically play the media file once it is opened, however there are explicit Play() and Stop() etc methods you can use to customise this behaviour to your liking.
The XNA framework also has ability to play music files using isn't allowed to contain spaces for some annoying reason so I recommend you use the Windows Media Player API if this is intended to be used on a PC.
I hope this has put you in the right direction!

How can I play any audio file(e.g. mp3,wav) in C#?

I have a question regarding with audio files.My question is:How can I play any audio file(e.g. mp3,wav) in C#? So if I press a button,I want to play to an audio file.
How can I do that? Can you tell me how it is done with a sample code?Lastly,I have used .Net Framework 4.0.
for that first you need to load wmp.dll from your system32 folder
Then you need to add COM component
Now you can do other things(creating player)
I have a tiny project (cutted NAudio version, only playback is left and only MP3 and WAV is supported) to play MP3 and WAV files. Do you still need it? I can upload it if you need to.
A full code example on how to play waves in c# forms apps can be found here
http://msdn.microsoft.com/en-us/library/ms173187%28v=VS.100%29.aspx
You can also play wave files using C# and the XNA Framework. But that may not serve your purpose, since that is primarily for games development.

How can I split an mp3 file into several individually playable parts?

How can I break a large mp3 file into one or more mp3 files, and still have each part playable in a media player?
It's probably not going to be super easy to break an mp3 file into separate playable mp3 files. There is header information in the mp3 file that you will need to include in all of the split-up files. You might need to get a book or look for a file spec on mp3s, so you know what you're dealing with.
You might be best-off looking for a library that can deal with mp3 files, rather than trying to do it yourself with a FileStream.
Actually, breaking a MP3 file into seperate playable MP3 files is quite easy. A MP3 file consists of a lot of individual frames of iirc 1500bytes, where each has it's own header. Invalid data will be ignored by the player.
But, it would be a lot better if you where to split your files on frame boundaries, creating correct mp3 files. More info on the frame header can be found on this site: http://www.mp3-tech.org/programmer/frame_header.html
Please also consider the possibility that the music is prepended and/or appended with various meta tags, like ID3, APE and lyrics.

Categories