Last active
October 25, 2018 21:27
-
-
Save dmattera/a8ada310744a7fae8f90710e88fc8624 to your computer and use it in GitHub Desktop.
play_song.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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