Reputation: 407
In my project I am using MPMediaPickerController to pick a file from the device music library. I am using following piece of code for displaying the media picker.
MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
mediaPicker.delegate = self;
mediaPicker.prompt = @"Select a personal message";
[self presentViewController:mediaPicker animated:NO completion:nil];
Everything is working fine,but the MediaPickerController is taking a couple of seconds (2 to 3 sec) for showing the music library. Is there any way to reduce this loading time of MPMediaPickerController. This delay after clicking the upload button is realy a bad user experience.
Upvotes: 0
Views: 582
Reputation: 535
I noticed my MediaPickerController was very slow as well (in my current project, using Swift 2.2), even though it was pretty quick earlier in the project (and most of what I have added since has been networking code).
In my code, I was instantiating the MPMediaPickerController only when the user tapped my "Find a song" button. By creating the instance of MPMediaPickerController at the time of the main view loading (I placed it in my class declaration, outside of viewDidLoad), I was (somehow) able to bring that load time back down to less than a second (whereas it sometimes didn't even show up, when I instantiated the MPMediaPickerController at the last possible second).
TLDR: write this:
import UIKit
import MediaPlayer
import MobileCoreServices
class SomeViewController: UIViewController, MPMediaPickerControllerDelegate {
var mediaPickerController = MPMediaPickerController(mediaTypes: .AnyAudio)
func viewDidLoad() {
mediaPickerController.delegate = self
mediaPickerController.prompt = "Select a song that you like"
}
@IBAction func buttonWasTapped(sender: AnyObject) {
self.presentViewController(mediaPickerController, animated: true, completion: nil)
}
}
Instead of this: (notice how I only instantiate mediaPickerController once my button is tapped.
import UIKit
import MediaPlayer
import MobileCoreServices
class SomeViewController: UIViewController, MPMediaPickerControllerDelegate {
func viewDidLoad() {
}
@IBAction func buttonWasTapped(sender: AnyObject) {
var mediaPickerController = MPMediaPickerController(mediaTypes: .AnyAudio)
mediaPickerController.delegate = self
mediaPickerController.prompt = "Select a song that you like"
self.presentViewController(mediaPickerController, animated: true, completion: nil)
}
}
Upvotes: 1
Reputation: 730
As matt suggested, a spinning activity indicator can really help the user experience as it reassures the user that it's working and it hasn't crashed. I used MBProgressHUD for this when loading a track from the music library.
Upvotes: 0
Reputation: 535304
There's nothing you can do. If there is a lot of music, it takes time to survey all of it and prepare the controller, and that's all there is to it. This is no different from the delay the first time the user tries to use the Music app. You might be able to reduce the amount of material shown by turning off showsCloudItems
, but even that might not help.
The best you can do is probably to try to cover the delay psychologically, i.e. by giving the user something to see, such as a spinning activity indicator, until everything is in place. I have not tried this with a MPMediaPickerController, however (I've done it with my own home-built music library exploration interface, which has the same delay issues).
Upvotes: 1