Skip to content

Instantly share code, notes, and snippets.

@dmattera
Last active October 25, 2018 21:27
Show Gist options
  • Save dmattera/a8ada310744a7fae8f90710e88fc8624 to your computer and use it in GitHub Desktop.
Save dmattera/a8ada310744a7fae8f90710e88fc8624 to your computer and use it in GitHub Desktop.
play_song.py
from objc_util import *
def play_song(my_song_title, my_song_artist):
MPMediaQuery = ObjCClass('MPMediaQuery')
songsQuery = MPMediaQuery.songsQuery()
my_songs = []
for result in songsQuery.valueForKey_('collections'):
for mediaItem in result.valueForKey_('items'):
title = mediaItem.valueForKey_('title')
album = mediaItem.valueForKey_('albumTitle')
playCount = int(str(mediaItem.valueForKey_('playCount')))
persistentID = mediaItem.valueForKey('persistentID')
song = Song(title, album, playCount, persistentID, mediaItem)
if my_song_title == str(title) and my_song_artist == str(artist):
songs.append(song)
MPMediaItemCollection = ObjCClass('MPMediaItemCollection')
collection = MPMediaItemCollection.collectionWithItems_(ns([songs[0].objcMediaItem]))
MPMusicPlayerController = ObjCClass('MPMusicPlayerController')
player = MPMusicPlayerController.systemMusicPlayer()
player.setQueueWithItemCollection_(collection)
player.play()
play_song(my_song_title="Come Together", my_song_artist="The Beatles")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment