[Swift] How to play songs from your music library

How to use the music library

If you want to use the music library, you need to allow access to the music library.


Privacy - Media Library Usage Description

To play a song, use MediaPlayer to get the song from your music library and You can play it by dividing it into MPMediaItemCollection.


import MediaPlayer

How to get a list of songs from your music library

In addition to songs, you can also get a list from albums and artists from the music library.


//Get a list of songs
let mPMediaQuery = MPMediaQuery.songs()

//Get a list of albums
let mPMediaQuery = MPMediaQuery.albums()

//Get a list of artists
let mPMediaQuery = MPMediaQuery.artists()

After you get a song, you need to separate it for each song to play.


let mPMediaQuery = MPMediaQuery.songs()
if let collections = mPMediaQuery.collections {
     for collection in collections {

Set the song to play

You can specify and play songs in music with setQueue (with: MPMediaItemCollection).


var player = MPMusicPlayerController.applicationMusicPlayer

//Set a song
player.setQueue(with: MPMediaItemCollection)

//Allow the song to play











You can change the repeat method depending on the setting value.

//User favorite repeat
player.repeatMode = .default

//Do not repeat
player.repeatMode = .none

//Repeat the current song
player.repeatMode = .one

//Repeat current playlist
player.repeatMode = .one


You can change the shuffle method depending on the setting value.

//Do not shuffle
player.shuffleMode = .off

//User favorite shuffle
player.shuffleMode = .default

//Shuffle for each song
player.shuffleMode = .songs

//Shuffle for each album
player.shuffleMode = .albums



Recommended Posts

[Swift] How to play songs from your music library
How to call Swift 5.3 code from Objective-C
How to play audio and music using javascript
[Swift] How to use UserDefaults
How to use Swift UIScrollView
How to migrate from JUnit4 to JUnit5
How to transition from the [Swift5] app to the iPhone settings screen
How to push from Tarminal to GitHub
How to insert an external library
[Swift] How to use SwiftLint (cocoapods)
[Swift] How to use Unwind segue
[Swift] How to send a notification
[Swift5] How to communicate from ViewController to Model and pass a value
[Swift] How to replace multiple strings
How to change from HTML to Haml
How to return a value from Model to Controller using the [Swift5] protocol
[Swift] Summary of how to remove elements from an array (personal note)
[swift5] How to specify color in hexadecimal
How to install Play Framework 2.6 for Mac
[Rails] How to convert from erb to haml
How to use BootStrap with Play Framework
[IOS] How to get data from DynamoDB
[Swift] How to fix Label in UIPickerView
[Swift] How to use Tab Bar Controller
[Swift] How to implement the countdown function
[Flutter] How to use C / C ++ from Dart?
Post to Slack from Play Framework 2.8 (Java)
Java: How to send values from Servlet to Servlet
[Swift5] How to implement standby screen using'PKHUD'
[Swift5] How to create a splash screen
[Swift5] How to implement animation using "lottie-ios"
How to publish a library in jCenter
How to overwrite Firebase data in Swift
[Swift] How to use one option alert
How to connect to lcalhost from your smartphone and use the app under development
[swift5] How to transition from the app to an external site by specifying the URL
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
Convert from C String pointer to Swift String type
How to link Rails6 Vue (from environment construction)
How to get a heapdump from a Docker container
How to dump from database (DB) to seeds file
[Swift] How to implement the LINE login function
Note how to use Swift super basic TableView
[swift5] How to implement the Twitter share function
How to add sound in the app (swift)
How to get Class from Element in Java
[Swift] How to link the app with Firebase
How to give your image to someone with docker
[Swift UI] How to disable ScrollsToTop of ScrollView
[Java] How to switch from open jdk to oracle jdk
I want to play with Firestore from Rails
[Swift] How to implement the fade-in / out function
How to get SIMD optimization from HotSpot JavaVM
How to execute tasks in parallel in Swift in Swift Package
[ruby] How to receive values from standard input?
[Swift] Skill map from inexperienced to practical (roadmap)
[Swift, ARKit] Migrate from deprecated hitTest to raycastQuery
How to make an crazy Android music player
Add packages to your project with Swift PM