MPMediaPickerController shows white screen , no error - ios

I get a white screen when I try to present MPMediaPickerController. After that, it automatically dismisses itself. I can't access music library in order to import songs in my app.
What is really weird is that there is no error printed out in the console.
I am testing on a physical iPhone device.
#IBAction func importSong(_ sender: Any) {
let mediaPicker: MPMediaPickerController = MPMediaPickerController.self(mediaTypes: MPMediaType.music)
mediaPicker.delegate = self
mediaPicker.allowsPickingMultipleItems = false
self.present(mediaPicker, animated: true, completion: nil)
}
See video : https://youtu.be/8fjeWXjObPo

Add NSAppleMusicUsageDescription in your Info.plist file, e.g.:
<key>NSAppleMusicUsageDescription</key>
<string>App wants to use music</string>

Related

How to play two audio files at the same time in iOS SDK

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!)

I cannot access the camera in a UIViewController

I really hope I'm not making a duplicate - but I read a ton different camera-questions inhere and implemented all of their answers, with the same result: Nothing happens!
No errors, the app doesn't crash, no problems whatsoever - only there is no sign of the camera, which should be activated! My goal is to get it activated in viewDidAppear or viewDidLoad, but I also tried testing it by connecting the code to a button - same result; nothing. Both on my own device and on the simulator: nothing!
What am i getting wrong in this simple code?? - Or which setting do I need to change? I have tried playing with the "data protection": nothing!
Code:
class CreateNewPerson: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func viewDidAppear () {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
private func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]!) {
PersonPhoto.image = info[UIImagePickerControllerOriginalImage] as? UIImage
dismiss(animated: true, completion: nil)
}
Hope someone can help me!
Photo of info.plist (where I can't seem to find the camera ussage description) - maybe I'm an idiot...:
Thanks!
You need to add a Camara usage description into your info.plist file and ask permission for your app to access the camera.
Add this to your plist file:
Privacy - Camera Usage Description
With some text like
"We need your permission to access the device camera"
To request permission:
AVCaptureDevice.requestAccess(for: AVMediaType.video) { granted in
if granted {
// show the image picker
} else {
// show an error
}
}
It is usually best to check if you need permission or what state the permissions are in, so I would do it like this...
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
checkCameraPermissions()
}
private func checkCameraPermissions() {
let status = AVCaptureDevice.authorizationStatus(for: .video)
switch status {
case .authorized:
self.presentPicker()
case .notDetermined:
self.requestPermissions()
case .denied:
// user denied access
self.permissionDenied()
}
}
private func requestAccess() {
AVCaptureDevice.requestAccess(for: AVMediaType.video) { granted in
if !granted {
// show an error
}
// call it again in to recheck now that permissions have changed.
checkCameraPermissions
}
}
private func presentPicker() {
// permissions are all set, continue as planned.
}
private func permissionDenied() {
// show an alert and link to app settings to turn on
// usually I would show a view which explains they have denied permission to the camera so this functionality isn't available until they manually change the setting in the app settings.
}
You have to add CreateNewPerson in UINavigationController after that run your code.
Don't forget to add Privacy - Camera Usage Description in info.plist

Cant play video in my ios app

I want to have video player in my ios application (I work with xcode version 9.2 and swift version 4). It must play m3u8 video from internet. I use the code below for this:
import UIKit
import AVKit
import AVFoundation
class ViewControllerPlayer: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let videoURL = URL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let video = AVPlayer(url: videoURL!)
let videoPlayer = AVPlayerViewController()
videoPlayer.player = video
present(videoPlayer, animated: true, completion: {
video.play()
})
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
}
but unfortunately after compile my app crashes and doesnt play video. Then I transfered code from viewDidLoad() method to viewDidAppear(). But there is no effect. So, how can i play video from internet in my ios app? Thanks.
Documentation for present method says the following about completion:
The block to execute after the presentation finishes. This block has no return value and takes no parameters. You may specify nil for this parameter.
Move video.play() above present and it should work.
However you are using HTTP, so ATS will complain about it and won't show video.
Add (BAD PRACTICE) the following code to your Info.plist to disable ATS:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

How Do I Present a Music Library With a View Controller for a MPMediaPickerControllerDelegate using Swift 3 iOS10?

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

Swift - Is it possible to select a video from the library on the iOS simulator?

All that shows up when I use the UIImagePickerController is a library of photos. I have an mp4 video saved to the simulator library, but it never shows up in the list when I access the simulator library programmatically with UIImagePickerController. Is there something I'm doing wrong?
You just need to add a video to your simulator.
Drag and drop a video file on top of the simulator window. It will show in the photos app and whenever you want a video for upload.
Based on Swift 2.2
Your code may look like this:
#IBAction func selectImageFromPhotoLibrary(sender: UIBarButtonItem) {
let imagePickerController = UIImagePickerController()
imagePickerController.sourceType = .PhotoLibrary
imagePickerController.delegate = self
imagePickerController.mediaTypes = ["public.image", "public.movie"]
presentViewController(imagePickerController, animated: true, completion: nil)
}
Actually you can get the answers from the Apple Document:
UIImagePickerController Class Reference
The useful method is class func availableMediaTypesForSourceType.
You can try this:
let types = UIImagePickerController.availableMediaTypesForSourceType(.PhotoLibrary)
print(types)
Then you know videos types are named public.movie , not kUTTypeMovie anymore.

Resources