Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm working on an iOS app(swift) that provides a faster way for users to create playlists from their Apple Music library. After reading the documentation I still can't figure out how to get acces to the users library.
Is there a way I can get accesss to all the songs in the users library and add the song ids to an array?
To access Apple's music library, you will need to add the "Privacy - Media Library Usage Description" to your info.plist. Then you need to make your class conform to MPMediaPickerControllerDelegate. To display the Apple Music library, you present the MPMediaPickerController. To add the songs to an array, you implement the didPickMediaItems method of MPMediaPickerControllerDelegate.
class MusicPicker:UIViewController, MPMediaPickerControllerDelegate {
//the songs the user will select
var selectedSongs: [URL]!
//this method is to display the music library.
func getSongs() {
var mediaPicker: MPMediaPickerController?
mediaPicker = MPMediaPickerController(mediaTypes: .music)
mediaPicker?.delegate = self
mediaPicker?.allowsPickingMultipleItems = true
mediaPicker?.showsCloudItems = false
//present the music library
present(mediaPicker!, animated: true, completion: nil)
}
//this is called when the user selects songs from the library
func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
//these are the songs that were selected. We are looping over the choices
for mpMediaItem in mediaItemCollection.items {
//the song url, add it to an array
let songUrl = mpMediaItem.assetURL
selectedSongs.append(songURL)
}
//dismiss the Apple Music Library after the user has selected their songs
dismiss(animated: true, completion: nil)
}
//if the user clicks done or cancel, dismiss the Apple Music library
func mediaPickerDidCancel(mediaPicker: MPMediaPickerController) {
dismiss(animated: true, completion: nil)
}
}
Related
I am working on an app where app iPhone music library & play song. I am able to play a selected song as below
//MARK:- Media Picker delegate
extension ViewController : MPMediaPickerControllerDelegate
{
func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
self.dismiss(animated: true, completion: nil)
musicPlayer?.setQueue(with: mediaItemCollection)
musicPlayerFirst?.play()
}
func mediaPickerDidCancel(_ mediaPicker: MPMediaPickerController) {
self.dismiss(animated: true, completion: nil)
}
}
But my requirement is that user can select another from iPhone library & play both songs side by side without affecting each other. The user can modify one playing song (Volume, Pitch etc) with given controls.
I can play one file & modify it.
Please suggest any idea how to play two files side by side.
what about giving the mediaplayer a tag and check if its not the same that playing play side by side ( this is just an idea, not tested!)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have no idea how to select a video from the camera roll using Swift in iOS. I am making an instagram app clone and I want users to only be able to select videos from their camera roll.
I have searched this question on google and only found tutorials explaining how to select images from the camera roll.
You can use this:
func getMovie() {
let selecionadorDeFoto = UIImagePickerController()
selecionadorDeFoto.delegate = self
selecionadorDeFoto.mediaTypes = [kUTTypeMovie as String]
selecionadorDeFoto.allowsEditing = false
selecionadorDeFoto.sourceType = .photoLibrary
present(selecionadorDeFoto, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// Your code here
}
How to get all playlist available on apple music to my project . I want to access all apple music playlist ,Currently I am using MPMediaLibrary get playlist method but not getting any data or error?
`func getUserPlaylist()
{
MPMediaLibrary.requestAuthorization { (status) in
print(status)
}
let lib = MPMediaLibrary()
// let name = "playlist name"
// let id:NSUUID = NSUUID()
// let metadata = MPMediaPlaylistCreationMetadata.init(name: name)
// metadata.authorDisplayName = "author"
// metadata.descriptionText = "description"
lib.getPlaylist(with:id as UUID, creationMetadata: nil) { (playlist, error) in
guard error == nil
else
{
print(error.debugDescription)
return
}
if let currentPlaylist = playlist
{
print(currentPlaylist.name)
}
}
} `
To access Apple's music library, you will need to add the "Privacy - Media Library Usage Description" to your info.plist. Then you need to make your class conform to MPMediaPickerControllerDelegate. To display the Apple Music library, you present the MPMediaPickerController. To add the songs to an array, you implement the didPickMediaItems method of MPMediaPickerControllerDelegate.
class MusicPicker:UIViewController, MPMediaPickerControllerDelegate {
//the songs the user will select
var selectedSongs: [URL]!
//this method is to display the music library. You might call it when a user taps a button to add songs to their playlist
func getSongs() {
var mediaPicker: MPMediaPickerController?
mediaPicker = MPMediaPickerController(mediaTypes: .music)
mediaPicker?.delegate = self
mediaPicker?.allowsPickingMultipleItems = true
mediaPicker?.showsCloudItems = false
//present the music library
present(mediaPicker!, animated: true, completion: nil)
}
//this is called when the user selects songs from the library
func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
//these are the songs that were selected. We are looping over the choices
for mpMediaItem in mediaItemCollection.items {
//the song url, add it to an array
let songUrl = mpMediaItem.assetURL
selectedSongs.append(songURL)
}
//dismiss the Apple Music Library after the user has selected their songs
dismiss(animated: true, completion: nil)
}
//if the user clicks done or cancel, dismiss the Apple Music library
func mediaPickerDidCancel(mediaPicker: MPMediaPickerController) {
dismiss(animated: true, completion: nil)
}
}
I got all the playlist using the below code, I used MPMediaQuery class.
let query: MPMediaQuery = MPMediaQuery.playlists()
let playlists = query.collections
guard playlists != nil else{
return
}
for collection in playlists!{
print(playlists?.description)
}
I am trying to follow a recent post on using a MPMediaPickerControllerDelegate to present a music selection list.
The tutorial is found at this URL:
http://www.justindoan.com/tutorials/
I am using this code:
import UIKit
import MediaPlayer
class ViewController: UIViewController, MPMediaPickerControllerDelegate {
var mediapicker1: MPMediaPickerController!
override func viewDidLoad() {
super.viewDidLoad()
let mediaPicker: MPMediaPickerController = MPMediaPickerController.self(mediaTypes:MPMediaType.music)
mediaPicker.allowsPickingMultipleItems = false
mediapicker1 = mediaPicker
mediaPicker.delegate = self
self.presentViewController(mediapicker1, animated: true, completion: nil)
}
}
However I have found that the:
self.presentViewController(mediapicker1, animated: true, completion: nil)
does not work. Unfortunately, Swift 3's suggested automatic solution does not work either:
self.present(mediapicker1, animated: true, completion: nil)
Furthermore, the iOS 10 Beta Release Notes, found at:
https://www.scribd.com/doc/315770725/IOS-10-Beta-Release-Notes
says on page 10 of 18,
An MPMediaPickerController object may not display as expected.
I have spent a great deal of time looking to solve this issue on my own with no success.
Any suggestions?
Go through the steps:
Add 'NSAppleMusicUsageDescription' to your Info.plist for the privacy authority.
Make sure your Music app is available in your iPhone. It will not work in the simulator.
As per our discussion over comments, I recently use the MPMediaPickerController in my latest app named playmates. I am sharing the working code with you. I have written the self-explanatory code.
import MediaPlayer
class viewControllerName: UIViewController,MPMediaPickerControllerDelegate {
//Below is Inaction for picking music from media library
#IBAction func btnMediaPickerAction(_ sender: UIButton) {
let mediaPicker: MPMediaPickerController = MPMediaPickerController.self(mediaTypes:MPMediaType.music)
mediaPicker.delegate = self
mediaPicker.allowsPickingMultipleItems = false
self.present(mediaPicker, animated: true, completion: nil)
}
// MPMediaPickerController Delegate methods
func mediaPickerDidCancel(_ mediaPicker: MPMediaPickerController) {
self.dismiss(animated: true, completion: nil)
}
func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
self.dismiss(animated: true, completion: nil)
print("you picked: \(mediaItemCollection)")//This is the picked media item.
// If you allow picking multiple media, then mediaItemCollection.items will return array of picked media items(MPMediaItem)
}
}
I found below discripencies in your code:
use self.present not self.presentViewController
No need to create global instance of mediaPicker. As in the delegate method you are getting the instance of MPMediaPickerController
Add Privacy - Media Library Usage Description in info.plist
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
in my iOS app I want to upload an image as a profile photo. So, I need something like an Icon to upload the image and then a popup or such a user interface to make a photo by camera or to upload it from the photo album on device. It should be like Facebook.
I created my app with XCode 6 and Swift. Are there any ideas, solutions or other information?
THX!
I've solved my problem with following code:
func takePhoto(sender: AnyObject) {
if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
var picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = UIImagePickerControllerSourceType.Camera
var mediaTypes: Array<AnyObject> = [kUTTypeImage]
picker.mediaTypes = mediaTypes
picker.allowsEditing = true
self.presentViewController(picker, animated: true, completion: nil)
}
else{
NSLog("No Camera.")
}
}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {
let selectedImage : UIImage = image
}