Madmike's Blog

December 4, 2009

Another good thing developed by Microsoft

There was always a time when you needed to make an application that used videos and music ,and you needed to make a player from scratch , you needed to download lots of libraries and many dll for your project.
But now I’ve discovered an useful namespace made by Microsoft : Microsoft.Directx.AudioVideoPlayback.
These library is found in DirectX SDK and the latest version until now can be found here : Download here.

I will include some examples of how easy to use are this libraries:

Audio:

string filePath=”the location where is your file , including file name and extension”;
Audio audio=new Audio(filePath);

For the 3 state a music and movie could have : Play , Pause and Stop are made 3 methods
audio.Stop(), audio.Play() , audio.Pause() and also 3 states illustrated by 3 properties audio.Paused, audio.Stopped , audio.Playing (all 3 boolean).

Example of music player panel in C#:

public partial class MusicContainer : UserControl
{
public MusicContainer()
{
InitializeComponent();
}

public string Path { get; set; }
public Audio audio=null;
//control loaded and we try to load a movie from disk
private void MusicContainer_Load(object sender, EventArgs e)
{
if (audio == null)
{
audio = new Audio(Path);
label1.Text = Path;
}
//we use the timer to show music/movie progress
timer1.Start();
}
//button used for Play
private void button1_Click(object sender, EventArgs e)
{
if (audio != null)
{
if (audio.Paused || audio.Stopped)
{
audio.Play();
}
}
}
//button used to pause
private void button2_Click(object sender, EventArgs e)
{
if (audio != null)
{
if (audio.Paused)
{
audio.Pause();
}
}
}
//buton used for stopping
private void button3_Click(object sender, EventArgs e)
{
if (audio != null)
{
if (audio.Paused || audio.Playing)
{
audio.Stop();
}
}
}
//show the movie/music progress during playing every second
private void timer1_Tick(object sender, EventArgs e)
{
trackBar1.Value = (int)(audio.CurrentPosition / audio.Duration*100);
}
//navigate forward/backward in the movie/music
private void trackBar1_Scroll(object sender, EventArgs e)
{
audio.CurrentPosition = trackBar1.Value * audio.Duration / 100;
}
}

For the movie part is the same as music , but this one has more features.

Movie movie=new Movie(filePath);
Control control="a control that will be the video showing surface";
movie.Owner=control;
//or null if you want to take the full form of the container in which movie resides

movie.Fullscreen=true; // if you want to make it full screen

I found this namespace and used it , there are many examples of different players all over the MSDN and I was impressed of the quality of the playback and the useful and easiness to use it.

Have fun making your own player

Create a free website or blog at WordPress.com.