現在、MediaElementとstoryBoradを用いて
動画を再生するWPFアプリケーションを作成しております。
流れとしては、[開始(再生)] → [一時停止] → [削除]です。
具体的には以下のように実装しております
[再生] _storyboard.Begin();
[停止] _storyboard.Pause();
[動画ファイル削除] _storyboard.Stop();の後に、File.Delete("動画ファイル名")
しかし現在、[削除]の際に行っているFile.Delete("動画ファイル名")
のタイミングでファイルが消せなくて困っております。
###発生している問題
File.Delete("動画ファイル名")した時点では 動画ファイルが消えてくれません。 またその際に、例外も発生しません。 (しかし、その後アプリケーションを終了するとファイルが消えます)
###該当のソースコード
xaml側 (MainWindow.xaml)
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <!-- ストーリーボード --> <Storyboard x:Key="MainStoryBoard" /> </Window.Resources> <Grid> <MediaElement x:Name="myMediaElement" Stretch="Uniform" LoadedBehavior="Manual" ScrubbingEnabled="True" Volume="{Binding DataContext.Volume, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"/> <Button x:Name="button" Content="削除" HorizontalAlignment="Left" Margin="425,275,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/> <Button x:Name="button1" Content="一時停止" HorizontalAlignment="Left" Margin="323,275,0,0" VerticalAlignment="Top" Width="75" Click="button1_Click"/> <Button x:Name="button2" Content="開始" HorizontalAlignment="Left" Margin="229,275,0,0" VerticalAlignment="Top" Width="75" Click="button2_Click"/> </Grid> </Window>
xaml.cs側 (MainWindow.xaml.cs)
using System; using System.IO; using System.Reflection; using System.Windows; using System.Windows.Media; using System.Windows.Media.Animation; namespace WpfApplication1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { /// <summary> /// ストーリーボード /// </summary> private Storyboard _storyboard; string _videoPath; public MainWindow() { InitializeComponent(); // exe直下のmp4ファイルを読みます(ファイル名:origin.mp4) string moviePath = Directory.GetParent(Assembly.GetExecutingAssembly().Location).ToString(); _videoPath = System.IO.Path.Combine(moviePath, "origin.mp4"); // 動画ファイル存在確認 if (!File.Exists(_videoPath)) { MessageBox.Show("動画ファイルが存在しません"); } // ストーリーボード検索 _storyboard = (Storyboard)this.FindResource("MainStoryBoard"); //メディアタイムラインを作成 MediaTimeline mediaTimeline = new MediaTimeline(); mediaTimeline.Source = new Uri(_videoPath, UriKind.Absolute); // タイムライン設定イベント発行 TimeLineEventArgs args = new TimeLineEventArgs(); args.TimeLineValue = mediaTimeline; SetStoryBoardTimeLine(this, args); } /// <summary> /// 削除押下 /// </summary> private void button_Click(object sender, RoutedEventArgs e) { // 停止 _storyboard.Stop(); // タイムラインクリア _storyboard.Children.Clear(); // 動画ファイル存在確認 if (File.Exists(_videoPath)) { // <動画ファイル削除> File.Delete(_videoPath); } // 一応1秒待ちます System.Threading.Thread.Sleep(1000); // 結果 if (File.Exists(_videoPath)) { MessageBox.Show("消せてません!!"); } else { MessageBox.Show("消せました"); } } /// <summary> /// ストーリーボードにタイムラインセットイベント /// </summary> private void SetStoryBoardTimeLine(object sender, TimeLineEventArgs e) { if (null != _storyboard) { Timeline timeline = e.TimeLineValue; Storyboard.SetTarget(timeline, myMediaElement); // ストーリーボードへタイムライン追加 _storyboard.Children.Add(timeline); } } // 一時停止 private void button1_Click(object sender, RoutedEventArgs e) { _storyboard.Pause(); } // 開始 private void button2_Click(object sender, RoutedEventArgs e) { // ストーリーボード開始 _storyboard.Begin(); } } public class TimeLineEventArgs : EventArgs { public int TypeValue { set; get; } public Timeline TimeLineValue { set; get; } public TimeSpan DurationValue { set; get; } } }
###試したこと
誰かが動画を持ち続けている状態と思いましたので、
myMediaElementをclose()したり、storyBoardにnullを代入などの
初期化の処理を試みました。しかし解決には至りませんでした。
解決法、或いは解決のためのアプローチの仕方など、ご教授頂けると幸いです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/05 05:51