Hello
I create a code that access camera, I convert my code to swift 2 and I receive this warning:
'MPMoviePlayerController' was deprecated in iOS 9.0: Use AVPlayerViewController in AVKit.
So I try use AVPlayerViewController, but I have error, in my first code using swift 1.2 I do this:
import UIKit
import MediaPlayer
import MobileCoreServices
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var myImageView : UIImageView!
var myMoviePlayerController : MPMoviePlayerController!
func updateDisplay() -> Void
{
......
myMoviePlayerController = MPMoviePlayerController(contentURL: myMovieURL)
myMoviePlayerController.play()
...
}
}I have error warning on this lines:
var myMoviePlayerController : MPMoviePlayerController!
myMoviePlayerController = MPMoviePlayerController(contentURL: myMovieURL)
myMoviePlayerController.play()
So I change to this:
var myMoviePlayerController : AVPlayerViewController!
myMoviePlayerController = AVPlayerViewController(contentURL: myMovieURL)
AVPlayerViewController.play()But this lines:
myMoviePlayerController = AVPlayerViewController(contentURL: myMovieURL)
AVPlayerViewController.play()
show this errors:
Cannot assign a value of type 'MPMoviePlayerController!' to a value of type 'AVPlayerViewController!'
Value of type 'AVPlayerViewController' has no member 'play'
This is the comple code change:
import UIKit
import MediaPlayer
import MobileCoreServices
import AVKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var myImageView : UIImageView!
@IBOutlet weak var myButton : UIButton!
var myImageFrame : CGRect!
var myLastMediaType : String!
var myMovieURL : NSURL!
var myImage : UIImage!
var myMoviePlayerController : AVPlayerViewController!
//var myMoviePlayerController : MPMoviePlayerController! // warning
override func viewDidLoad() {
super.viewDidLoad()
if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) == false)
{
myButton.hidden = true
}
myImageFrame = myImageView.frame
}
@IBAction func getMediaRealTime()
{
self.getMediaFromControllerSourceType(UIImagePickerControllerSourceType.Camera)
}
@IBAction func getMediaFromLibrary()
{
self.getMediaFromControllerSourceType(UIImagePickerControllerSourceType.PhotoLibrary)
}
func getMediaFromControllerSourceType(sourceType: UIImagePickerControllerSourceType) -> Void
{
let arrayMediaTypes : NSArray = UIImagePickerController.availableMediaTypesForSourceType(sourceType)!
print(arrayMediaTypes)
if(arrayMediaTypes.count > 0 && UIImagePickerController.isSourceTypeAvailable(sourceType))
{
let imagePickerController : UIImagePickerController = UIImagePickerController()
imagePickerController.sourceType = sourceType
imagePickerController.allowsEditing = true
imagePickerController.delegate = self
self.presentViewController(imagePickerController, animated: true, completion: nil)
}
}
func imagePickerControllerDidCancel(picker: UIImagePickerController)
{
picker.dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
myLastMediaType = info[UIImagePickerControllerMediaType] as! String
if(myLastMediaType as String == kUTTypeMovie as String)
{
myMovieURL = info[UIImagePickerControllerMediaURL] as! NSURL
}
else if(myLastMediaType as String == kUTTypeImage as String)
{
myImage = info[UIImagePickerControllerEditedImage] as! UIImage
}
picker.dismissViewControllerAnimated(true, completion: nil)
}
func updateDisplay() -> Void
{
if(myLastMediaType == nil)
{
return
}
if(myLastMediaType as String == kUTTypeMovie as String)
{
if(myMoviePlayerController != nil)
{
myMoviePlayerController.view.removeFromSuperview()
}
myMoviePlayerController = AVPlayerViewController(contentURL: myMovieURL) // error line
// myMoviePlayerController = MPMoviePlayerController(contentURL: myMovieURL) // warning
myMoviePlayerController.view.frame = myImageFrame
myMoviePlayerController.view.clipsToBounds = true
self.view.addSubview(myMoviePlayerController.view)
AVPlayerViewController.play() // error line
myImageView.hidden = true
}
else if(myLastMediaType as String == kUTTypeImage as String)
{
myImageView.image = myImage
myImageView.hidden = false
if(myMoviePlayerController != nil)
{
myMoviePlayerController.view.hidden = true
}
}
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.updateDisplay()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
/
}
}