Play MP4 using MPMoviePlayerController() in Swift
So this was blindly annoying:
All I did was change:
self.moviePlayerController.contentURL = NSURL(string: filePath)
TO:
self.moviePlayerController.contentURL = NSURL.fileURLWithPath(filePath)
Play Video File from iOS Device
Ok given your scenario the following documentation should help you - It's written in objective c but you should be able to work out the swift counterpart:
https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html#//apple_ref/doc/uid/TP40009767-CH2-SW43
https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html#//apple_ref/doc/uid/TP40009767-CH2-SW43
Please mark as answer when you understand it all :)
iPhone - issue in playing mp4 video with Swift - iOS
Your code looks correct, there is no issue in that. Probably you forgot to add that file to your target membership.
- Select your file
- Choose the file inspector
- Under Target Membership select your target
mp4 video on iOS app using Swift
The URL that Picovico have given you actually responds with a message telling the requester that the content is not at this location but that they should try a different location - this is a common technique for URL redirecting.
You can see this if you try the link in a browser and capture the network request and responses. For example using the link you include above in Chrome gives this response:
You can see that the server is telling the client (browser in this case) to redirect (i.e. send a new request) to an S3 amazon URL which is where your video actually has been stored by Picovico.
When the browser then sends it's request to that location it finds the video successfully:
Looking at your video using ffprobe the format seems fine so it seems likely that the iOS client is having a hard time handling the redirect, or else that there was an issues with the URL redirection temporarily on Picovico's servers. If you still have the problem now then the former is most likely the problem.
If you take a look here you can see how someone has solved the problem of iOS media player handling redirects:
- iOS Mediaplayer and URL redirection
How to play a local video with Swift?
Sure you can use Swift!
1. Adding the video file
Add the video (lets call it video.m4v
) to your Xcode project
2. Checking your video is into the Bundle
Open the Project Navigator
cmd + 1
Then select your project root
> your Target
> Build Phases
> Copy Bundle Resources
.
Your video MUST be here. If it's not, then you should add it using the plus button
3. Code
Open your View Controller and write this code.
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
playVideo()
}
private func playVideo() {
guard let path = Bundle.main.path(forResource: "video", ofType:"m4v") else {
debugPrint("video.m4v not found")
return
}
let player = AVPlayer(url: URL(fileURLWithPath: path))
let playerController = AVPlayerViewController()
playerController.player = player
present(playerController, animated: true) {
player.play()
}
}
}
ios AVPlayerViewController can not play local mp4
Try this:
guard let path = NSBundle.mainBundle().pathForResource("drum", ofType:"mp4") else {
break
}
let url = NSURL(fileURLWithPath: path)
self.loadPlayer(url)
then:
private func loadPlayer(url: NSURL) {
// present video window
let playerItem = AVPlayerItem(URL: url)
let player = AVPlayer.init(playerItem: playerItem)
self.playerViewController.player = player
self.view.addSubview(self.playerViewController.view)
self.videoControlView.alpha = 1.0
self.setupPlayerObserver()
self.playPlayer()
self.setSlider()
}
Related Topics
Swift Sprite Kit in App Purchase
Get the Type of Anyobject Dynamically in Swift
Explit Conformance to Codable Removes Memberwise Initializer Generation on Structs
Left Aligned Horizontal Stackview and Top Aligned Vertical Stackview
Animate Path Stroke Drawing in Swiftui
Reading Data from Excel Document in a Swift App
Hex String to Text Conversion - Swift 3
Easiest Way to Increment a Data Point in Firebase
Xcode 8 Swift 3 Pitch-Altering Sounds
Best Way to Structure My Firebase Database
Changing Tab Bar Color (Swift)
Is There an Kotlin Equivalent 'With' Function in Swift
Swift Array to Array of Tuples
Swiftui View Does Not Updated When Observedobject Changed
How to Increase the Scope of Variables in Switch-Case/Loops in Swift