@@ -92,50 +92,55 @@ std::shared_ptr<MediaInfo> backend::getMediaInformation() {
9292 return nullptr ;
9393
9494 auto playbackInfo = currentSession.GetPlaybackInfo ();
95- auto mediaProperties = currentSession.TryGetMediaPropertiesAsync ().get ();
96- auto timelineInformation = currentSession.GetTimelineProperties ();
97- if (!mediaProperties)
98- return nullptr ;
95+ try {
96+ auto mediaProperties = currentSession.TryGetMediaPropertiesAsync ().get ();
97+ auto timelineInformation = currentSession.GetTimelineProperties ();
98+ if (!mediaProperties)
99+ return nullptr ;
99100
100- auto endTime = std::chrono::duration_cast<std::chrono::milliseconds>(timelineInformation.EndTime ()).count ();
101- auto elapsedTime = std::chrono::duration_cast<std::chrono::milliseconds>(timelineInformation.Position ()).count ();
101+ auto endTime = std::chrono::duration_cast<std::chrono::milliseconds>(timelineInformation.EndTime ()).count ();
102+ auto elapsedTime =
103+ std::chrono::duration_cast<std::chrono::milliseconds>(timelineInformation.Position ()).count ();
102104
103- auto thumbnail = mediaProperties.Thumbnail ();
104- std::string thumbnailData = " " ;
105+ auto thumbnail = mediaProperties.Thumbnail ();
106+ std::string thumbnailData = " " ;
105107
106- if (thumbnail) {
107- auto stream = thumbnail.OpenReadAsync ().get ();
108- size_t size = static_cast <size_t >(stream.Size ());
108+ if (thumbnail) {
109+ auto stream = thumbnail.OpenReadAsync ().get ();
110+ size_t size = static_cast <size_t >(stream.Size ());
109111
110- DataReader reader (stream);
111- reader.LoadAsync (static_cast <uint32_t >(size)).get ();
112+ DataReader reader (stream);
113+ reader.LoadAsync (static_cast <uint32_t >(size)).get ();
112114
113- std::vector<uint8_t > buffer (size);
114- reader.ReadBytes (buffer);
115- reader.Close ();
115+ std::vector<uint8_t > buffer (size);
116+ reader.ReadBytes (buffer);
117+ reader.Close ();
116118
117- thumbnailData = std::string (buffer.begin (), buffer.end ());
118- stream.Close ();
119- }
119+ thumbnailData = std::string (buffer.begin (), buffer.end ());
120+ stream.Close ();
121+ }
120122
121- std::string artist = toStdString (mediaProperties.Artist ());
122- std::string albumName = toStdString (mediaProperties.AlbumTitle ());
123- if (artist == " " )
124- artist = toStdString (mediaProperties.AlbumArtist ()); // Needed for some apps
123+ std::string artist = toStdString (mediaProperties.Artist ());
124+ std::string albumName = toStdString (mediaProperties.AlbumTitle ());
125+ if (artist == " " )
126+ artist = toStdString (mediaProperties.AlbumArtist ()); // Needed for some apps
125127
126- if (artist.find (EM_DASH) != std::string::npos) {
127- albumName = artist.substr (artist.find (EM_DASH) + 3 );
128- artist = artist.substr (0 , artist.find (EM_DASH));
129- utils::trim (artist);
130- utils::trim (albumName);
131- }
128+ if (artist.find (EM_DASH) != std::string::npos) {
129+ albumName = artist.substr (artist.find (EM_DASH) + 3 );
130+ artist = artist.substr (0 , artist.find (EM_DASH));
131+ utils::trim (artist);
132+ utils::trim (albumName);
133+ }
132134
133- std::string modelId = toStdString (currentSession.SourceAppUserModelId ());
135+ std::string modelId = toStdString (currentSession.SourceAppUserModelId ());
134136
135- return std::make_shared<MediaInfo>(
136- playbackInfo.PlaybackStatus () == GlobalSystemMediaTransportControlsSessionPlaybackStatus::Paused,
137- toStdString (mediaProperties.Title ()), std::move (artist), std::move (albumName), std::move (modelId),
138- std::move (thumbnailData), endTime, elapsedTime);
137+ return std::make_shared<MediaInfo>(
138+ playbackInfo.PlaybackStatus () == GlobalSystemMediaTransportControlsSessionPlaybackStatus::Paused,
139+ toStdString (mediaProperties.Title ()), std::move (artist), std::move (albumName), std::move (modelId),
140+ std::move (thumbnailData), endTime, elapsedTime);
141+ } catch (...) {
142+ return nullptr ;
143+ }
139144}
140145
141146bool backend::init () {
0 commit comments