wpf实现简单媒体播放效果

前台:

<Window x:Class="WpfApplication1.Window3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="媒体播放器" Height="500" Width="800" Loaded="Window_Loaded">
    <Grid Background="#2D2C31">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="600"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid Grid.Column="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="30"></RowDefinition>
                <RowDefinition Height="155*"></RowDefinition>
                <RowDefinition Height="276*" />
            </Grid.RowDefinitions>
            <Label Grid.Row="0"   VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="16" Foreground="#FF9AE500">播放列表</Label>
            <ListView  Name="listView1" SelectionMode="Single" IsSynchronizedWithCurrentItem="{x:Null}"  Margin="1,1,5,5" Grid.Row="1" Background="#2D2C31" Foreground="#FF9AE500" FontSize="13" Grid.RowSpan="2" SelectionChanged="listView1_SelectionChanged">
                <ListBox.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="添加单个媒体文件" Click="MenuItem_Click">
                        </MenuItem>
                        <MenuItem Header="删除单个媒体文件"  Click="MenuItem_Click_1">
                        </MenuItem>
                        <MenuItem Header="清空列表"  Click="MenuItem_Click_2">
                        </MenuItem>
                    </ContextMenu>
                </ListBox.ContextMenu>
            </ListView>
        </Grid>
        <Grid Grid.Column="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="40"></RowDefinition>
            </Grid.RowDefinitions>
            <MediaElement  LoadedBehavior="Manual" Volume="{Binding ElementName=volumeSlider, Path=Value}" Grid.Row="0" Name="mediaElement1" Margin="10,10,5,10" MediaEnded="mediaElement1_MediaEnded" />
            <Grid Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="150"></ColumnDefinition>
                    <ColumnDefinition Width="80"></ColumnDefinition>
                    <ColumnDefinition Width="80"></ColumnDefinition>
                    <ColumnDefinition Width="80"></ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Button Background="{x:Null}" Content="播 放" Cursor="Hand" FontSize="14" Foreground="#FF9AE500" Margin="80,5,20,5" Name="button3" Grid.Column="0" Click="button3_Click" />
                <Button Background="{x:Null}" Content="停 止" Cursor="Hand" FontSize="14" Foreground="#FF9AE500" Margin="20,5,10,5" Name="button4" Grid.Column="1" Click="button4_Click" />
                <Button Background="{x:Null}" Content="快 进" Cursor="Hand" FontSize="14" Foreground="#FF9AE500" Margin="20,5,10,5" Name="button5" Grid.Column="2" Click="button5_Click" />
                <Button Background="{x:Null}" Content="快 退" Cursor="Hand" FontSize="14" Foreground="#FF9AE500" Margin="20,5,10,5" Name="button6" Grid.Column="3" Click="button6_Click" />
                <TextBlock Grid.Row="4" Margin="4,10,4,5" Name="textBlock1" Text="音量:" Foreground="#FF9AE500" FontSize="14" Grid.Column="4" />
                <Slider x:Name="volumeSlider" Minimum="0" Maximum="1" Value="0.5" Margin="37,10,0,13" Grid.Column="4" HorizontalAlignment="Left" Width="159" />
            </Grid>
        </Grid>
    </Grid>
</Window>
View Code


后台:

  1        string root = "", pathMedia = "";
  2         StringBuilder sb = new StringBuilder();
  3         List<string> str8 = new List<string>();
  4         byte[] bs;
  5         string path = "D:\\";
  6         public Window3()
  7         {
  8             InitializeComponent();
  9             InitPath();
 10         }
 11         public void InitPath()
 12         {
 13           
 14         }
 15         private void listView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
 16         {
 17             string fileName = this.listView1.SelectedValue.ToString();
 18             this.mediaElement1.Source = new Uri(path + "//" + fileName);  
 19             this.mediaElement1.Play();  
 20         }
 21 
 22         private void MenuItem_Click(object sender, RoutedEventArgs e)
 23         {
 24             OpenFileDialog ofp = new OpenFileDialog();
 25             ofp.Multiselect = true;
 26             ofp.ShowDialog();
 27             if (ofp.FileName == "")
 28             {
 29                 MessageBox.Show("请选择正确的文件");
 30                 return;
 31             }
 32 
 33             for (int i = 0; i <= ofp.FileNames.Count() - 1; i++)
 34             {
 35                 sb.Append(ofp.FileNames[i]);
 36                 sb.Append("\r\n");
 37                 if (ofp.SafeFileNames[i] != "")
 38                 {
 39                     listView1.Items.Add(ofp.SafeFileNames[i]);
 40                     str8.Add(ofp.FileNames[i].Substring(0, ofp.FileNames[i].LastIndexOf("\\") + 1));
 41                 }
 42             }
 43             FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "\\媒体列表.txt", FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
 44             bs = Encoding.Default.GetBytes(sb.ToString());
 45             fs.Write(bs, 0, bs.Length);
 46             fs.Flush();
 47             fs.Close();
 48 
 49         }
 50 
 51         private void MenuItem_Click_1(object sender, RoutedEventArgs e)
 52         {
 53             try
 54             {
 55                 FileStream fs1 = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "\\媒体列表.txt", FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite);
 56                 byte[] bt = new byte[fs1.Length];
 57                 fs1.Read(bt, 0, bt.Length);
 58                 fs1.Dispose();
 59                 string str = Encoding.Default.GetString(bt);
 60                 string[] s = str.Split(new string[] { "\r\n" }, StringSplitOptions.None);
 61                 List<string> list = new List<string>(s);
 62                 if (listView1.SelectedItem != null)
 63                 {
 64                     for (int i = 0; i < s.Length - 1; i++)
 65                     {
 66                         if (s[i].Contains(listView1.SelectedItem.ToString()))
 67                         {
 68                             list.RemoveAt(i);
 69                         }
 70                     }
 71                 }
 72                 s = (string[])list.ToArray();
 73                 FileInfo file = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "\\媒体列表.txt");
 74                 file.Refresh();
 75                 file.Delete();
 76                 List<string> li = new List<string>();
 77                 li = s.Distinct().ToList();
 78                 StringBuilder sb = new StringBuilder();
 79                 for (int i = 0; i <= li.Count - 1; i++)
 80                 {
 81                     sb.Append(li[i]);
 82                     sb.Append("\r\n");
 83                 }
 84                 FileStream fs2 = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "\\媒体列表.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
 85                 bs = Encoding.Default.GetBytes(sb.ToString());
 86                 fs2.Write(bs, 0, bs.Length);
 87                 fs2.Flush();
 88                 fs2.Close();
 89             }
 90             catch
 91             {
 92 
 93             }
 94 
 95             listView1.Items.Remove(listView1.SelectedItem);
 96 
 97         }
 98 
 99         private void MenuItem_Click_2(object sender, RoutedEventArgs e)
100         {
101             listView1.Items.Clear();
102             FileInfo file = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "\\媒体列表.txt");
103             file.Refresh();
104             file.Delete();
105 
106         }
107 
108         private void Window_Loaded(object sender, RoutedEventArgs e)
109         {
110 
111         }
112         private void button3_Click(object sender, RoutedEventArgs e)
113         {
114               if (button3.Content.ToString() == "播 放")  
115             {  
116                 mediaElement1.Play();  
117                 button3.Content = "暂 停";  
118             }  
119             else  
120             {  
121                 mediaElement1.Pause();  
122                 button3.Content = "播 放";  
123            }  
124         }
125 
126         private void button4_Click(object sender, RoutedEventArgs e)
127         {
128             this.mediaElement1.Stop();  
129         }
130 
131         private void button5_Click(object sender, RoutedEventArgs e)
132         {
133             mediaElement1.Position = mediaElement1.Position + TimeSpan.FromSeconds(10);  
134         }
135 
136         private void button6_Click(object sender, RoutedEventArgs e)
137         {
138             mediaElement1.Position = mediaElement1.Position - TimeSpan.FromSeconds(10);  
139         }
View Code

 

posted @ 2013-05-23 10:17  蔡云云  阅读(242)  评论(0编辑  收藏  举报