Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 4 Upload song to Apple Music Library

Tags:

ios

swift

I am building an iOS app in Swift 4 and I am trying to add a song to the users Apple Music Library, I am able to get my Apple Music Library like so:

mediaPicker = MPMediaPickerController(mediaTypes: .anyAudio)
        mediaPicker?.showsCloudItems = false
        mediaPicker?.showsItemsWithProtectedAssets = false
        if let picker = mediaPicker{
            picker.delegate = self
            picker.allowsPickingMultipleItems = false
            present(picker, animated: true, completion: nil)
        }

But is it possible to upload a song to the users Apple Music Library?

Here is how I am getting the audio file that I wish to add to the users Apple Music Library

let audioString = (result[0]["audio"] as! String).addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
                let audioUrl = URL(string:"http://example.com/uploads/" + audioString!)
                Alamofire.request(audioUrl!).responseData { response in
                    do {
                        self.audio = try AVAudioPlayer(data: response.data!)
                        self.audio?.prepareToPlay()
                    }catch{
                    }
                }

UPDATE

I found this: https://stackoverflow.com/a/17433006/979331 Now I have 2 questions, how would I convert this to Swift and if I use ScriptingBridge will this be allowed when I submit my app to the app store?

like image 856
user979331 Avatar asked Jul 13 '18 20:07

user979331


People also ask

How do I import music into Apple Music library?

In the Music app on your Mac, choose File > Add To Library or File > Import. If you selected “Copy files to Music Media folder when adding to library” in Files settings, you see File > Import. Locate a file or folder, then click Open. If you add a folder, all the files it contains are added to your library.

Can anyone upload a song to Apple Music?

As we mentioned before, it's not possible to directly upload your music to Apple Music so you will need to go through a music distributor to get your music on streaming platforms.


2 Answers

According to the Official Documentation

An MPMediaPickerController object, or media item picker, is a specialized view controller that you employ to provide a graphical interface for selecting media items.

This makes it clear that MPMediaPickerController cannot be used for uploading songs into the music library. It is intended to be used for fetching items from the User Media Library.

So even if you download the songs to your application. You cannot transfer it to the Music Library.

Hope this helps.

like image 183
Md. Ibrahim Hassan Avatar answered Oct 31 '22 22:10

Md. Ibrahim Hassan


The Apple have not provided any provision to upload the song directly to Apple Music library. Rather you can save your song locally in your application DocumentDirectory for later use.

For more Information, you can refer to this below blog:

https://mobikul.com/play-audio-file-save-document-directory-ios-swift/

like image 24
Abilash Balasubramanian Avatar answered Oct 31 '22 23:10

Abilash Balasubramanian