Version 0.75: versatile infolabels for any trackbar (or other control), trackbar values from/to coordinates, improved overlay z-order, relative media startpositions and others. For more information please see the 'About PVS.AVPlayer versions.txt' download.
Library size: 144 KB
Platform : Microsoft Windows - WinForms Any CPU
Requirements: Microsoft .NET Framework 2.0 or higher
License : CPOL - Free to Use
PVS.AVPlayer is an MCI Wrapper Class Library for .NET developers for easy but powerful playback of movies and/or music from within your application using Microsoft Windows built-in Media Control Interface (MCI), adding:
PVS.AVPlayer
PVS.AVPlayer
Please note that a VB.NET version of the article can be downloaded from the link at the top of this article.
Before you can use the PVS.AVPlayer library, you have to add a reference to it in your project.
First download "PVS.AVPlayer.zip", if you haven't already done so, and unpack the library (DLL) anywhere you like on your harddisk (the .XML-file contains information for Visual Studio intellisense and is not needed with your finished application). Later on, Visual Studio will automatically copy the DLL to your project output folder (bin\Release) together with the executable (application) you have created.
Next, create a new project (you can also use an existing project of course) and add a reference to the library. You can do this by right-clicking on the project in solution explorer (or using the Project menu) and then select Add Reference: choose the Browse tab and locate and select "PVS.AVPlayer.dll". Now you're ready to use PVS.AVPlayer.
Finally, you might want to include the library namespace in your project files. It allows shorter names to be used. Type at the top of every project file that uses the library:
using PVS.AVPlayer;
If you don't want to get into the examples below, here's how you can play a movie inside a control (in this case 'panel1') on a .NET Windows Forms Application Form:
panel1
Player myPlayer = new Player();
myPlayer.Play(@"C:\MyMovie.mpg", panel1);
To stop playing and remove myPlayer, use:
myPlayer
myPlayer.Dispose();
To play a mediafile (movie or music), you'll first have to create a 'Player'.
Player
In your project, you can create as many Players as you like and also get rid of them again when you don't need them anymore. You can use them simultaneously, even to play the same mediafile.
Once you have created a Player, you can keep using it to play mediafiles. You don't have to stop a playing mediafile in order to play another mediafile, the Player takes care of that.
Usually you would create one or more Players to be used throughout the project, so you declare them at the top of your project, outside any method but within a class (global). In the next example, the name of the Player is "myPlayer1" and a few lines of code are added to use one of the events raised by the Player.
myPlayer1
A Player can inform you about changes in its playing state by sending you 'event messages'. You can 'catch' these events with your Player's EventHandler and act upon it. For instance, in case of a MediaEnded event (that tells you that a mediafile has finished playing) you may want to play another (next) mediafile (for more information, please see: 9. MediaEvents).
EventHandler
MediaEnded
Adding a Player with EventHandler to a new Windows Forms Application project:
using PVS.AVPlayer;
namespace MyProject
{
public partial class Form1 : Form
{
Player myPlayer1;
public Form1()
{
InitializeComponent();
myPlayer1 = new Player();
myPlayer1.Events.MediaEnded += myPlayer1_MediaEnded;
}
private void myPlayer1_MediaEnded(object sender, EventArgs e)
{
// ... a mediafile has finished playing
}
}
}
Now all you have to do is tell the Player which mediafile (path and filename) to play and, if it's a movie, where to show it (player display).
To play a mediafile, you can use one of the Player's Play methods, but before you do so, you'll need the path and filename of the mediafile you want to play. Later on, we'll add an OpenFileDialog to select mediafiles, but for now the name of a mediafile is just added to the project (replace "C:\MyMovies\MyMovie.mpg" by the path and filename of one of your movies).
Play
OpenFileDialog
Also, if you're about to play a movie, you have to tell the Player where to display the movie: you can use a Form or a Control on a Form to display the video of a mediafile (for more information please see 4. Player Display).
Form
PlayMedia
using PVS.AVPlayer;
namespace MyProject
{
public partial class Form1 : Form
{
Player myPlayer1;
string myMovie = @"C:\MyMovies\MyMovie.mpg";
public Form1()
{
InitializeComponent();
myPlayer1 = new Player();
myPlayer1.Events.MediaEnded += myPlayer1_MediaEnded;
}
private void myPlayer1_MediaEnded(object sender, EventArgs e)
{
// ... a mediafile has finished playing
}
private void PlayMedia(string fileName)
{
myPlayer1.Play(fileName, panel1);
if (myPlayer1.LastError)
MessageBox.Show(myPlayer1.LastErrorString);
}
private void button1_Click(object sender, EventArgs e)
{
PlayMedia(myMovie);
}
}
}
Press F5 (Start Debugging), click the button on your Form and see what happens.
You can display the video of a mediafile using a Form or almost any other type of control, like a picturebox, label or panel. A panel might be the best choice because it has the least resources attached and is very suitable for this purpose.
picturebox
label
panel
panel
Any Form or suitable control will do, it doesn't have to be in the same class as the player. If you have a second Form in your project, you can display the video on that Form, controlling it from the first Form.
You can set or change the display of a player not only with the Play method (as above), but anytime you like, even when a movie is playing, with:
myPlayer1.Display.Window = panel1; // panel1 as display
myPlayer1.Display.Window = this; // the Form as display
myPlayer1.Display.Window = Form2.panel1; // panel1 on another Form as display
myPlayer1.Display.Window = null; // no display
You only have to set the player's display once, the player will 'remember' which display to use (as with all other player settings).
If you don't want to display video at all (but only hear the sound), you can disable the display of video with:
myPlayer1.Video.Enabled = false;
If you just want to play musicfiles, like mp3, you don't have to use a display or disable the display of video:
myPlayer1.Play(@"C:\MyMusic\MySong.mp3");
Video can be shown in different ways on the player's display by setting the player's displaymode. You can have the video fill the display (stretch), zoom it, use its original size, center it or set it to any size and position you like.
displaymode
You can set the player's displaymode with (among others):
displaymode
myPlayer1.Display.Mode = DisplayMode.Stretch; // fill the display
myPlayer1.Display.Mode = Displaymode.ZoomAndCenter; // as large as possible preserving size ratio (default setting)
You can set the position and size of the video at will by setting myPlayer1.Video.Bounds. When you do so, the displaymode is set to DisplayMode.Manual. The position is relative to the top-left of the player's display (panel1). With the manual displaymode, the player does not resize the movie (as with most of the other displaymodes) when the size of the player's display changes.
myPlayer1.Video.Bounds
DisplayMode.Manual
displaymodes
// left = -10, top = -20, width = 1000, height = 750
myPlayer1.Video.Bounds = new Rectangle(-10, -20, 1000, 750);
// same size as myPlayer1 display
myPlayer1.Video.Bounds = myPlayer1.Display.Window.DisplayRectangle;
Similar to this option are the options to scroll the position of the video (myPlayer1.Video.Move) and to zoom in or out (myPlayer1.Video.Zoom). These options also allow the use of two or more movies in one display (with multiple players using the same display) for instance by scrolling them in and out of view.
myPlayer1.Video.Move
myPlayer1.Video.Zoom
for (int i = 0; i < myPlayer1.Display.Height; i++)
{
myPlayer1.Video.Move(0, 1); // scroll movie 1 pixel down
System.Threading.Thread.Sleep(2); // slow down a bit
}
Note: A movie is displayed (sort of) on top of a display control (panel1). If you click on the part of the control where the movie is being displayed, the control (panel1) does not 'receive' the mouseclick (exception: right-click for contextmenu). The same goes for most other events. Also any control inside the display control is overwritten (not visible) by the movie. You can display a control on top of a video if its parent control is not the player's display (panel1) or by using a PVS.AVPlayer display overlay, which is more convenient and has more options (like transparency and opacity).
PVS.AVPlayer has built-in options to display a movie using the entire screen, hiding the desktop, taskbar and anything else. However, you still can have other windows (and the Taskbar) to be displayed in front of your fullscreen application.
You can not only show a Form fullscreen but also a player's display, meaning that you can show a player's display using the entire screen even if it's smaller than the Form it's on (while the player's display still remains part of the Form).
You can switch to fullscreen display with:
myPlayer1.FullScreen = true;
Before (or after) you do so, you can use the FullScreenMode option to select what will be shown fullscreen:
FullScreenMode
FullScreenMode.Form
FullScreenMode.Parent
FullScreenMode.Display
myPlayer1.FullScreenMode = FullScreenMode.Form;
The player's display or its parent can't be inside any other control than the Form itself if you want to use fullscreen modes 2 or 3.
Before adding the fullscreen option to the example code, we must have some way to return to normal display, otherwise we'll be stuck in fullscreen display. The best way perhaps would be to add a contextmenu to panel1 or even an 'always on top' controller Form, but for now we're just going to 'catch' keypress events for the Form, the player's display is on by setting the Form's KeyPreview property to 'true' and add a Form's keyeventhandler to check if the 'Space' key was pressed.
contextmenu
panel1
catch
KeyPreview
true
Form
keyeventhandler
Also a few other things have been added to the code:
button1
string
PlayNextMedia
using PVS.AVPlayer;
namespace MyProject
{
public partial class Form1 : Form
{
Player myPlayer1;
//Form2 myOverlay1; // please see below: 7. Display Overlays
OpenFileDialog myOpenFileDialog1;
string[] myMovies;
int movieToPlay = 0;
Point buttonLocation;
public Form1()
{
InitializeComponent();
myPlayer1 = new Player();
myPlayer1.Events.MediaEnded += myPlayer1_MediaEnded;
myPlayer1.Display.Window = panel1;
myPlayer1.Display.Mode = DisplayMode.Stretch;
//myOverlay1 = new Form2(); // please see below: 7. Display Overlays
//myPlayer1.Overlay.Window = myOverlay1;
myOpenFileDialog1 = new OpenFileDialog();
myOpenFileDialog1.Multiselect = true;
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
}
private void myPlayer1_MediaEnded(object sender, EventArgs e)
{
PlayNextMedia();
}
private void PlayMedia(string fileName)
{
myPlayer1.Play(fileName);
if (myPlayer1.LastError)
{
MessageBox.Show(fileName + "\r\n\r\n" + myPlayer1.LastErrorString);
if (myMovies.Length > 1) PlayNextMedia();
}
}
private void button1_Click(object sender, EventArgs e)
{
if (myOpenFileDialog1.ShowDialog(this) == DialogResult.OK)
{
myMovies = myOpenFileDialog11.FullScreen)
{
myPlayer1.FullScreen = false;
button1.Location = buttonLocation;
}
else
{
buttonLocation = button1.Location;
myPlayer1.FullScreen = true;
button1.Location = new Point(24, 24);
button1.BringToFront();
}
}
}
}
One Form, one button and one panel. That doesn't even come close to being a real mediaplayer (there's not even a 'Stop' button). Still, it already has everything in it to play mediafiles, and more.
The example code was kept short, just to give an idea how to start with PVS.AVPlayer. The code could do with some rewriting for a real application but the building blocks of it can be used as a starting point. You can have only a few lines of code in your application to play a movie or even create your own home mediaplayer.
Still, it's Windows Media Control Interface (MCI) and your application that do all the real work. PVS.AVPlayer just passes on your instructions to MCI and then continues doing nothing until you use another instruction, resize the player's display or when a MediaEvent occurs.
MediaEvent.
To see what that looks like, let's add a display overlay to the example code:
First, create a new Form (in this example 'Form2') in your Project (menu Project: Add Windows Form...) and drag a control from the Toolbox, let's use a Label, on it. We now have a display overlay.
Form2
You may want to make some adjustments to the label, like changing the text, increasing the fontsize and changing the foregroundcolor, but for now leave its backgroundcolor the same as the Form's backgroundcolor..
backgroundcolor
backgroundcolor
Next (and last), all we have to do is to attach the overlay (Form2) to the player:
Form2 myOverlay1 = new Form2(); // create an instance of Form2
myPlayer1.Overlay.Window = myOverlay1; // and attach it to the player
You can insert these lines in the example's Form1() method (see remarks in the example project code above) or anywhere you like (but of course only after a player with a display has been created), press F5, play a movie and see what it looks like.
Form1()
Note: You might want to store the overlay reference in a global variable (as with 'myPlayer1') in case you want to dispose (or reuse) the overlay:
myPlayer1.Overlay.Window = null; // remove the overlay from the player
myOverlay1.Dispose(); // dispose the overlay (Form2)
To make a display overlay transparent, PVS.AVPlayer sets the Form's TransparencyKey property to the backgroundcolor of the Form. If you don't want the background to be transparent but want to use one of the foregroundcolors for transparency (for 'see through' shapes) you can set the Form's TransparencyKey yourself (if it's set, PVS.AVPlayer doesn't change it).
TransparencyKey
foregroundcolor
To avoid unwanted (partially) transparency of items on a display overlay, use a background (or transparencykey) color that is usually not or very rarely used (like maybe 'RosyBrown'). To avoid visible 'edges' around text on a transparent overlay, use a backgroundcolor for the Form that is close to the color of the text. Setting the Form's opacity a little lower than 100% 'softens' text and other items on a transparent display overlay.
transparencykey
RosyBrown
An overlay can be displayed using the size of the movie or the size of the player's display. Use the OverlayMode property to set the overlay size.
OverlayMode
Usually an overlay is shown only when a movie is being played. If you want it to be always visible, you can set the OverlayHold property to 'true'.
OverlayHold
If you want to use an overlay for textinput or have selectable controls (like buttons) on it, you can set the property OverlayCanFocus to 'true'.
OverlayCanFocus
To regularly change items (animation) on a display overlay (e.g. the text of a Label) or executing code in the overlay (Form2) class, you can use a timer (the overlay is usually not the Form that has focus).
Label
If the display overlay Form has no contextmenu, it will 'inherit' the contextmenu of the player's display (panel1; if any).
contextmenu.Window = myOverlay1 and a mediafile is playing (or the Overlay Hold option is active) and stop updating when the overlay is deactivated (e.g. with myPlayer1.Overlay.Window = null) or not visible (minimized).
myPlayer1.Overlay.Window = myOverlay1
myPlayer1.Overlay.Window =.Delay option is enabled by default (300 ms) but can be changed with:
OverlayDelay
Overlay.Delay
myPlayer1.Overlay.Delay = ms; // set delay time (100-1000 ms)
myPlayer1.Overlay.Delay = 0; // disable overlay delay1.Sliders.Position = trackBar1;
By setting the Sliders.PositionMode option, you can have the trackbar show the position between the beginning and end of a mediafile (PositionSliderMode.Track) or between the player's media Start- and EndPosition (PositionSliderMode.Progress).
Sliders.PositionMode
PositionSliderMode.Track
PositionSliderMode.Progress
On slower systems, you might want to set myPlayer1.Sliders.PositionLiveUpdate = false.
myPlayer1.Sliders.PositionLiveUpdate = false
Similar, you can also let the player handle an audio volume slider (myPlayer1.Sliders.AudioVolume), an audio balance slider (myPlayer1.Sliders.AudioBalance), a playback speed slider (myPlayer1.Sliders.Speed) and/or a playback shuttle slider (myPlayer1.Sliders.Shuttle):
myPlayer1.Sliders.AudioVolume
myPlayer1.Sliders.AudioBalance
myPlayer1.Sliders.Speed
myPlayer1.Sliders.Shuttle
myPlayer1.Sliders.AudioVolume = trackBar2;
Note: With audio (music) only, the playback position returned by MCI with other than normal playback speed is not always correct.
The MediaEvents.
MediaEvents
The MediaPositionChanged event is a little bit different from the other ones because it gets sent very often when a mediafile is playing and it involves a timer. It informs you about a player's playback position change when a mediafile is playing and you could use it to update some position indicators on the screen, like a slider (if you're not using the player's positionslider) and/or some 'counters':
MediaPositionChanged
private void myPlayer1_MediaPositionChanged(object sender, PositionChangedEventArgs e)
{
myTrackBar1.Value = e.FromStart.TotalMilliseconds;
label1.Text = e.FromStart.ToString();
label2.Text = e.ToEnd.ToString();
}
By using myPlayer1.TimerInterval, you can change the frequency by which the mediapositionchanged events are sent (default 100ize).
To display player audio peak level data you'll need the "Stereo Mix" device/codec. If the "Stereo Mix" device is not available on your computer, you either have to enable it (in the system sound input control panel - right click and select "Show All" if not visible, then right click the device and select "Enable") or download (free from (on the home page select "Downloads" and on the next page "High Definition Audio Codecs")) and install it. For more information please search the internet with 'stereo mix'.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
myPlayer.Play(myMovie);
myPlayer.Position.Track = 0.5;
myRecorder2.InputDevices.Index = 0
If myRecorder2.LastError = true Then
msgbox("Error")
else
msgbox("OK")
end if
if (myRecorder.InputDevices.Count == 0) // ... no input devices
string[] deviceNames = myRecorder.InputDevices.GetDevices();
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.