I'm using MPMoviePlayerController to play avi formatted video files.
The audio works, but the video doesn't. Why is that?
MPMoviePlayerViewController only supports .mov, .mp4,.mpv, and .3gp and is deprecated.
From Apple's documentation:
The MPMoviePlayerViewController class is formally deprecated in iOS 9.
(The MPMoviePlayerController class is also formally deprecated.) To
play video content in iOS 9 and later, instead use the
AVPictureInPictureController or AVPlayerViewController class from the
AVKit framework, or the WKWebView class from WebKit.
You should use AVPlayerViewController:
import AVFoundation
import AVKit
let videoPath = "https://video.avi"
let videoURL = URL(string: videoPath)
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
present(playerViewController, animated: true) {
playerViewController.player?.play()
}
or just add AVPlayer in as a subview:
let videoURL = URL(string: videoPath)
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
view.layer.addSublayer(playerLayer)
player.play()
it supports the following audio/video formats:
.mpeg, .mpeg-2-video, .avi, .aifc-audio, .aac-audio, .mpeg-4, .au-audio, .aiff-audio, .mp2, .3gpp2, .ac3-audio, .mp3, .mpeg-2-transport-stream, .3gpp, .mpeg-4-audio
I think you cannot play .avi file using MPMoviePlayerController in ios.
As .avi format contents are not possible to decode the video on a iOS device.
Related
I am trying to play an HEVC(H.265) codec media url on AVPlayer. But the video is not showing. I didn't get any solution. How do I implement this in ios?
let videoURL = URL(string: "http://bitmovin-a.akamaihd.net/content/dataset/multi-codec/hevc/stream_fmp4.m3u8")
let avPlayer = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = avPlayer
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
Following your example I am able to play this content on a device (not simulator)
Pay attention also that I changed the URL from HTTP to HTTPS!
import UIKit
import AVFoundation
class ViewController: UIViewController {
let videoURL = URL(string: "https://bitmovin-a.akamaihd.net/content/dataset/multi-codec/hevc/stream_fmp4.m3u8")
let avPlayer = AVPlayer()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
let asset = AVAsset(url: videoURL!)
let playerItem = AVPlayerItem(asset: asset)
avPlayer.replaceCurrentItem(with: playerItem)
let avLayer = AVPlayerLayer(player: avPlayer)
avLayer.frame = view.bounds
view.layer.addSublayer(avLayer)
avPlayer.play()
}
}
NB:
The container format for HEVC video MUST be fMP4.
There are a few ways to deliver HEVC content to users:
HEVC in HLS using MPEG-2 Transport Stream chunks, which Apple doesn’t
support
HEVC in HLS using fMP4 segments, which is what Apple announced on
WWDC17 and our player supports
HEVC in MPEG-DASH using fMP4 segments
For reference:
WWDC17 – HEVC with HLS – Apple just announced a feature that we support out of the box
HLS Authoring Specification for Apple Devices
I have an app where I can record a video using UIImagePicker. On saving the recorded video I want to play the video in UIView within my app.
I am able to record a video and on clicking "use-video" in the UIImagePicker I am printing the video URL (url: /private/var/mobile/Containers/Data/Application/F9DFDFE8-467F-4DDB-A013-C076F3629F06/tmp/51411329851__5CDC14A6-053B-44C1-915D-0E87D9068D6E.MOV).
I want to load the video using the above url and play it in UIView of my app.
Any help will be appreciated.
Thank You.
you can use following code for playing video using AVPlayerViewController
let videoURL = URL(string: YOUR_URL)
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
I am making a small website for video viewing as a project,
I need to be able to play a video using the local player of the phone, in Android i managed to find the solution:
intent://localhost/video.avi#Intent;action=android.intent.action.VIEW;scheme=http;type=video/mp4;end
This tells the Android OS to open this file using a media player, I need something like this for iOS.
I couldn't find anything on the internet about this, all other posts deal with making an actual application, I don't have access to an iPhone making this even harder to test and play around with it.
How can I do this for iPhone? if not possible are there alternatives?
Notice the code is a mere URL and needs absolutely no further implementations
There's URL Schemes provided by Apple to launch different native stuff via links.
try this: videos://"your video url here"
as an alternative: youtube://"youtube video url here"
About Apple iPhone URL schemes:
https://developer.apple.com/library/content/featuredarticles/iPhoneURLScheme_Reference/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007899-CH1-SW1
Building an iOS application you can use AVFoundation framework, it has AVPlayer (samples are in Swift):
let videoUrl = NSURL(string: "video url here")
let videoPlayer = AVPlayer(URL: videoUrl!)
let playerLayer = AVPlayerLayer(player: videoPlayer)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
you can also use AVPlayerViewController class for it:
let videoUrl = NSURL(string: "video url here")
let videoPlayer = AVPlayer(URL: videoUrl!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
if let player = playerViewController.player {
player.play()
}
}
or you can use a UIWebView to play video from a player within a web page:
let webview = UIWebView(frame: CGRect(x: 0, y: 0, width: 240, height: 375))
Hy, I'm trying to understand if there is a way to get sound from the player even when your iPhone is set to silent .. Is there a Swift code to enable the Tone?
//IMPOSRT FOUNDATION PER PLAYER
import AVFoundation
let url = NSURL(string: "https://mywebsite.site/file/\(audio_r!)")
playerItem = AVPlayerItem(URL: url!)
player = AVPlayer(playerItem: playerItem!)
let playerLayer = AVPlayerLayer(player: player!)
playerLayer.frame = CGRectMake(0,0,300,50)
self.view.layer.addSublayer(playerLayer)
//imposto il volume
player!.muted = false
player!.volume = 1.0
Someone can help me?
A playback category should not obey the mute switch
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
AVAudioSession.sharedInstance().setActive(true)
I have a video player in an IOS App and I want to update the video when I click on a button, but I do not see how to manage this.
(Note : it's not a list of video within a queue)
Here is the code for adding the AVPlayer:
let url = NSURL.fileURLWithPath(path)
let player = AVPlayer(URL: url)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
..
..
self.ViewForVideo.addSubview(playerViewController.view)
self.addChildViewController(playerViewController)
player.play()
I have done like this : each time I want to change the video I create a new AVPlayer and affect it to the playerViewController.player like this
let url = NSURL.fileURLWithPath(path)
let player = AVPlayer(URL: url)
playerViewController.player = player