forked from clarkezone/cppwinrt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.cpp
More file actions
121 lines (99 loc) · 3.31 KB
/
App.cpp
File metadata and controls
121 lines (99 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "pch.h"
using namespace std::chrono;
using namespace winrt;
using namespace Windows::ApplicationModel::Core;
using namespace Windows::Foundation;
using namespace Windows::UI::Core;
using namespace Windows::UI::Composition;
using namespace Windows::Media::Core;
using namespace Windows::Media::Playback;
using namespace Windows::System;
using namespace Windows::Storage;
using namespace Windows::Storage::Pickers;
struct App : implements<App, IFrameworkViewSource, IFrameworkView>
{
IFrameworkView CreateView()
{
return *this;
}
void Initialize(CoreApplicationView const &)
{
}
void Load(hstring const&)
{
}
void Uninitialize()
{
}
void Run()
{
CoreWindow window = CoreWindow::GetForCurrentThread();
window.Activate();
CoreDispatcher dispatcher = window.Dispatcher();
dispatcher.ProcessEvents(CoreProcessEventsOption::ProcessUntilQuit);
}
void SetWindow(CoreWindow const & window)
{
m_activated = window.Activated(auto_revoke, { this, &App::OnActivated });
}
fire_and_forget OnActivated(CoreWindow window, WindowActivatedEventArgs)
{
m_activated.revoke();
Compositor compositor;
SpriteVisual visual = compositor.CreateSpriteVisual();
Rect bounds = window.Bounds();
visual.Size({ bounds.Width, bounds.Height });
m_target = compositor.CreateTargetForCurrentView();
m_target.Root(visual);
FileOpenPicker picker;
picker.SuggestedStartLocation(PickerLocationId::VideosLibrary);
picker.FileTypeFilter().Append(L".mp4");
StorageFile file = co_await picker.PickSingleFileAsync();
if (!file)
{
CoreApplication::Exit();
return;
}
MediaSource source = MediaSource::CreateFromStorageFile(file);
MediaPlayer player;
player.Source(MediaPlaybackItem(source));
MediaPlayerSurface surface = player.GetSurface(compositor);
visual.Brush(compositor.CreateSurfaceBrush(surface.CompositionSurface()));
player.Play();
window.PointerPressed([=](auto && ...)
{
static bool playing = true;
playing = !playing;
playing ? player.Play() : player.Pause();
});
window.SizeChanged([=](auto &&, WindowSizeChangedEventArgs const & args)
{
visual.Size(args.Size());
});
window.KeyDown([=](auto&&, KeyEventArgs const& args)
{
VirtualKey key = args.VirtualKey();
MediaPlaybackSession session = player.PlaybackSession();
TimeSpan position = session.Position();
if (key == VirtualKey::Left)
{
position -= 10s;
}
else if (key == VirtualKey::Right)
{
position += 10s;
}
else if (key == VirtualKey::Up)
{
position = 0s;
}
session.Position(position);
});
}
CoreWindow::Activated_revoker m_activated;
CompositionTarget m_target{ nullptr };
};
int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int)
{
CoreApplication::Run(App());
}