Hej
Så fik jeg jo næsten hele min media-player til at virke, men nu driller den mig, fordi den konstant looper det første sekund af det medie jeg forsøger at afspille. Lige nu ser MainWindow.xaml således ud:
Kode der er udkommenteret er gamle forsøg, som ikke virkede.
<Window x:Class="Media_Player.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="iRonnie Media Player" Height="408.209" Width="947.388">
<Grid>
<MediaElement Name="mediaElement1" HorizontalAlignment="Left" Height="273" Margin="10,10,0,0" VerticalAlignment="Top" Width="919" LoadedBehavior="Manual" />
<Slider Name="slider1" HorizontalAlignment="Left" Margin="10,301,0,0" VerticalAlignment="Top" Width="919" ValueChanged="SeekToMediaPosition" Maximum="100"/>
<Slider Name="Lyd" HorizontalAlignment="Left" Margin="836,346,0,0" VerticalAlignment="Top" Width="93" Maximum="1" Value="1" ValueChanged="Slider_ValueChanged_1"/>
<Label Content="Volume
" HorizontalAlignment="Left" Margin="780,346,0,0" VerticalAlignment="Top"/>
<Button Name="Play" HorizontalAlignment="Left" Margin="10,328,0,0" VerticalAlignment="Top" Width="47" Height="40" Click="Button_Click_1">
<Image Source="images\Actions-media-playback-start-icon.png" />
</Button>
<Button Name="Stop" HorizontalAlignment="Left" Margin="62,328,0,0" VerticalAlignment="Top" Width="44" Height="40" Click="Button_Click_2">
<Image Source="images\Actions-media-playback-stop-icon.png" />
</Button>
<Button HorizontalAlignment="Left" Margin="111,328,0,0" VerticalAlignment="Top" Width="44" Height="40" Click="Button_Click_3">
<Image Source="images\Actions-media-playback-pause-icon.png" />
</Button>
<Button HorizontalAlignment="Left" Margin="733,328,0,0" VerticalAlignment="Top" Width="42" Height="40" Click="Button_Click_4">
<Image Source="images\Download-icon.png" />
</Button>
</Grid>
</Window>
MainWindow.xaml.cs ser således ud:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- 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.Media.Animation;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
-
- namespace Media_Player
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
- //Play
- private void Button_Click_1(object sender, RoutedEventArgs e)
- {
- mediaElement1.Play();
- }
- //Stop
- private void Button_Click_2(object sender, RoutedEventArgs e)
- {
- mediaElement1.Stop();
- }
- //Pause
- private void Button_Click_3(object sender, RoutedEventArgs e)
- {
- mediaElement1.Pause();
- }
- //Åbn fil
- private void Button_Click_4(object sender, RoutedEventArgs e)
- {
- OpenFileDialog ofd;
- ofd = new OpenFileDialog();
- ofd.AddExtension = true;
- ofd.DefaultExt = "*.*";
- 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(Time_tick);
- dispatchertimer.Interval = new TimeSpan(0, 0, 1);
- dispatchertimer.Start();
- }
- //Timer
- void Time_tick(object sender, EventArgs e)
- {
- slider1.Value = mediaElement1.Position.TotalSeconds;
- }
- //Timer
- /*private void SliderTid_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
- {
- TimeSpan ts = TimeSpan.FromSeconds(e.NewValue);
- mediaElement1.Position = ts;
- }*/
- /*private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
- {
- TimeSpan tid = TimeSpan.FromSeconds(e.NewValue);
- mediaElement1.Position = tid;
- }*/
- //Lyd
- private void Slider_ValueChanged_1(object sender, RoutedPropertyChangedEventArgs<double> e)
- {
- mediaElement1.Volume = Lyd.Value;
- }
- private void mediaElement1_MediaOpened(object sender, RoutedEventArgs e)
- {
- if (mediaElement1.NaturalDuration.HasTimeSpan)
- {
- /*TimeSpan totaltid = TimeSpan.FromMilliseconds(mediaElement1.NaturalDuration.TimeSpan.TotalMilliseconds);
- slider1.Maximum = totaltid.TotalSeconds;
- tidalt = Convert.ToDouble(slider1.Maximum);*/
- slider1.Maximum = mediaElement1.NaturalDuration.TimeSpan.TotalMilliseconds;
- }
-
- }
- private void mediaElement1_MediaEnded(object sender, EventArgs e)
- {
- mediaElement1.Stop();
- }
- private void SeekToMediaPosition(object sender, RoutedPropertyChangedEventArgs<double> args)
- {
- int SliderValue = (int)slider1.Value;
- TimeSpan ts = new TimeSpan(0, 0, 0, 0, SliderValue);
- mediaElement1.Position = ts;
- }
-
-
- }
- }
Når vi laver et gennemløb ved at trykke F11, looper programmet konstant mellem linje 63 og linje 66.
Har forsøgt afspilning af lydfiler(mp3) og videofiler(mp4).
Hvor er det lige det går galt?
Indlæg senest redigeret d. 28.11.2012 10:18 af Bruger #17072