I'm trying to play a video of type avi in an application created in xcode written in swift.
The thing is all of the videos I have are in avi and I cannot afford time spent converting the videos right now. Is there a way to play the video in the provided Media player library?
the code I wrote plays the audio of the video only:
func playVideo() {
let path = NSBundle.mainBundle().pathForResource(symbol, ofType:"avi")
let url = NSURL.fileURLWithPath(path!)
moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = moviePlayer {
player.view.frame = self.view.bounds
player.prepareToPlay()
player.scalingMode = .AspectFill
self.view.addSubview(player.view)
}
or maybe there's a way to convert a video when the function is called?
I,
following the documentation of the MPMediaPlayer :
For movie files, this typically means files with the extensions .mov, .mp4, .mpv, and .3gp
So, unfortunately, you can't use the MPMediaPlayer to read your avi video.
Thanks to VLC for iOs source code, you can re-use it to implement a custom player to read these files.
Related
I have an iOS app that streams video on iPhone/iPad devices.
Now I need to get the bitrate on video streaming on the iPhone/iPad device.
ObservedBitrate and IndicatedBitrate does not help with this.
I also came across this below post but few comments mention it works for mpeg4 videos only.
How to check Resolution, bitrate of video in iOS
I'm looking for the bitrate of video streaming on iPhone/iPad and for any video format.
Thanks!
You can listen for AVPlayerItemNewAccessLogEntry notification to get current bitrates for your stream such as:
observedBitrate - aka your current download speed
indicatedBitrate - comes from m3u8 (BANDWIDTH) and means a min bitrate value to play your current stream
Adaptive video playback with AVPlayer can change a current stream to a stream with high or low bandwidth back and forth by network conditions but these changes do not appears on every stalls, network issues etc. because your steam can be buffered, m3u8 doesn't have stream with lower bandwidth to play etc. So if you need to detect a current realtime playback state look to stalls, FPS.
There is sample code in swift but it's easy convertible to objc if you need:
let url = URL(string: "https://bitdash-a.akamaihd.net/content/MI201109210084_1/m3u8s/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8")!
let playerItem = AVPlayerItem(url: url)
self.player = AVPlayer(playerItem: playerItem)
NotificationCenter.default.addObserver(forName: .AVPlayerItemNewAccessLogEntry, object: playerItem, queue: nil) { notification in
if let event = playerItem.accessLog()?.events.last {
let bitrates = [event.observedBitrate,
event.indicatedBitrate,
event.averageVideoBitrate,
]
print(">", bitrates)
}
}
player?.play()
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = view.bounds
view.layer.addSublayer(playerLayer)
Outputs:
> [16375938.865617642, 628000.0, 307568.0]
> [nan, 1728000.0, 0.0]
> [9830221.39078689, 2528000.0, 1422032.0]
My following code plays the "fox village.mp4" video, which is commented out, but not the you tube link and gives this error:
AddInstanceForFactory: No factory registered for id F8BB1C28-BAE8-11D6-9C31-00039315CD46
Please help if you can. Thanks!!
let videoURL = URL(string: "https://youtu.be/MTNCcC_H3oM")!
// let videoURL = URL(string: "https://wolverine.raywenderlich.com/content/ios/tutorials/video_streaming/foxVillage.mp4")!
let player = AVPlayer(url: videoURL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
present(playerViewController, animated: true) {
player.play()
The first thing you need to clear that AVPlayer does not have the capacity to play youtube video from URL.
Note:
One thing you need to keep in your mind, AVPlayer is able played video if URL is direct downloadable.
In your case, you need to go with below solutions
Solution: 1
To play a youtube video, You need to go with the WKWebView or WKWebView with iFrame
OR
I suggest you need to go with YTPlayerView
OR
How to embed a Youtube video into my app?
I am trying to use an AVPlayer to play a video that has been recorded in my app. However, the player won't play the video. I know for a fact that this is a properly recorded mp4 file, because I can take it and play it on my Mac just fine. Here's the setup for the player:
let documents = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
let URL = NSURL(fileURLWithPath: "tempVideo", relativeToURL: documentsDirectory)
let asset = AVAsset(URL: URL)
let item = AVPlayerItem(asset: asset)
//videoPlayer is a property on the view controller being used
videoPlayer = AVPlayer()
//videoPlayerLayer is a property on the view controller being used
videoPlayerLayer = AVPlayerLayer(player: videoPlayer)
videoPlayerLayer.frame.size = view.frame.size
videoPlayerLayer.backgroundColor = UIColor.redColor().CGColor
view.layer.addSublayer(videoPlayerLayer!)
//wait 5 seconds
videoPlayer.play()
I know for sure that the videoPlayer is, in fact, ready to play, because I've checked its status property. I also know that videoPlayerLayer has properly been added to view.layer because its visible and takes up the whole screen. When I call videoPlayer.play(), the music playing on the device stops, but videoPlayerLayer doesn't show anything.
Any ideas? Thank you in advance for the help!
EDIT: I forgot to show that videoPlayerLayer is indeed connected to videoPlayer, I have updated my question to reflect this.
The correct answer was given by #Dershowitz123, but he or she left it in a comment so I can't mark it as correct. The solution was to change the URL to include the .mp4 extension. Thank you for your help.
I am trying to play a song from a link using the avkit, but nothin is being played. I used an online mp3 file to link converter to generate a link. If anyone has a better method to convert an mp3 file to a link I am all ears. Apart from that the real problem I'm having is why I can't hear anything. I have a feeling it's the link and it is not supported. Keep in mind that playedSong is being called in the viewDidLoad. Link to the song I am trying to play
var urlPath = "http://picosong.com/ANQY"
func playedSong(){
let url: NSURL = NSURL(string: urlPath)!
player = AVPlayer(URL: url)
player.play()
player.rate = 1.0
if(player.rate == 1.0){
print(url)//prints correct url
print("playing")//prints
}else{
print("not playing")
}
}
HI I'm developing Broadcast App for that I'm using Videocore library now how can i play that streaming video in ios app i tried with the MpMoviePlayer but it won't support the rtmp stream. so is there any third party libraries available for RTMP supported Players please help me
If you already have the RTMP live stream ready and playing as HLS then you can simply add .m3u8 after the stream name and make RTMP link to http. For example you have RTMP link like this:
rtmp://XY.Y.ZX.Z/hls/chid
You have to just make the url like this:
http://XY.Y.ZX.Z/hls/chid.m3u8
and it will play smoothly in iOS. I have tried following code and it is working fine.
func setPlayer()
{
// RTMP URL rtmp://XY.Y.ZX.Z/hls/chid be transcripted like this http://XY.Y.ZX.Z/hls/chid.m3u8 it will play normally.
let videoURL = URL(string: "http://XY.Y.ZX.Z/hls/chid.m3u8")
let playerItem = AVPlayerItem(url: videoURL!)
let adID = AVMetadataItem.identifier(forKey: "X-TITLE", keySpace: .hlsDateRange)
let metadataCollector = AVPlayerItemMetadataCollector(identifiers: [adID!.rawValue], classifyingLabels: nil)
//metadataCollector.setDelegate(self, queue: DispatchQueue.main)
playerItem.add(metadataCollector)
let player = AVPlayer(playerItem: playerItem)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
self.player = player
player.play()
}
But it will be slow and laggy because of the high resolution video stream upload. If you make the resolution to low when uploading the video stream, it will work smooth in low bandwidth network as well.