[macOS] show what's currently playing in MPNowPlayingInfoCenter

This commit is contained in:
Andrey Filipenkov 2024-03-16 16:04:29 +03:00
parent 25affb5b9c
commit 1328d0a548
2 changed files with 41 additions and 2 deletions

View File

@ -16,11 +16,14 @@ public:
bool initInput() override;
const char* inputName() override { return "AppleMediaKeys"; }
private:
void* m_delegate;
private slots:
void handleStateChanged(PlayerComponent::State newState, PlayerComponent::State oldState);
void handlePositionUpdate(quint64 position);
void handleUpdateDuration(qint64 duration);
void handleUpdateMetaData(const QVariantMap& meta);
private:
void* m_delegate;
typedef void (*SetNowPlayingVisibilityFunc)(void* origin, int visibility);
typedef void* (*GetLocalOriginFunc)(void);

View File

@ -95,6 +95,8 @@ bool InputAppleMediaKeys::initInput()
&InputAppleMediaKeys::handlePositionUpdate);
connect(&PlayerComponent::Get(), &PlayerComponent::updateDuration, this,
&InputAppleMediaKeys::handleUpdateDuration);
connect(&PlayerComponent::Get(), &PlayerComponent::onMetaData, this,
&InputAppleMediaKeys::handleUpdateMetaData);
if (auto lib =
dlopen("/System/Library/PrivateFrameworks/MediaRemote.framework/MediaRemote", RTLD_NOW))
@ -171,3 +173,37 @@ void InputAppleMediaKeys::handleUpdateDuration(qint64 duration)
center.nowPlayingInfo = playingInfo;
m_pendingUpdate = true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void InputAppleMediaKeys::handleUpdateMetaData(const QVariantMap& meta)
{
auto info = [NSMutableDictionary
dictionaryWithDictionary:MPNowPlayingInfoCenter.defaultCenter.nowPlayingInfo];
info[MPMediaItemPropertyTitle] = meta["Name"].toString().toNSString();
if (meta["MediaType"] == QLatin1String{ "Video" })
{
info[MPNowPlayingInfoPropertyMediaType] = @(MPNowPlayingInfoMediaTypeVideo);
if (meta["Type"] == QLatin1String{ "Episode" })
{
[info addEntriesFromDictionary:@{
MPMediaItemPropertyArtist : meta["SeriesName"].toString().toNSString(),
MPMediaItemPropertyMediaType : @(MPMediaTypeTVShow),
}];
}
else
info[MPMediaItemPropertyMediaType] = @(MPMediaTypeMovie);
}
else // audio most probably
{
[info addEntriesFromDictionary:@{
MPMediaItemPropertyAlbumArtist : meta["AlbumArtist"].toString().toNSString(),
MPMediaItemPropertyAlbumTitle : meta["Album"].toString().toNSString(),
MPMediaItemPropertyArtist : meta["Artists"].toStringList().join(", ").toNSString(),
MPMediaItemPropertyMediaType : @(MPMediaTypeMusic),
MPNowPlayingInfoPropertyMediaType : @(MPNowPlayingInfoMediaTypeAudio),
}];
}
MPNowPlayingInfoCenter.defaultCenter.nowPlayingInfo = info;
}