I am creating a music player to play the selected item when a user taps on the tableview cell using MPMusicPlayerController's setNowPlayingItem. The following code works well in Objective C, But when I tried to convert this into swift it doesn't works. Can anybody suggest what is the equivalent for the following code in SWIFT ?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MPMediaItem *song = self.songs[indexPath.row];
[self.musicPlayer setNowPlayingItem:song];
[self.musicPlayer play];
}
Found the solution
var mySongs = [MPMediaItemCollection]()
var songsQry = MPMediaQuery.songsQuery()
songsQry.groupingType = MPMediaGrouping.Title
mySongs = songsQry.collections as [MPMediaItemCollection]
var playQueue = MPMediaItemCollection(items: mySongs)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
var currentSong: MPMediaItem
currentSong = songs[indexPath.row].representativeItem
let musicPlayer = MPMusicPlayerController.systemMusicPlayer()
musicPlayer.setQueueWithItemCollection(playQueue)
musicPlayer.nowPlayingItem = currentSong
musicPlayer.play()
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.