MediaElement并不总是在WPF应用程序中播放视频

时间:2016-03-29 10:40:05

标签: c# wpf mediaelement

我们创建了一个测试应用程序,用户使用手势与其进行交互。每个用户以顺序方式与所有手势交互。当用户首先被引入新手势时,为用户播放小的教学电影,向他显示如何执行该手势。电影首先应该在主屏幕上播放,然后播放一次后应该在第二个监视器上循环,直到下一个手势开始,然后应该关闭辅助视频屏幕并开始整个过程​​。这是我们遇到问题的地方。

这些电影都很好用,实际上大部分时间都能正常播放。问题是,有时,电影实际上并不是在辅助监视器中启动。他们加载很好但从未真正开始。这不是我们可以可靠地再现的东西,这有时是随机发生的。

这是我们目前为VideoPlayer窗口提供的代码:

public partial class VideoWindow : Window {

    static VideoWindow currentVideoWindow;

    public VideoWindow(GestureDirection direction, GestureType type, bool reopen = false)
    {
        if(currentVideoWindow != null) {
            currentVideoWindow.CloseWindow();
            currentVideoWindow = this;
        }
        else {
            currentVideoWindow = this;
        }
        GestureParser.Pause(!reopen);
        InitializeComponent();
        this.Title = type + " " + direction;
        this.Show();

        videoMediaElement.LoadedBehavior = MediaState.Manual;
        videoMediaElement.UnloadedBehavior = MediaState.Manual;
        string videoDirectory = @"techniques/";
        string video = direction.ToString() + "_" + type.ToString() + ".mp4";


        if (Screen.AllScreens.Length > 1) {
            int secScreen = Screen.AllScreens.Length == 2 ? 0 : 2;
            int mainScreen = Screen.AllScreens.Length == 2 ? 1 : 0;
            Screen s = reopen ? Screen.AllScreens[secScreen] : Screen.AllScreens[mainScreen];
            System.Drawing.Rectangle r = s.WorkingArea;
            this.Top = r.Top;
            this.Left = r.Left;
            this.Topmost = true;
            this.Show();
            this.WindowStyle = WindowStyle.None;
            this.WindowState = WindowState.Maximized;
        }
        else {
            Screen s = Screen.AllScreens[0];
            System.Drawing.Rectangle r = s.WorkingArea;
            this.Top = r.Top;
            this.Left = r.Left;
            this.Show();
        }
        String videoPath = CreateAbsolutePathTo(videoDirectory + video);
        if (File.Exists(videoPath)) {
            Uri videoUri = new Uri(videoPath, UriKind.Relative);
            videoMediaElement.Source = videoUri;
        }


        if (reopen) {
            this.Activate();
            canvasWindow.Activate();
            videoMediaElement.MediaEnded += (sender, args) => {
                videoMediaElement.Stop();
                videoMediaElement.Position = TimeSpan.Zero;
            };
        } else {
            videoMediaElement.MediaEnded += (sender, args) => {
                this.CloseWindow();
                var t = new VideoWindow(direction, type, true);
            };
        }
        videoMediaElement.Play();

        Task task = Task.Factory.StartNew(() =>
        {
            while(GetMediaState(videoMediaElement) != MediaState.Play)
            {
                videoMediaElement.Play();
            }
        });
    }

    private MediaState GetMediaState(MediaElement myMedia)
    {
        FieldInfo hlp = typeof(MediaElement).GetField("_helper", BindingFlags.NonPublic | BindingFlags.Instance);
        object helperObject = hlp.GetValue(myMedia);
        FieldInfo stateField = helperObject.GetType().GetField("_currentState", BindingFlags.NonPublic | BindingFlags.Instance);
        MediaState state = (MediaState)stateField.GetValue(helperObject);
        return state;
    }

    static CanvasWindow canvasWindow;
    public static void SetCanvasWindow(CanvasWindow window) {
        canvasWindow = window;
    }

    private static string CreateAbsolutePathTo(string mediaFile) {
        return Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName, mediaFile);
    }

    private void CloseWindow() {
        videoMediaElement.Stop();
        videoMediaElement.Close();
        this.Close();
    }

}

正如您所看到的,我们尝试创建一个不断检查以查看视频是否正在播放的任务,如果不是,则应该播放该视频。问题是,媒体状态总是在播放,即使电影停止播放,因此它什么都不做。

我们真的不知道出了什么问题,真的很感激一些帮助。

0 个答案:

没有答案
相关问题