forked from ameer1234567890/mb_MediaControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMediaControl.cs
231 lines (214 loc) · 11 KB
/
MediaControl.cs
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using Windows.Media;
using Windows.Media.Core;
using Windows.Media.Playback;
using Windows.Storage;
using Windows.Storage.Streams;
using System.Runtime.InteropServices.WindowsRuntime;
using static System.IO.WindowsRuntimeStreamExtensions;
namespace MusicBeePlugin
{
public partial class Plugin
{
private MusicBeeApiInterface mbApiInterface;
private readonly PluginInfo about = new PluginInfo();
private SystemMediaTransportControls systemMediaControls;
private SystemMediaTransportControlsDisplayUpdater displayUpdater;
private MusicDisplayProperties musicProperties;
private InMemoryRandomAccessStream artworkStream;
public PluginInfo Initialise(IntPtr apiInterfacePtr)
{
mbApiInterface = new MusicBeeApiInterface();
mbApiInterface.Initialise(apiInterfacePtr);
about.PluginInfoVersion = PluginInfoVersion;
about.Name = "Media Control";
about.Description = "Enables MusicBee to interact with the Windows 10 Media Control overlay.";
about.Author = "Steven Mayall";
about.TargetApplication = ""; // the name of a Plugin Storage device or panel header for a dockable panel
about.Type = PluginType.General;
about.VersionMajor = 1; // your plugin version
about.VersionMinor = 0;
about.Revision = 1;
about.MinInterfaceVersion = MinInterfaceVersion;
about.MinApiRevision = MinApiRevision;
about.ReceiveNotifications = ReceiveNotificationFlags.PlayerEvents;
about.ConfigurationPanelHeight = 0; // height in pixels that musicbee should reserve in a panel for config settings. When set, a handle to an empty panel will be passed to the Configure function
return about;
}
public bool Configure(IntPtr panelHandle)
{
return false;
}
// called by MusicBee when the user clicks Apply or Save in the MusicBee Preferences screen.
// its up to you to figure out whether anything has changed and needs updating
public void SaveSettings()
{
}
// MusicBee is closing the plugin (plugin is being disabled by user or MusicBee is shutting down)
public void Close(PluginCloseReason reason)
{
SetArtworkThumbnail(null);
}
// uninstall this plugin - clean up any persisted files
public void Uninstall()
{
}
// receive event notifications from MusicBee
// you need to set about.ReceiveNotificationFlags = PlayerEvents to receive all notifications, and not just the startup event
public void ReceiveNotification(string sourceFileUrl, NotificationType type)
{
switch (type)
{
case NotificationType.PluginStartup:
systemMediaControls = BackgroundMediaPlayer.Current.SystemMediaTransportControls;
systemMediaControls.PlaybackStatus = MediaPlaybackStatus.Closed;
systemMediaControls.IsEnabled = false;
systemMediaControls.IsPlayEnabled = true;
systemMediaControls.IsPauseEnabled = true;
systemMediaControls.IsStopEnabled = true;
systemMediaControls.IsPreviousEnabled = true;
systemMediaControls.IsNextEnabled = true;
systemMediaControls.IsRewindEnabled = false;
systemMediaControls.IsFastForwardEnabled = false;
systemMediaControls.ButtonPressed += systemMediaControls_ButtonPressed;
systemMediaControls.PlaybackPositionChangeRequested += systemMediaControls_PlaybackPositionChangeRequested;
systemMediaControls.PlaybackRateChangeRequested += systemMediaControls_PlaybackRateChangeRequested;
systemMediaControls.ShuffleEnabledChangeRequested += systemMediaControls_ShuffleEnabledChangeRequested;
systemMediaControls.AutoRepeatModeChangeRequested += systemMediaControls_AutoRepeatModeChangeRequested;
displayUpdater = systemMediaControls.DisplayUpdater;
displayUpdater.Type = MediaPlaybackType.Music;
musicProperties = displayUpdater.MusicProperties;
SetDisplayValues();
break;
case NotificationType.PlayStateChanged:
SetPlayerState();
break;
case NotificationType.TrackChanged:
SetDisplayValues();
break;
}
}
private void systemMediaControls_ButtonPressed(SystemMediaTransportControls smtc, SystemMediaTransportControlsButtonPressedEventArgs args)
{
switch (args.Button)
{
case SystemMediaTransportControlsButton.Stop:
mbApiInterface.Player_Stop();
break;
case SystemMediaTransportControlsButton.Play:
if (mbApiInterface.Player_GetPlayState() != PlayState.Playing)
mbApiInterface.Player_PlayPause();
break;
case SystemMediaTransportControlsButton.Pause:
if (mbApiInterface.Player_GetPlayState() != PlayState.Paused)
mbApiInterface.Player_PlayPause();
break;
case SystemMediaTransportControlsButton.Next:
mbApiInterface.Player_PlayNextTrack();
break;
case SystemMediaTransportControlsButton.Previous:
mbApiInterface.Player_PlayPreviousTrack();
break;
case SystemMediaTransportControlsButton.Rewind:
break;
case SystemMediaTransportControlsButton.FastForward:
break;
case SystemMediaTransportControlsButton.ChannelUp:
mbApiInterface.Player_SetVolume(mbApiInterface.Player_GetVolume() + 0.05F);
break;
case SystemMediaTransportControlsButton.ChannelDown:
mbApiInterface.Player_SetVolume(mbApiInterface.Player_GetVolume() - 0.05F);
break;
}
}
private void systemMediaControls_PlaybackPositionChangeRequested(SystemMediaTransportControls smtc, PlaybackPositionChangeRequestedEventArgs args)
{
mbApiInterface.Player_SetPosition(args.RequestedPlaybackPosition.Milliseconds);
}
private void systemMediaControls_PlaybackRateChangeRequested(SystemMediaTransportControls smtc, PlaybackRateChangeRequestedEventArgs args)
{
}
private void systemMediaControls_AutoRepeatModeChangeRequested(SystemMediaTransportControls smtc, AutoRepeatModeChangeRequestedEventArgs args)
{
switch (args.RequestedAutoRepeatMode)
{
case MediaPlaybackAutoRepeatMode.Track:
mbApiInterface.Player_SetRepeat(RepeatMode.One);
break;
case MediaPlaybackAutoRepeatMode.List:
mbApiInterface.Player_SetRepeat(RepeatMode.All);
break;
case MediaPlaybackAutoRepeatMode.None:
mbApiInterface.Player_SetRepeat(RepeatMode.None);
break;
}
}
private void systemMediaControls_ShuffleEnabledChangeRequested(SystemMediaTransportControls smtc, ShuffleEnabledChangeRequestedEventArgs args)
{
mbApiInterface.Player_SetShuffle(args.RequestedShuffleEnabled);
}
private void SetDisplayValues()
{
displayUpdater.ClearAll();
displayUpdater.Type = MediaPlaybackType.Music;
SetArtworkThumbnail(null);
string url = mbApiInterface.NowPlaying_GetFileUrl();
if (url != null)
{
musicProperties.AlbumArtist = mbApiInterface.NowPlaying_GetFileTag(MetaDataType.AlbumArtist);
musicProperties.AlbumTitle = mbApiInterface.NowPlaying_GetFileTag(MetaDataType.Album);
uint value;
if (UInt32.TryParse(mbApiInterface.NowPlaying_GetFileTag(MetaDataType.TrackCount), out value))
musicProperties.AlbumTrackCount = value;
musicProperties.Artist = mbApiInterface.NowPlaying_GetFileTag(MetaDataType.Artist);
musicProperties.Title = mbApiInterface.NowPlaying_GetFileTag(MetaDataType.TrackTitle);
if (string.IsNullOrEmpty(musicProperties.Title))
musicProperties.Title = url.Substring(url.LastIndexOfAny(new char[] { '/', '\\' }) + 1);
if (UInt32.TryParse(mbApiInterface.NowPlaying_GetFileTag(MetaDataType.TrackNo), out value))
musicProperties.TrackNumber = value;
//musicProperties.Genres = mbApiInterface.NowPlaying_GetFileTag(MetaDataType.Genres).Split(new string[] {"; "}, StringSplitOptions.RemoveEmptyEntries);
PictureLocations pictureLocations;
string pictureUrl;
byte[] imageData;
mbApiInterface.Library_GetArtworkEx(url, 0, true, out pictureLocations, out pictureUrl, out imageData);
SetArtworkThumbnail(imageData);
}
displayUpdater.Update();
}
private void SetPlayerState()
{
switch (mbApiInterface.Player_GetPlayState())
{
case PlayState.Playing:
systemMediaControls.PlaybackStatus = MediaPlaybackStatus.Playing;
systemMediaControls.IsEnabled = true;
break;
case PlayState.Paused:
systemMediaControls.PlaybackStatus = MediaPlaybackStatus.Paused;
break;
case PlayState.Stopped:
systemMediaControls.PlaybackStatus = MediaPlaybackStatus.Stopped;
systemMediaControls.IsEnabled = false;
break;
}
}
private async void SetArtworkThumbnail(byte[] data)
{
if (artworkStream != null)
artworkStream.Dispose();
if (data == null)
{
artworkStream = null;
displayUpdater.Thumbnail = null;
}
else
{
artworkStream = new InMemoryRandomAccessStream();
await artworkStream.WriteAsync(data.AsBuffer());
displayUpdater.Thumbnail = RandomAccessStreamReference.CreateFromStream(artworkStream);
}
}
}
}