N
N
Nikolai Kupstas2011-08-14 17:23:14
C++ / C#
Nikolai Kupstas, 2011-08-14 17:23:14

Player in C#

What libraries can be used to write an audio and video player?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
M
MercuryShine, 2011-08-14
@MercuryShine

How about using the class class Microsoft.DirectX.AudioVideoPlayback.Video ?

P
Pavel Zagrebelin, 2011-08-14
@Zagrebelion

System.Windows.Controls.MediaElement, there are Play, Stop, Pause methods and Position and Volume properties. Well, the rest of the WPF goodies.

E
Eugene, 2011-08-14
@csfmeridian

Bass audio library + wrapper for .NET (in the same place, on the site).
Wikipedia

D
Denis Domansky, 2011-08-14
@Doman

What level player? Just Play/Stop/Pause or a lot of settings, options, etc.?

A
Anton Antonov, 2019-04-04
@antonitpro

Greetings.
Perhaps late, but still ...
Perhaps the option will suit you (WnForms, c#):

using Microsoft.DirectX.AudioVideoPlayback;


private Video video;
private string[] videoPaths;
private string folderPath = @"C:\Users\Dell XPS\Desktop\Videos\";
private int selectedIndex = 0;
private Size formSize;
private Size pnlSize;

public Form1()
{
    InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
    formSize = new Size(this.Width, this.Height);
    pnlSize = new Size(pnlVideo.Width, pnlVideo.Height);

    videoPaths = Directory.GetFiles(folderPath, "*.wmv");

    if (videoPaths != null)
    {
        foreach (string path in videoPaths)
        {
            string vid = path.Replace(folderPath, string.Empty);
            vid = vid.Replace(".wmv", string.Empty);
            lstVideos.Items.Add(vid);
        }
    }
    lstVideos.SelectedIndex = selectedIndex;
}

private void lstVideos_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        video.Stop();
        video.Dispose();
    }
    catch { }

    int index = lstVideos.SelectedIndex;
    selectedIndex = index;
    video = new Video(videoPaths[index], false);
    video.Owner = pnlVideo;
    pnlVideo.Size = pnlSize;
    video.Play();
    tmrVideo.Enabled = true;
    btnPlayPause.Text = "Pause";
    video.Ending += Video_Ending;
    lblVideo.Text = lstVideos.Text;
}

private void Video_Ending(object sender, EventArgs e)
{
    Task.Factory.StartNew(() =>
    {
        System.Threading.Thread.Sleep(2000);

        if (InvokeRequired)
        {
            this.Invoke(new Action(() =>
            {
                NextVideo();
            }));
        }
    });
}

private void NextVideo()
{
    int index = lstVideos.SelectedIndex;
    index++;
    if (index > videoPaths.Length - 1)
        index = 0;
    selectedIndex = index;
    lstVideos.SelectedIndex = index;
}

private void btnNext_Click(object sender, EventArgs e)
{
    NextVideo();
}

private void btnPrevious_Click(object sender, EventArgs e)
{
    PreviousVideo();
}

private void PreviousVideo()
{
    int index = lstVideos.SelectedIndex;
    index--;
    if (index == -1)
        index = videoPaths.Length - 1;
    selectedIndex = index;
    lstVideos.SelectedIndex = index;
}

private void btnPlayPause_Click(object sender, EventArgs e)
{
    if (!video.Playing)
    {
        video.Play();
        tmrVideo.Enabled = true;
        btnPlayPause.Text = "Pause";
    }
    else if (video.Playing)
    {
        video.Pause();
        tmrVideo.Enabled = false;
        btnPlayPause.Text = "Play";
    }
}

private void btnFullscreen_Click(object sender, EventArgs e)
{
    FormBorderStyle = FormBorderStyle.None;
    WindowState = FormWindowState.Maximized;
    video.Owner = this;
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Escape)
    {
        //exit full screen when escape is pressed
        FormBorderStyle = FormBorderStyle.Sizable;
        WindowState = FormWindowState.Normal;
        this.Size = formSize;
        video.Owner = pnlVideo;
        pnlVideo.Size = pnlSize;
    }
}

private void trackVolume_Scroll(object sender, EventArgs e)
{
    video.Audio.Volume = trackVolume.Value;
}

private void btnVolume_Click(object sender, EventArgs e)
{
    trackVolume.Visible = !trackVolume.Visible;
}

private void tmrVideo_Tick(object sender, EventArgs e)
{
    int currentTime = Convert.ToInt32(video.CurrentPosition);
    int maxTime = Convert.ToInt32(video.Duration);

    lblVideoPosition.Text = string.Format("{0:00}:{1:00}:{2:00}", currentTime / 3600, (currentTime / 60) % 60, currentTime % 60)
                            + " / " +
                            string.Format("{0:00}:{1:00}:{2:00}", maxTime / 3600, (maxTime / 60) % 60, maxTime % 60);
}

S
SkyRZN, 2011-08-14
@SkyRZN

If you are interested in a video player, then the easiest way is probably to dig towards DirectShow. You can see an example player here: http://www.codeproject.com/KB/directx/directshowmediaplayer.aspx

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question