@@ -44,7 +44,7 @@ void MediaController::handleEarDetection(EarDetection *earDetection)
4444
4545 if (shouldPause && isActiveOutputDeviceAirPods ())
4646 {
47- if (m_mediaState == Playing)
47+ if (getCurrentMediaState () == Playing)
4848 {
4949 pause ();
5050 }
@@ -172,7 +172,7 @@ void MediaController::setConnectedDeviceMacAddress(const QString &macAddress) {
172172}
173173
174174MediaController::MediaState MediaController::mediaStateFromPlayerctlOutput (
175- const QString &output) {
175+ const QString &output) const {
176176 if (output == " Playing" ) {
177177 return MediaState::Playing;
178178 } else if (output == " Paused" ) {
@@ -182,71 +182,77 @@ MediaController::MediaState MediaController::mediaStateFromPlayerctlOutput(
182182 }
183183}
184184
185- QDBusInterface * MediaController::getMediaPlayerInterface ()
185+ MediaController::MediaState MediaController::getCurrentMediaState () const
186186{
187- // List all media player services
188- QDBusConnection sessionBus = QDBusConnection::sessionBus ();
189- QDBusInterface dbusInterface (" org.freedesktop.DBus" , " /org/freedesktop/DBus" ,
190- " org.freedesktop.DBus" , sessionBus);
191- QDBusReply<QStringList> reply = dbusInterface.call (" ListNames" );
192-
193- if (!reply.isValid ())
194- {
195- LOG_ERROR (" Failed to list DBus services: " << reply.error ().message ());
196- return nullptr ;
197- }
187+ return mediaStateFromPlayerctlOutput (PlayerStatusWatcher::getCurrentPlaybackStatus (" " ));
188+ }
198189
199- QStringList services = reply.value ();
200- QString mediaPlayerService;
190+ bool MediaController::sendMediaPlayerCommand (const QString &method)
191+ {
192+ // Connect to the session bus
193+ QDBusConnection bus = QDBusConnection::sessionBus ();
201194
195+ // Find available MPRIS-compatible media players
196+ QStringList services = bus.interface ()->registeredServiceNames ().value ();
197+ QStringList mprisServices;
202198 for (const QString &service : services)
203199 {
204200 if (service.startsWith (" org.mpris.MediaPlayer2." ))
205201 {
206- mediaPlayerService = service;
207- break ;
202+ mprisServices << service;
208203 }
209204 }
210205
211- if (mediaPlayerService .isEmpty ())
206+ if (mprisServices .isEmpty ())
212207 {
213- LOG_DEBUG (" No active media player found on DBus" );
214- return nullptr ;
208+ LOG_ERROR (" No MPRIS-compatible media players found on DBus" );
209+ return false ;
215210 }
216211
217- LOG_DEBUG (" Found media player service: " << mediaPlayerService);
218- return new QDBusInterface (mediaPlayerService, " /org/mpris/MediaPlayer2" ,
219- " org.mpris.MediaPlayer2.Player" , sessionBus, this );
220- }
221-
222- bool MediaController::sendMediaPlayerCommand (const QString &method)
223- {
224- QDBusInterface *iface = getMediaPlayerInterface ();
225- if (!iface)
212+ bool success = false ;
213+ // Try each MPRIS service until one succeeds
214+ for (const QString &service : mprisServices)
226215 {
227- LOG_ERROR (" No media player interface available for " << method);
228- return false ;
229- }
216+ QDBusInterface playerInterface (
217+ service,
218+ " /org/mpris/MediaPlayer2" ,
219+ " org.mpris.MediaPlayer2.Player" ,
220+ bus);
230221
231- // Use QDBusMessage for more control and error handling
232- QDBusMessage message = QDBusMessage::createMethodCall (
233- iface->service (),
234- iface->path (),
235- iface->interface (),
236- method);
222+ if (!playerInterface.isValid ())
223+ {
224+ LOG_ERROR (" Invalid DBus interface for service: " << service);
225+ continue ;
226+ }
237227
238- QDBusPendingCall call = iface->connection ().asyncCall (message);
239- call.waitForFinished ();
228+ // Send the Play or Pause command
229+ if (method == " Play" || method == " Pause" )
230+ {
231+ QDBusReply<void > reply = playerInterface.call (method);
232+ if (reply.isValid ())
233+ {
234+ LOG_INFO (" Successfully sent " << method << " to " << service);
235+ success = true ;
236+ break ; // Exit after the first successful command
237+ }
238+ else
239+ {
240+ LOG_ERROR (" Failed to send " << method << " to " << service
241+ << " : " << reply.error ().message ());
242+ }
243+ }
244+ else
245+ {
246+ LOG_ERROR (" Unsupported method: " << method);
247+ return false ;
248+ }
249+ }
240250
241- if (call. isError () )
251+ if (!success )
242252 {
243- LOG_ERROR (" Failed to execute " << method << " : " << call.error ().message ());
244- delete iface;
245- return false ;
253+ LOG_ERROR (" No media player responded successfully to " << method);
246254 }
247-
248- delete iface;
249- return true ;
255+ return success;
250256}
251257
252258void MediaController::play ()
0 commit comments