Entwickler-Ecke

C# - Die Sprache - Einstiegsprogramm


cshark - So 02.01.11 18:05
Titel: Einstiegsprogramm
Hallo,

ich bin c# beginner mit PHP Erfahrung und möchte einen Videoplayer mit einfachen Funktionen schreiben, welcher zumindest divx abspielen kann. Ist das schwer, und was sollte ich mir da anschauen?

Mfg cshark


Th69 - So 02.01.11 18:26

Es kommt drauf an, ob du WinForms oder gleich schon WPF programmieren willst.
Für WinForms:
- das WindowsMediaPlayer-Control [http://msdn.microsoft.com/en-us/library/dd562388%28v=VS.85%29.aspx] als ActiveX (COM)-Control

Für WPF:
- die MediaPlayer bzw. MediaElement-Klasse

Um DivX-Videos abzuspielen, genügt es ja, dann den entsprechenden Codec installiert zu haben.


cshark - So 02.01.11 20:51

Ich werde mich wohl für die WPF entscheiden. Dieses Testscript habe ich probiert es funktioniert aber nicht einmal der play-button. Hier der code:


XML-Daten
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="SDKSample.MediaElementExample" >

    <StackPanel Background="Black">

        <!-- To interactively stop, pause, and play the media, the LoadedBehavior 
           property of the MediaElement must be set to "Manual". -->

        <MediaElement Source="D:\Filme\test_wmv.wmv" Name="myMediaElement" 
     Width="450" Height="250" LoadedBehavior="Manual" UnloadedBehavior="Stop" Stretch="Fill" 
     MediaOpened="Element_MediaOpened" MediaEnded="Element_MediaEnded"/>

        <StackPanel HorizontalAlignment="Center" Width="450" Orientation="Horizontal">

            <!-- Play button. -->
            <StackPanel>
                <Button Content="Play" MouseDown="OnMouseDownPlayMedia"/>
            </StackPanel>
            <Image Source="images\UI_play.gif" MouseDown="OnMouseDownPlayMedia" Margin="5"/>

            <!-- Pause button. -->
            <Image Source="images\UI_pause.gif" MouseDown="OnMouseDownPauseMedia" Margin="5" />

            <!-- Stop button. -->
            <Image Source="images\UI_stop.gif" MouseDown="OnMouseDownStopMedia" Margin="5" />

            <!-- Volume slider. This slider allows a Volume range between 0 and 1. -->
            <TextBlock Foreground="White" VerticalAlignment="Center" Margin="5"  >Volume</TextBlock>
            <Slider Name="volumeSlider" VerticalAlignment="Center" ValueChanged="ChangeMediaVolume" 
       Minimum="0" Maximum="1" Value="0.5" Width="70"/>

            <!-- Volume slider. This slider allows a Volume range between 0 and 10. -->
            <TextBlock Foreground="White" Margin="5"  VerticalAlignment="Center">Speed</TextBlock>
            <Slider Name="speedRatioSlider" VerticalAlignment="Center" ValueChanged="ChangeMediaSpeedRatio" 
       Value="1" Width="70" />

            <!-- Seek to slider. Ths slider allows you to jump to different parts of the media playback. -->
            <TextBlock Foreground="White" Margin="5"  VerticalAlignment="Center">Seek To</TextBlock>
            <Slider Name="timelineSlider" Margin="5" ValueChanged="SeekToMediaPosition" Width="70"/>

        </StackPanel>
    </StackPanel>
</Page>



C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
    
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Input;

namespace SDKSample
{

    public partial class MediaElementExample : Page
    {

        // Play the media.
        void OnMouseDownPlayMedia(object sender, MouseButtonEventArgs args)
        {

            // The Play method will begin the media if it is not currently active or 
            // resume media if it is paused. This has no effect if the media is
            // already running.
            MessageBox.Show("Play!");
            myMediaElement.Play();

            // Initialize the MediaElement property values.
            InitializePropertyValues();

        }

        // Pause the media.
        void OnMouseDownPauseMedia(object sender, MouseButtonEventArgs args)
        {

            // The Pause method pauses the media if it is currently running.
            // The Play method can be used to resume.
            myMediaElement.Pause();

        }

        // Stop the media.
        void OnMouseDownStopMedia(object sender, MouseButtonEventArgs args)
        {

            // The Stop method stops and resets the media to be played from
            // the beginning.
            myMediaElement.Stop();

        }

        // Change the volume of the media.
        private void ChangeMediaVolume(object sender, RoutedPropertyChangedEventArgs
<double>args)
        {
            myMediaElement.Volume = (double)volumeSlider.Value;
        }

        // Change the speed of the media.
        private void ChangeMediaSpeedRatio(object sender, RoutedPropertyChangedEventArgs
    <double>args)
        {
            myMediaElement.SpeedRatio = (double)speedRatioSlider.Value;
        }

        // When the media opens, initialize the "Seek To" slider maximum value
        // to the total number of miliseconds in the length of the media clip.
        private void Element_MediaOpened(object sender, EventArgs e)
        {
            timelineSlider.Maximum = myMediaElement.NaturalDuration.TimeSpan.TotalMilliseconds;
        }

        // When the media playback is finished. Stop() the media to seek to media start.
        private void Element_MediaEnded(object sender, EventArgs e)
        {
            myMediaElement.Stop();
        }

        // Jump to different parts of the media (seek to). 
        private void SeekToMediaPosition(object sender, RoutedPropertyChangedEventArgs
        <double>args)
        {
            int SliderValue = (int)timelineSlider.Value;

            // Overloaded constructor takes the arguments days, hours, minutes, seconds, miniseconds.
            // Create a TimeSpan with miliseconds equal to the slider value.
            TimeSpan ts = new TimeSpan(0000, SliderValue);
            myMediaElement.Position = ts;
        }

        void InitializePropertyValues()
        {
            // Set the media's starting Volume and SpeedRatio to the current value of the
            // their respective slider controls.
            myMediaElement.Volume = (double)volumeSlider.Value;
            myMediaElement.SpeedRatio = (double)speedRatioSlider.Value;
        }

    }
}


Mfg cshark