forked from MicrosoftEdge/WebView2Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioComponent.cpp
113 lines (100 loc) · 3.58 KB
/
AudioComponent.cpp
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
// Copyright (C) Microsoft Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "stdafx.h"
#include "AudioComponent.h"
#include "CheckFailure.h"
using namespace Microsoft::WRL;
//! [IsDocumentPlayingAudioChanged] [IsDocumentPlayingAudio] [IsMutedChanged] [ToggleIsMuted]
AudioComponent::AudioComponent(AppWindow* appWindow)
: m_appWindow(appWindow), m_webView(appWindow->GetWebView())
{
auto webview2_8 = m_webView.try_query<ICoreWebView2_8>();
if (webview2_8)
{
// Register a handler for the IsDocumentPlayingAudioChanged event.
CHECK_FAILURE(webview2_8->add_IsDocumentPlayingAudioChanged(
Callback<ICoreWebView2IsDocumentPlayingAudioChangedEventHandler>(
[this, webview2_8](ICoreWebView2* sender, IUnknown* args) -> HRESULT
{
UpdateTitleWithMuteState(webview2_8);
return S_OK;
})
.Get(),
&m_isDocumentPlayingAudioChangedToken));
// Register a handler for the IsMutedChanged event.
CHECK_FAILURE(webview2_8->add_IsMutedChanged(
Callback<ICoreWebView2IsMutedChangedEventHandler>(
[this, webview2_8](ICoreWebView2* sender, IUnknown* args) -> HRESULT
{
UpdateTitleWithMuteState(webview2_8);
return S_OK;
})
.Get(),
&m_isMutedChangedToken));
}
}
bool AudioComponent::HandleWindowMessage(
HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT* result)
{
if (message == WM_COMMAND)
{
switch (LOWORD(wParam))
{
case IDM_TOGGLE_MUTE_STATE:
ToggleMuteState();
return true;
}
}
return false;
}
// Toggle the mute state of the current window and show a mute or unmute icon on the title bar
void AudioComponent::ToggleMuteState()
{
auto webview2_8 = m_webView.try_query<ICoreWebView2_8>();
if (webview2_8)
{
BOOL isMuted;
CHECK_FAILURE(webview2_8->get_IsMuted(&isMuted));
CHECK_FAILURE(webview2_8->put_IsMuted(!isMuted));
std::wstring result = !isMuted ? L"WebView is Now Muted" : L"WebView is Now Unmuted";
MessageBox(nullptr, result.c_str(), L"Mute State Changed", MB_OK);
}
}
void AudioComponent::UpdateTitleWithMuteState(wil::com_ptr<ICoreWebView2_8> webview2_8)
{
BOOL isDocumentPlayingAudio;
CHECK_FAILURE(webview2_8->get_IsDocumentPlayingAudio(&isDocumentPlayingAudio));
BOOL isMuted;
CHECK_FAILURE(webview2_8->get_IsMuted(&isMuted));
wil::unique_cotaskmem_string title;
CHECK_FAILURE(m_webView->get_DocumentTitle(&title));
std::wstring result = L"";
if (isDocumentPlayingAudio)
{
if (isMuted)
{
result = L"🔇 " + std::wstring(title.get());
}
else
{
result = L"🔊 " + std::wstring(title.get());
}
}
else
{
result = std::wstring(title.get());
}
m_appWindow->SetDocumentTitle(result.c_str());
}
//! [IsDocumentPlayingAudioChanged] [IsDocumentPlayingAudio] [IsMutedChanged] [ToggleIsMuted]
AudioComponent::~AudioComponent()
{
auto webview2_8 = m_webView.try_query<ICoreWebView2_8>();
if (webview2_8)
{
CHECK_FAILURE(webview2_8->remove_IsDocumentPlayingAudioChanged(
m_isDocumentPlayingAudioChangedToken));
CHECK_FAILURE(webview2_8->remove_IsMutedChanged(m_isMutedChangedToken));
}
}