win8文件操作以及音视频的应用
win8文件操作有好幾種方式,可以采用txt文件,也可以采用xml操作。由于win8中xml操作與wp7還是有所不同的,所以本文采用txt操作。下面以一個小應用:《中國十大名曲音視頻賞析》為例子進行解析一下具體實現:(本應用基本完成,處于調試,基本功能都實現。采用部分核心代碼粘貼,最后附加全部代碼!)
一 首先進行頁面布局和數據綁定:(關于頁面布局的核心知識點梳理)
1,利用資源文件
View Code 1 <!-- 2 主題:資源文件引用 3 描述:利用Resources定義資源文件,資源目標只需要引用模板項key名就行了,本例:key名ListItemTemplate 4 開發者:科眾工作室 5 --> 6 <Page.Resources> 7 <BitmapImage x:Key="PlayImage" UriSource="Images/Playicon.png"></BitmapImage> 8 <BitmapImage x:Key="PauseImage" UriSource="Images/Pauseicon.png"></BitmapImage> 9 <DataTemplate x:Key="ListItemTemplate"> 10 <Grid Height="110" Margin="6"> 11 <Grid.ColumnDefinitions> 12 <ColumnDefinition Width="Auto"> 13 </ColumnDefinition> 14 <ColumnDefinition Width="*"></ColumnDefinition> 15 </Grid.ColumnDefinitions> 16 <Border Background="Blue" Width="110" Height="110"> 17 <Image Source="{Binding uri}" Stretch="UniformToFill"></Image> 18 </Border> 19 <StackPanel Grid.Column="1" VerticalAlignment="Center" Margin="10,0,0,0"> 20 <TextBlock Foreground="Red" Text="{Binding Title}" Style="{StaticResource TitleTextStyle}" TextWrapping="Wrap"></TextBlock> 21 </StackPanel> 22 </Grid> 23 </DataTemplate> 24 </Page.Resources>注:資源定義時采用模板,模板key即為目標項的綁定名。這點與ajax技術有點類似
2,引用資源進行綁定布局
View Code 1 <!-- 2 主題:圖文顯示布局 3 描述:顯示左側顯示圖片和主題,并采用項模板引用資源文件進行布局:ItemTemplate="{StaticResource ListItemTemplate}" 4 開發者:科眾工作室 5 --> 6 <ListView Name="VideoListView" Grid.Row="1" ItemTemplate="{StaticResource ListItemTemplate}" 7 Grid.Column="0" Margin="10,10" BorderBrush="Black" SelectionChanged="lv1_SelectionChanged"/>3,視頻播放布局(MediaElement控件的使用,處于核心地位,可以播放音視頻,AutoPlay="False"設置為非自動播放,如果不添加則自動播放)
View Code 1 <!-- 2 主題:視頻播放 3 描述:選中所選項,點擊觸發視頻,可以控制視頻聲音大小 4 開發者:科眾工作室 5 --> 6 <GridView x:Name="PlayVideoBack" Grid.Row="1" Grid.Column="1" Width="650" Height="650" VerticalAlignment="Center" HorizontalAlignment="Center" BorderBrush="White" BorderThickness="10"> 7 <Grid x:Name="PlayVideo" VerticalAlignment="Top" HorizontalAlignment="Center" Width="600" Height="600" Background="Black" Margin="10,0,20,10"> 8 <Grid.RowDefinitions> 9 <RowDefinition Height="50"/> 10 <RowDefinition Height="500"/> 11 <RowDefinition Height="*"/> 12 </Grid.RowDefinitions> 13 <StackPanel x:Name="IconsStackPanel" Grid.Row="0" Margin="10,0,0,0" Orientation="Horizontal"> 14 <Button x:Name="OpenFileButton" ToolTipService.ToolTip="Open a Video File" BorderThickness="0,0,0,0" Click="OpenFileButton_Click_1" > 15 <Button.Content> 16 <Image x:Name="OpenFileImage" Source="Images/open.png" ></Image> 17 </Button.Content> 18 </Button> 19 <TextBlock x:Name="VolumeTitle" Text="聲音" Margin="160,0,0,0" FontSize="15" FontWeight="Bold" TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock> 20 <Slider x:Name="VolumeSlider" Margin="10,0,0,0" Width="250" Minimum="0" Maximum="100" ValueChanged="VolumeSlider_ValueChanged_1"></Slider> 21 <TextBlock x:Name="VolumeLevelText" Width="42" Text="{Binding ElementName=VolumeSlider,Path=Value}" HorizontalAlignment="Center" Margin="10,4,0,0" VerticalAlignment="Center" FontSize="16"></TextBlock> 22 </StackPanel> 23 <TextBlock x:Name="Initial_Display" Grid.Row="1" Margin="0,0,0,200" Text="Please Select a Video from List or Open a Video File to play" Visibility="Collapsed" FontWeight="SemiBold" Height="30" FontSize="22" HorizontalAlignment="Center"></TextBlock> 24 25 <MediaElement x:Name="VideoPlayer" Grid.Row="1" Grid.Column="1" Margin="0,-2,0,-2" MediaOpened="VideoPlayer_MediaOpened_1"/> 26 <Button x:Name="Play_ImageButton" Grid.Row="1" Width="100" Height="100" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="0" > 27 <Button.Content> 28 <Image Name="PlayImage" Opacity="0.5" Source="Images/Playicon.png"></Image> 29 </Button.Content> 30 </Button> 31 <Button x:Name="Stop_ImageButton" Visibility="Collapsed" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Height="100" Width="100" BorderThickness="0" Opacity="0.5"> 32 <Button.Content> 33 <Image Name="StopImage" Opacity="0.5" Source="Images/Pauseicon.png"/> 34 </Button.Content> 35 </Button> 36 <StackPanel Name="ControlsPanel" Grid.Row="2" Orientation="Horizontal"> 37 <Button x:Name="Play_Button" Width="60" BorderThickness="0" Click="Play_Button_Click_1"> 38 <Button.Content> 39 <Image x:Name="PlayButtonImage" Source="{StaticResource PlayImage}"/> 40 </Button.Content> 41 </Button> 42 <Button x:Name="Stop_Button" Visibility="Collapsed" Width="60" BorderThickness="0" Click="Stop_Button_Click_1"> 43 <Button.Content> 44 <Image x:Name="StopButtonImage" Source="Images/Pauseicon.png"/> 45 </Button.Content> 46 </Button> 47 <Slider x:Name="TimeSlider" Width="450" Margin="4,0,0,0"/> 48 <TextBlock x:Name="TimeText" Text="時間:" Margin="6,5,0,0" Width="50" FontSize="13" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center"></TextBlock> 49 </StackPanel> 50 <MediaElement HorizontalAlignment="Left" Height="100" Margin="456,53,0,0" Grid.Row="1" VerticalAlignment="Top" Width="100"/> 51 </Grid> 52 </GridView>4音頻播放,選擇項后本頁顯示圖文布局
View Code 1 <!-- 2 主題:圖文顯示布局 3 描述:選中所選項,播放音樂,點擊暫停觸發視頻,主要圖文顯示 4 開發者:科眾工作室 5 --> 6 <ScrollViewer BorderThickness="10" BorderBrush="Black" Grid.Column="2" Grid.Row="1" > 7 <StackPanel Orientation="Vertical" > 8 <Image x:Name="DescriptionImage" Margin="0,20,0,0" Width="350" Height="200" Source="{Binding uri}"/> 9 <TextBlock Foreground="Red" x:Name="Description" Margin="0,20,0,0" FontSize="16" Width="350" TextAlignment="Justify" TextWrapping="Wrap"/> 10 </StackPanel> 11 </ScrollViewer> 12 <MediaElement x:Name="Music" AutoPlay="False" Grid.Column="2" Grid.Row="1" Margin="10,-400,10,0" Height="80" Width="80"/> 13 <StackPanel x:Name="musicPanel" Grid.Column="2" Grid.Row="1" Margin="0,100,0,500" HorizontalAlignment="Center" Orientation="Vertical"> 14 <Button x:Name="Playbt" Width="60" Height="60" BorderThickness="0" Click="Playbt_Click_1" > 15 <Image x:Name="PlaybtImage" Opacity="0.5" Source="Images/Playicon.png"/> 16 </Button> 17 <Button x:Name="Stopbt" Visibility="Collapsed" Width="80" Height="80" BorderThickness="0" Click="Stopbt_Click_1"> 18 <Image x:Name="StopbtImage" Source="Images/Pauseicon.png"/> 19 </Button> 20 </StackPanel>以上頁面布局一些知識點,完整前臺xaml代碼如下:
View Code 1 <Page 2 x:Class="操作文件.MainPage" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:local="using:操作文件" 6 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 7 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 8 mc:Ignorable="d"> 9 <!-- 10 主題:資源文件引用 11 描述:利用Resources定義資源文件,資源目標只需要引用模板項key名就行了,本例:key名ListItemTemplate 12 開發者:科眾工作室 13 --> 14 <Page.Resources> 15 <BitmapImage x:Key="PlayImage" UriSource="Images/Playicon.png"></BitmapImage> 16 <BitmapImage x:Key="PauseImage" UriSource="Images/Pauseicon.png"></BitmapImage> 17 <DataTemplate x:Key="ListItemTemplate"> 18 <Grid Height="110" Margin="6"> 19 <Grid.ColumnDefinitions> 20 <ColumnDefinition Width="Auto"> 21 </ColumnDefinition> 22 <ColumnDefinition Width="*"></ColumnDefinition> 23 </Grid.ColumnDefinitions> 24 <Border Background="Blue" Width="110" Height="110"> 25 <Image Source="{Binding uri}" Stretch="UniformToFill"></Image> 26 </Border> 27 <StackPanel Grid.Column="1" VerticalAlignment="Center" Margin="10,0,0,0"> 28 <TextBlock Foreground="Red" Text="{Binding Title}" Style="{StaticResource TitleTextStyle}" TextWrapping="Wrap"></TextBlock> 29 </StackPanel> 30 </Grid> 31 </DataTemplate> 32 </Page.Resources> 33 <Grid Background="Azure"> 34 <Grid> 35 <Grid.RowDefinitions> 36 <RowDefinition Height="100"></RowDefinition> 37 <RowDefinition Height="*"></RowDefinition> 38 </Grid.RowDefinitions> 39 <Grid.ColumnDefinitions> 40 <ColumnDefinition Width="300"></ColumnDefinition> 41 <ColumnDefinition Width="650"></ColumnDefinition> 42 <ColumnDefinition Width="*"></ColumnDefinition> 43 </Grid.ColumnDefinitions> 44 <StackPanel Grid.ColumnSpan="3" VerticalAlignment="Center" Width="1200" Height="80" Margin="0,10" HorizontalAlignment="Center" > 45 <TextBlock x:Name="txtname" Text="中國十大名曲音視頻賞析" HorizontalAlignment="Center" Foreground="Black" VerticalAlignment="Center" Margin="0,20,0,0" FontSize="40" FontWeight="Bold" /> 46 </StackPanel> 47 <!-- 48 主題:圖文顯示布局 49 描述:顯示左側顯示圖片和主題,并采用項模板引用資源文件進行布局:ItemTemplate="{StaticResource ListItemTemplate}" 50 開發者:科眾工作室 51 --> 52 <ListView Name="VideoListView" Grid.Row="1" ItemTemplate="{StaticResource ListItemTemplate}" 53 Grid.Column="0" Margin="10,10" BorderBrush="Black" SelectionChanged="lv1_SelectionChanged"/> 54 <!-- 55 主題:視頻播放 56 描述:選中所選項,點擊觸發視頻,可以控制視頻聲音大小 57 開發者:科眾工作室 58 --> 59 <GridView x:Name="PlayVideoBack" Grid.Row="1" Grid.Column="1" Width="650" Height="650" VerticalAlignment="Center" HorizontalAlignment="Center" BorderBrush="White" BorderThickness="10"> 60 <Grid x:Name="PlayVideo" VerticalAlignment="Top" HorizontalAlignment="Center" Width="600" Height="600" Background="Black" Margin="10,0,20,10"> 61 <Grid.RowDefinitions> 62 <RowDefinition Height="50"/> 63 <RowDefinition Height="500"/> 64 <RowDefinition Height="*"/> 65 </Grid.RowDefinitions> 66 <StackPanel x:Name="IconsStackPanel" Grid.Row="0" Margin="10,0,0,0" Orientation="Horizontal"> 67 <Button x:Name="OpenFileButton" ToolTipService.ToolTip="Open a Video File" BorderThickness="0,0,0,0" Click="OpenFileButton_Click_1" > 68 <Button.Content> 69 <Image x:Name="OpenFileImage" Source="Images/open.png" ></Image> 70 </Button.Content> 71 </Button> 72 <TextBlock x:Name="VolumeTitle" Text="聲音" Margin="160,0,0,0" FontSize="15" FontWeight="Bold" TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock> 73 <Slider x:Name="VolumeSlider" Margin="10,0,0,0" Width="250" Minimum="0" Maximum="100" ValueChanged="VolumeSlider_ValueChanged_1"></Slider> 74 <TextBlock x:Name="VolumeLevelText" Width="42" Text="{Binding ElementName=VolumeSlider,Path=Value}" HorizontalAlignment="Center" Margin="10,4,0,0" VerticalAlignment="Center" FontSize="16"></TextBlock> 75 </StackPanel> 76 <TextBlock x:Name="Initial_Display" Grid.Row="1" Margin="0,0,0,200" Text="Please Select a Video from List or Open a Video File to play" Visibility="Collapsed" FontWeight="SemiBold" Height="30" FontSize="22" HorizontalAlignment="Center"></TextBlock> 77 78 <MediaElement x:Name="VideoPlayer" Grid.Row="1" Grid.Column="1" Margin="0,-2,0,-2" MediaOpened="VideoPlayer_MediaOpened_1"/> 79 <Button x:Name="Play_ImageButton" Grid.Row="1" Width="100" Height="100" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="0" > 80 <Button.Content> 81 <Image Name="PlayImage" Opacity="0.5" Source="Images/Playicon.png"></Image> 82 </Button.Content> 83 </Button> 84 <Button x:Name="Stop_ImageButton" Visibility="Collapsed" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Height="100" Width="100" BorderThickness="0" Opacity="0.5"> 85 <Button.Content> 86 <Image Name="StopImage" Opacity="0.5" Source="Images/Pauseicon.png"/> 87 </Button.Content> 88 </Button> 89 <StackPanel Name="ControlsPanel" Grid.Row="2" Orientation="Horizontal"> 90 <Button x:Name="Play_Button" Width="60" BorderThickness="0" Click="Play_Button_Click_1"> 91 <Button.Content> 92 <Image x:Name="PlayButtonImage" Source="{StaticResource PlayImage}"/> 93 </Button.Content> 94 </Button> 95 <Button x:Name="Stop_Button" Visibility="Collapsed" Width="60" BorderThickness="0" Click="Stop_Button_Click_1"> 96 <Button.Content> 97 <Image x:Name="StopButtonImage" Source="Images/Pauseicon.png"/> 98 </Button.Content> 99 </Button> 100 <Slider x:Name="TimeSlider" Width="450" Margin="4,0,0,0"/> 101 <TextBlock x:Name="TimeText" Text="時間:" Margin="6,5,0,0" Width="50" FontSize="13" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center"></TextBlock> 102 </StackPanel> 103 <MediaElement HorizontalAlignment="Left" Height="100" Margin="456,53,0,0" Grid.Row="1" VerticalAlignment="Top" Width="100"/> 104 </Grid> 105 </GridView> 106 107 108 109 110 <!-- 111 主題:圖文顯示布局 112 描述:選中所選項,播放音樂,點擊暫停觸發視頻,主要圖文顯示 113 開發者:科眾工作室 114 --> 115 <ScrollViewer BorderThickness="10" BorderBrush="Black" Grid.Column="2" Grid.Row="1" > 116 <StackPanel Orientation="Vertical" > 117 <Image x:Name="DescriptionImage" Margin="0,20,0,0" Width="350" Height="200" Source="{Binding uri}"/> 118 <TextBlock Foreground="Red" x:Name="Description" Margin="0,20,0,0" FontSize="16" Width="350" TextAlignment="Justify" TextWrapping="Wrap"/> 119 </StackPanel> 120 </ScrollViewer> 121 <MediaElement x:Name="Music" AutoPlay="False" Grid.Column="2" Grid.Row="1" Margin="10,-400,10,0" Height="80" Width="80"/> 122 <StackPanel x:Name="musicPanel" Grid.Column="2" Grid.Row="1" Margin="0,100,0,500" HorizontalAlignment="Center" Orientation="Vertical"> 123 <Button x:Name="Playbt" Width="60" Height="60" BorderThickness="0" Click="Playbt_Click_1" > 124 <Image x:Name="PlaybtImage" Opacity="0.5" Source="Images/Playicon.png"/> 125 </Button> 126 <Button x:Name="Stopbt" Visibility="Collapsed" Width="80" Height="80" BorderThickness="0" Click="Stopbt_Click_1"> 127 <Image x:Name="StopbtImage" Source="Images/Pauseicon.png"/> 128 </Button> 129 </StackPanel> 130 </Grid> 131 132 </Grid> 133 </Page>到這里有沒有相當界面究竟怎么樣呢?那么先一睹為快吧,隨后再討論數據綁定:
細心的你已經發現了,左側圖片文字前臺并沒有綁定,下面看看具體綁定吧:
?
View Code 1 public sealed partial class MainPage : Page 2 { 3 List<ImageDescription> VideosList = new List<ImageDescription>();//對ImageDescription屬性操作集合 4 private Dictionary<int, string> Description_uri = new Dictionary<int, string>();//文本內容的鍵值集合 5 DispatcherTimer ticks = new DispatcherTimer();//時間間隔處理 6 private Dictionary<int, string> videouri = new Dictionary<int, string>();//視頻內容的鍵值集合 7 private Dictionary<int, string> musicuri = new Dictionary<int, string>();//音頻內容的鍵值集合 8 bool isplaying = false; 9 bool isstop = false; 10 public MainPage() 11 { 12 this.InitializeComponent(); 13 videouri.Add(0, "ms-appx:///music/1.wma"); 14 videouri.Add(1, "ms-appx:///music/2.wma"); 15 videouri.Add(2, "ms-appx:///music/3.wma"); 16 videouri.Add(3, "ms-appx:///music/4.wma"); 17 videouri.Add(4, "ms-appx:///music/5.wma"); 18 videouri.Add(5, "ms-appx:///music/6.wma"); 19 20 musicuri.Add(0, "ms-appx:///Videos/11.avi"); 21 musicuri.Add(1, "ms-appx:///Videos/video2.wmv"); 22 musicuri.Add(2, "ms-appx:///Videos/video3.wmv"); 23 musicuri.Add(3, "ms-appx:///Videos/video4.wmv"); 24 musicuri.Add(4, "ms-appx:///Videos/video5.wmv"); 25 musicuri.Add(5, "ms-appx:///Videos/11.avi"); 26 27 Description_uri.Add(0, "ms-appx:///Description/video1.txt"); 28 Description_uri.Add(1, "ms-appx:///Description/video2.txt"); 29 Description_uri.Add(2, "ms-appx:///Description/video3.txt"); 30 Description_uri.Add(3, "ms-appx:///Description/video4.txt"); 31 Description_uri.Add(4, "ms-appx:///Description/video5.txt"); 32 Description_uri.Add(5, "ms-appx:///Description/video6.txt"); 33 //VideosList.Add(new ImageDescription { Title = "Fishes", uri = "ms-appx:///Images/1.jpg" }); 34 //VideosList.Add(new ImageDescription { Title = "Nature", uri = "ms-appx:///Images/2.jpg" }); 35 //VideosList.Add(new ImageDescription { Title = "Twister", uri = "ms-appx:///Images/3.jpg" }); 36 //VideosList.Add(new ImageDescription { Title = "Flower", uri = "ms-appx:///Images/4.jpg" }); 37 //VideosList.Add(new ImageDescription { Title = "Insect", uri = "ms-appx:///Images/5.jpg" }); 38 //VideosList.Add(new ImageDescription { Title = "Natural Scene", uri = "ms-appx:///Images/6.jpg" }); 39 40 VideosList.Add(new ImageDescription { Title = "瀟湘水云", uri = "ms-appx:///image/瀟湘水云1.jpg" }); 41 VideosList.Add(new ImageDescription { Title = "廣陵散", uri = "ms-appx:///image/廣陵散2.jpg" }); 42 VideosList.Add(new ImageDescription { Title = "高山流水", uri = "ms-appx:///image/高山流水3.jpg" }); 43 VideosList.Add(new ImageDescription { Title = "漁樵問答", uri = "ms-appx:///image/漁樵問答4.jpg" }); 44 VideosList.Add(new ImageDescription { Title = "平沙落雁", uri = "ms-appx:///image/平沙落雁5.jpg" }); 45 VideosList.Add(new ImageDescription { Title = "陽春白雪", uri = "ms-appx:///image/陽春白雪6.jpg" }); 46 VideosList.Add(new ImageDescription { Title = "胡笳十八拍", uri = "ms-appx:///image/胡笳十八拍7.jpg" }); 47 VideosList.Add(new ImageDescription { Title = "陽關三疊", uri = "ms-appx:///image/陽關三疊8.jpg" }); 48 VideosList.Add(new ImageDescription { Title = "梅花三弄", uri = "ms-appx:///image/梅花三弄9.jpg" }); 49 VideosList.Add(new ImageDescription { Title = "醉漁唱晚", uri = "ms-appx:///image/醉漁唱晚10.jpg" }); 50 VideoListView.ItemsSource = VideosList; 51 } View Code 1 //圖文描述類 2 public class ImageDescription 3 { 4 public string Title { get; set; } 5 public string uri { get; set; } 6 }已經實現了數據綁定,是不是與你平常使用的方法不太一樣,這樣操作更加簡便,感覺到有木有!
繼續我們的視頻和圖文綁定操作:首先注冊左側ListViewl事件v1_SelectionChanged
View Code 1 /// <summary> 2 /// 主題:點擊listview項,顯示圖文(txt文件)詳細信息 3 /// 描述:通過點擊listview中選中項,讀取文件夾中的.txt文件和圖片詳細信息,并顯示 4 /// 作者:白寧超 5 /// </summary> 6 /// <param name="sender"></param> 7 /// <param name="e"></param> 8 private async void lv1_SelectionChanged(object sender, SelectionChangedEventArgs e) 9 { 10 PlayView(); 11 VideoPlayer.Source = Index_to_Uri_Converter(VideoListView.SelectedIndex);//加載視頻 12 Windows.Storage.StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation; 13 //Description_uri實例化后泛型集合,VideoListView.SelectedIndex,傳遞索引。獲取文本文件路徑 14 Windows.Storage.StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(Description_uri[VideoListView.SelectedIndex])); 15 //返回文本流并讀取 16 string Des = await Windows.Storage.FileIO.ReadTextAsync(file);//@1 17 BitmapImage bt = new BitmapImage(); 18 bt.UriSource = new Uri(VideosList[VideoListView.SelectedIndex].uri); 19 DescriptionImage.Source = bt; 20 Description.Text = Des; 21 22 }lv1事件中調用我們封裝的幾個方面,下面也貼出我們的方法:
View Code 1 //播放視頻 2 public void PlayView() 3 { 4 isplaying = true; 5 isstop = false; 6 Play_Button.Visibility = Visibility.Collapsed; 7 Play_ImageButton.Visibility = Visibility.Collapsed; 8 Stop_Button.Visibility = Visibility.Visible; 9 Stop_ImageButton.Visibility = Visibility.Collapsed; 10 } 11 //暫停播放 12 public void StopView() 13 { 14 isplaying = false; 15 isstop = true; 16 Play_Button.Visibility = Visibility.Visible; 17 Play_ImageButton.Visibility = Visibility.Collapsed; 18 Stop_Button.Visibility = Visibility.Collapsed; 19 Stop_ImageButton.Visibility = Visibility.Visible; 20 } 21 22 //根據選擇索引播放視頻 23 public Uri Index_to_Uri_Converter(int index) 24 { 25 var uri = new Uri(videouri[index]); 26 return uri; 27 }看一下效果吧:
注意:視頻支持wmv,mp4,avi對于常用的rmvb是不支持的
還有什么呢?對了聲音和時間的控制
聲音:?//控制聲音
??????? private void VolumeSlider_ValueChanged_1(object sender, RangeBaseValueChangedEventArgs e)
??????? {
??????????? VideoPlayer.Volume = (VolumeSlider.Value) / 100;
??????? }
時間:
View Code 1 //初始化時間進度條 2 private void VideoPlayer_MediaOpened_1(object sender, RoutedEventArgs e) 3 { 4 TimeSlider.Maximum = VideoPlayer.NaturalDuration.TimeSpan.TotalMilliseconds; 5 ticks.Interval = TimeSpan.FromMilliseconds(1); 6 ticks.Tick += ticks_Tick;//超時事件 7 ticks.Start(); 8 } 9 //加載顯示播放時間 10 void ticks_Tick(object sender, object e) 11 { 12 TimeSlider.Value = VideoPlayer.Position.TotalMilliseconds; 13 TimeText.Text = Milliseconds_to_Minute((long)VideoPlayer.Position.TotalMilliseconds); 14 } 15 16 //轉換秒顯示格式 17 public string Milliseconds_to_Minute(long milliseconds) 18 { 19 int minute = (int)(milliseconds / (1000 * 60)); 20 int seconds = (int)(milliseconds / 1000); 21 return (minute + " : " + seconds); 22 }剩下的工作就是播放和暫停問題了,那就在小圖片(底部)下注冊事件完成,開始暫停按鈕圖片三隱藏的:(主要MediaElement控件的播放Play和暫停Pause屬性)
View Code 1 //播放視頻 2 private void Play_Button_Click_1(object sender, RoutedEventArgs e) 3 { 4 if (isstop == false && isplaying == false) 5 { 6 Initial_Display.Visibility = Visibility.Collapsed; 7 } 8 else if (isstop == true && isplaying == false) 9 { 10 PlayView(); 11 VideoPlayer.Position = new TimeSpan(0, 0, 0, 0, (int)TimeSlider.Value); 12 VideoPlayer.Play(); 13 } 14 } 15 //暫停視頻 16 private void Stop_Button_Click_1(object sender, RoutedEventArgs e) 17 { 18 StopView(); 19 VideoPlayer.Pause(); 20 }看下暫停效果:
最后一步完成打開視頻播放事件,
View Code 1 //選擇播放本地視頻 2 private async void SetLocalMedia() 3 { 4 try 5 { 6 FileOpenPicker openPicker = new FileOpenPicker(); 7 openPicker.ViewMode = PickerViewMode.Thumbnail; 8 openPicker.FileTypeFilter.Add(".wmv"); 9 openPicker.FileTypeFilter.Add(".mp4"); 10 openPicker.FileTypeFilter.Add(".rmvb"); 11 StorageFile file = await openPicker.PickSingleFileAsync(); 12 var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); 13 VideoPlayer.SetSource(stream, file.ContentType); 14 VideoPlayer.Play(); 15 BitmapImage bt = new BitmapImage(); 16 bt.UriSource = new Uri("ms-appx:///Images/error.png"); 17 DescriptionImage.Source = bt; 18 Description.Text = "A Video Playing From Local Directory"; 19 PlayView(); 20 } 21 catch (Exception) 22 { } 23 } 24 private void OpenFileButton_Click_1(object sender, RoutedEventArgs e) 25 { 26 SetLocalMedia(); 27 }以上代碼完成了整個過程,選擇項,播放視頻顯示圖文。可以控制音樂,后臺源代碼也貼出來共享下:
View Code 1 namespace 操作文件 2 { 3 /// <summary> 4 /// 可用于自身或導航至 Frame 內部的空白頁。 5 /// </summary> 6 public sealed partial class MainPage : Page 7 { 8 List<ImageDescription> VideosList = new List<ImageDescription>();//對ImageDescription屬性操作集合 9 private Dictionary<int, string> Description_uri = new Dictionary<int, string>();//文本內容的鍵值集合 10 DispatcherTimer ticks = new DispatcherTimer();//時間間隔處理 11 private Dictionary<int, string> videouri = new Dictionary<int, string>();//視頻內容的鍵值集合 12 private Dictionary<int, string> musicuri = new Dictionary<int, string>();//音頻內容的鍵值集合 13 bool isplaying = false; 14 bool isstop = false; 15 public MainPage() 16 { 17 this.InitializeComponent(); 18 videouri.Add(0, "ms-appx:///music/1.wma"); 19 videouri.Add(1, "ms-appx:///music/2.wma"); 20 videouri.Add(2, "ms-appx:///music/3.wma"); 21 videouri.Add(3, "ms-appx:///music/4.wma"); 22 videouri.Add(4, "ms-appx:///music/5.wma"); 23 videouri.Add(5, "ms-appx:///music/6.wma"); 24 25 musicuri.Add(0, "ms-appx:///Videos/11.avi"); 26 musicuri.Add(1, "ms-appx:///Videos/video2.wmv"); 27 musicuri.Add(2, "ms-appx:///Videos/video3.wmv"); 28 musicuri.Add(3, "ms-appx:///Videos/video4.wmv"); 29 musicuri.Add(4, "ms-appx:///Videos/video5.wmv"); 30 musicuri.Add(5, "ms-appx:///Videos/11.avi"); 31 32 Description_uri.Add(0, "ms-appx:///Description/video1.txt"); 33 Description_uri.Add(1, "ms-appx:///Description/video2.txt"); 34 Description_uri.Add(2, "ms-appx:///Description/video3.txt"); 35 Description_uri.Add(3, "ms-appx:///Description/video4.txt"); 36 Description_uri.Add(4, "ms-appx:///Description/video5.txt"); 37 Description_uri.Add(5, "ms-appx:///Description/video6.txt"); 38 //VideosList.Add(new ImageDescription { Title = "Fishes", uri = "ms-appx:///Images/1.jpg" }); 39 //VideosList.Add(new ImageDescription { Title = "Nature", uri = "ms-appx:///Images/2.jpg" }); 40 //VideosList.Add(new ImageDescription { Title = "Twister", uri = "ms-appx:///Images/3.jpg" }); 41 //VideosList.Add(new ImageDescription { Title = "Flower", uri = "ms-appx:///Images/4.jpg" }); 42 //VideosList.Add(new ImageDescription { Title = "Insect", uri = "ms-appx:///Images/5.jpg" }); 43 //VideosList.Add(new ImageDescription { Title = "Natural Scene", uri = "ms-appx:///Images/6.jpg" }); 44 45 VideosList.Add(new ImageDescription { Title = "瀟湘水云", uri = "ms-appx:///image/瀟湘水云1.jpg" }); 46 VideosList.Add(new ImageDescription { Title = "廣陵散", uri = "ms-appx:///image/廣陵散2.jpg" }); 47 VideosList.Add(new ImageDescription { Title = "高山流水", uri = "ms-appx:///image/高山流水3.jpg" }); 48 VideosList.Add(new ImageDescription { Title = "漁樵問答", uri = "ms-appx:///image/漁樵問答4.jpg" }); 49 VideosList.Add(new ImageDescription { Title = "平沙落雁", uri = "ms-appx:///image/平沙落雁5.jpg" }); 50 VideosList.Add(new ImageDescription { Title = "陽春白雪", uri = "ms-appx:///image/陽春白雪6.jpg" }); 51 VideosList.Add(new ImageDescription { Title = "胡笳十八拍", uri = "ms-appx:///image/胡笳十八拍7.jpg" }); 52 VideosList.Add(new ImageDescription { Title = "陽關三疊", uri = "ms-appx:///image/陽關三疊8.jpg" }); 53 VideosList.Add(new ImageDescription { Title = "梅花三弄", uri = "ms-appx:///image/梅花三弄9.jpg" }); 54 VideosList.Add(new ImageDescription { Title = "醉漁唱晚", uri = "ms-appx:///image/醉漁唱晚10.jpg" }); 55 VideoListView.ItemsSource = VideosList; 56 } 57 58 /// <summary> 59 /// 在此頁將要在 Frame 中顯示時進行調用。 60 /// </summary> 61 /// <param name="e">描述如何訪問此頁的事件數據。Parameter 62 /// 屬性通常用于配置頁。</param> 63 protected override void OnNavigatedTo(NavigationEventArgs e) 64 { 65 } 66 //播放視頻 67 public void PlayView() 68 { 69 isplaying = true; 70 isstop = false; 71 Play_Button.Visibility = Visibility.Collapsed; 72 Play_ImageButton.Visibility = Visibility.Collapsed; 73 Stop_Button.Visibility = Visibility.Visible; 74 Stop_ImageButton.Visibility = Visibility.Collapsed; 75 } 76 //暫停播放 77 public void StopView() 78 { 79 isplaying = false; 80 isstop = true; 81 Play_Button.Visibility = Visibility.Visible; 82 Play_ImageButton.Visibility = Visibility.Collapsed; 83 Stop_Button.Visibility = Visibility.Collapsed; 84 Stop_ImageButton.Visibility = Visibility.Visible; 85 } 86 87 //根據選擇索引播放視頻 88 public Uri Index_to_Uri_Converter(int index) 89 { 90 var uri = new Uri(videouri[index]); 91 return uri; 92 } 93 94 /// <summary> 95 /// 主題:點擊listview項,顯示圖文(txt文件)詳細信息 96 /// 描述:通過點擊listview中選中項,讀取文件夾中的.txt文件和圖片詳細信息,并顯示 97 /// 作者:白寧超 98 /// </summary> 99 /// <param name="sender"></param> 100 /// <param name="e"></param> 101 private async void lv1_SelectionChanged(object sender, SelectionChangedEventArgs e) 102 { 103 PlayView(); 104 VideoPlayer.Source = Index_to_Uri_Converter(VideoListView.SelectedIndex);//加載視頻 105 Windows.Storage.StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation; 106 //Description_uri實例化后泛型集合,VideoListView.SelectedIndex,傳遞索引。獲取文本文件路徑 107 Windows.Storage.StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(Description_uri[VideoListView.SelectedIndex])); 108 //返回文本流并讀取 109 string Des = await Windows.Storage.FileIO.ReadTextAsync(file);//@1 110 BitmapImage bt = new BitmapImage(); 111 bt.UriSource = new Uri(VideosList[VideoListView.SelectedIndex].uri); 112 DescriptionImage.Source = bt; 113 Description.Text = Des; 114 115 } 116 //以上遇到問題是:讀取英文文本可以,讀取其他不行?,@1處報錯 117 118 119 //圖文描述類 120 public class ImageDescription 121 { 122 public string Title { get; set; } 123 public string uri { get; set; } 124 } 125 //初始化時間進度條 126 private void VideoPlayer_MediaOpened_1(object sender, RoutedEventArgs e) 127 { 128 TimeSlider.Maximum = VideoPlayer.NaturalDuration.TimeSpan.TotalMilliseconds; 129 ticks.Interval = TimeSpan.FromMilliseconds(1); 130 ticks.Tick += ticks_Tick;//超時事件 131 ticks.Start(); 132 } 133 //加載顯示播放時間 134 void ticks_Tick(object sender, object e) 135 { 136 TimeSlider.Value = VideoPlayer.Position.TotalMilliseconds; 137 TimeText.Text = Milliseconds_to_Minute((long)VideoPlayer.Position.TotalMilliseconds); 138 } 139 140 //轉換秒顯示格式 141 public string Milliseconds_to_Minute(long milliseconds) 142 { 143 int minute = (int)(milliseconds / (1000 * 60)); 144 int seconds = (int)(milliseconds / 1000); 145 return (minute + " : " + seconds); 146 } 147 //播放視頻 148 private void Play_Button_Click_1(object sender, RoutedEventArgs e) 149 { 150 if (isstop == false && isplaying == false) 151 { 152 Initial_Display.Visibility = Visibility.Collapsed; 153 } 154 else if (isstop == true && isplaying == false) 155 { 156 PlayView(); 157 VideoPlayer.Position = new TimeSpan(0, 0, 0, 0, (int)TimeSlider.Value); 158 VideoPlayer.Play(); 159 } 160 } 161 //暫停視頻 162 private void Stop_Button_Click_1(object sender, RoutedEventArgs e) 163 { 164 StopView(); 165 VideoPlayer.Pause(); 166 } 167 //控制聲音 168 private void VolumeSlider_ValueChanged_1(object sender, RangeBaseValueChangedEventArgs e) 169 { 170 VideoPlayer.Volume = (VolumeSlider.Value) / 100; 171 } 172 //選擇播放本地視頻 173 private async void SetLocalMedia() 174 { 175 try 176 { 177 FileOpenPicker openPicker = new FileOpenPicker(); 178 openPicker.ViewMode = PickerViewMode.Thumbnail; 179 openPicker.FileTypeFilter.Add(".wmv"); 180 openPicker.FileTypeFilter.Add(".mp4"); 181 openPicker.FileTypeFilter.Add(".rmvb"); 182 StorageFile file = await openPicker.PickSingleFileAsync(); 183 var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); 184 VideoPlayer.SetSource(stream, file.ContentType); 185 VideoPlayer.Play(); 186 BitmapImage bt = new BitmapImage(); 187 bt.UriSource = new Uri("ms-appx:///Images/error.png"); 188 DescriptionImage.Source = bt; 189 Description.Text = "A Video Playing From Local Directory"; 190 PlayView(); 191 } 192 catch (Exception) 193 { } 194 } 195 private void OpenFileButton_Click_1(object sender, RoutedEventArgs e) 196 { 197 SetLocalMedia(); 198 } 199 //根據選擇索引播放音頻 200 public Uri IndexUriConverter(int index) 201 { 202 var uri = new Uri(musicuri[index]); 203 return uri; 204 } 205 206 207 //音頻播放 208 private void Playbt_Click_1(object sender, RoutedEventArgs e) 209 { 210 Playbt.Visibility = Visibility.Visible; 211 Stopbt.Visibility = Visibility.Collapsed; 212 VideoPlayer.Source = IndexUriConverter(VideoListView.SelectedIndex);//加載音頻 213 Music.Play(); 214 VideoPlayer.Pause(); 215 } 216 //音頻暫停 217 private void Stopbt_Click_1(object sender, RoutedEventArgs e) 218 { 219 220 Playbt.Visibility = Visibility.Collapsed; 221 Stopbt.Visibility = Visibility.Visible; 222 Music.Pause(); 223 VideoPlayer.Pause(); 224 } 225 226 227 } 228 }到這里全部結束了,你已經知道文件操作和音視頻簡單控制了吧,具體深入還要自己研究啦。
聲明:1本人也是剛學,研究文檔資料后才整理出來的,技術有限,不正確的望指正
??????? 2想與喜歡win8的初學者,分享下學習知識
??????? 3本程序經過調試均可正確運行。
轉載于:https://www.cnblogs.com/kezhong/archive/2013/01/08/win8.html
總結
以上是生活随笔為你收集整理的win8文件操作以及音视频的应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【前端系列教程之CSS3】04_CSS定
- 下一篇: 怎样找自己研究领域的论文