Sunday 22 December 2013

Tutorial membuat video player sederhana menggunakan c#



Bukalah Visual studio dengan bahasa pemograman C#. Pilih Windows WMP dan create
Design form dari menu toolbox denganmenambahkan MediaElement, buttton, slider dan label. Designlah seperti gambar diatas.
masukkan gambar ikon play, pause stop dan open drive dengan cara:
1. copy gambar ikon dan paste di bin aplikasi Video Player
2. buka jendela XAML kemudian gantilah konten button menjadi :
     <Image Source="D:\SD\coba2\WpfApplication1\WpfApplication1\images (5).jpg" />
Tambahkan reference dengan cara :
1. Klik kanan pada WpfAplication1
2. Klik Add Reference
3. Pilih .NET
4. Pilih System.Windows.Form
Masukkan source code dari MediaElement, Button dan slider seperti dibawah ini:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
       
     
        void timer_Tick(object sender, EventArgs e)
        {
            slider1.Value = mediaElement1.Position.TotalSeconds;
        }
     
     
        private void button4_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd;
            ofd = new OpenFileDialog();
            ofd.AddExtension = true;
            ofd.Filter = "Media Files (*.*)|*.*";
            ofd.ShowDialog();

            try { mediaElement1.Source = new Uri(ofd.FileName); }
            catch { new NullReferenceException("Error"); }
            System.Windows.Threading.DispatcherTimer dispatcherTimer = new                            System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(timer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
            dispatcherTimer.Start();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            mediaElement1.Stop();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            mediaElement1.Pause();
        }

        private void button1_Click_1(object sender, RoutedEventArgs e)
        {
            mediaElement1.Play();
        }

        private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            TimeSpan ts = TimeSpan.FromSeconds(e.NewValue);
            mediaElement1.Position = ts;

        }

        private void slider2_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            mediaElement1.Volume = slider2.Value;
        }

        private void mediaElement1_MediaOpened(object sender, RoutedEventArgs e)
        {
            if (mediaElement1.NaturalDuration.HasTimeSpan)
            {
                TimeSpan ts = TimeSpan.FromMilliseconds(mediaElement1.NaturalDuration.TimeSpan.TotalMilliseconds);
                slider1.Maximum = ts.TotalSeconds;

            }
        }
    }
}

Setelah selesai memasukkan souce code, klik build solution pada menu build untuk memeriksa tidak ada yang error. Setelah itu, program dapat dijalankan(di debug) dengan cara mengklik menu Debug pada toolbar atau dengan cara menekan tombol F5.



Selamat mencoba...
Terima Kasih ^_^