How to play RTMP video streaming in ios app? - ios

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.

Related

How to prevent Google Cast app to be suspended while playing audio?

I am developing an iOS app in Swift that plays audio files on TV over Google Cast.
App uses two-way communication over TextChannel to exchange information with the receiver app.
The app is working as expected until I put the app into the background. After the app is backgrounded the Google Cast session gets disconnected.
To ensure app is not susspended in the background while playing audio I have in my AppDelegate this code snippet:
try session.setCategory(AVAudioSession.Category.playback,
mode: AVAudioSession.Mode.default,
options: [AVAudioSession.CategoryOptions.mixWithOthers, AVAudioSession.CategoryOptions.allowAirPlay])
try session.setActive(true)
This is how I initialise the Google Cast:
let criteria = GCKDiscoveryCriteria(applicationID: kReceiverAppID)
let options = GCKCastOptions(discoveryCriteria: criteria)
options.suspendSessionsWhenBackgrounded = false
GCKCastContext.setSharedInstanceWith(options)
GCKCastContext.setSharedInstanceWith(options)
If I add a dummy AVPlayer to my ViewController and play a random mp3 file on the player while I play other audio files overcast connection to the Google Cast session remains connected and the app works as expected until mp3 stops playing on AVPlayer.
Here is my AVPlayer snippet:
let videoURL = URL(string: "https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_5MG.mp3")
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
Can anybody help me prevent the app from being suspended when it plays audio only while casting to TV?
Thanks!

Play Video From Instagram

recently I have see that one of my apps feature stopped working. The main problem is that, now, I can play instagram videos from an URL. This feature was working time ago, but I can not say how time is broken because I have noticed the problem now.
For example, I have this URL from Instagram:
https://embed.storiesig.net/aHR0cHM6Ly9zY29udGVudC1hdGwzLTIuY2RuaW5zdGFncmFtLmNvbS9vMS92L3QxNi9mMS9tMTcvQzQ0NjhFNDI4NTkwN0JCM0M3QkREQjFEQ0MzMTJCODhfdHJhbnNjb2RlX291dHB1dF9kYXNoaW5pdC5tcDQ/ZWZnPWV5SnhaVjluY205MWNITWlPaUpiWENKcFoxOTNaV0pmWkdWc2FYWmxjbmxmZG5SelgyOTBabHdpWFNJc0luWmxibU52WkdWZmRHRm5Jam9pZG5SelgzWnZaRjkxY214blpXNHVOekl3TG5OMGIzSjVMbVJsWm1GMWJIUWlmUSZfbmNfaHQ9c2NvbnRlbnQtYXRsMy0yLmNkbmluc3RhZ3JhbS5jb20mX25jX2NhdD0xMDUmdnM9MTc4NzE0ODIzOTk2MjExNTBfMzM4ODI0NTY4MiZfbmNfdnM9SEJrY0ZRSVlXR2xuWDNOMGIzSnBaWE5mY0dWeWJXRnVaVzUwWDNCeWIyUXZRelEwTmpoRk5ESTROVGt3TjBKQ00wTTNRa1JFUWpGRVEwTXpNVEpDT0RoZmRISmhibk5qYjJSbFgyOTFkSEIxZEY5a1lYTm9hVzVwZEM1dGNEUVZBQUxJQVFBb0FCZ0FHd0dJQjNWelpWOXZhV3dCTVJVQUFDYjBvdWpjbG8zRVB4VUNLQUpETXl3WFFCUUFBQUFBQUFBWUVtUmhjMmhmWW1GelpXeHBibVZmTVY5Mk1SRUFkZWdIQUElM0QlM0QmY2NiPTktNCZvZT02MUIzRURDRSZvaD05ZjIyMmE3NWYxZmE1ZmYzNDlhZGM4MWNhNzFhZjExNCZfbmNfc2lkPWJhYjYzOA==
If you open the URL in web browser (Safari, Chrome, etc...) you will see the video running.
But If I try to play the video from a AVPlayer like it used to work, the video is not played. My code for video playback is like this:
let videoUrl = story.videoUrl!
if (videoUrl.absoluteString.lengthOfBytes(using: .utf8) > 0) {
let player = AVPlayer(url: videoUrl)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
}
Where story.videoUrl is the URL of the example. If yo play the URL in a AVPlayer you will see nothing, so I don't know what is going on.

how to get bitrate of video streaming on iPhone/iPad devices?

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]

Error while playing You Tube videos in IOS Swift app

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?

AVPlayer not playing video from server?

In my application I've to stream videos from server. For that I've used below Code
-(void)playingSong:(NSURL*) url{
AVAsset *asset = [AVAsset assetWithURL:url];
duration = asset.duration;
playerItem = [AVPlayerItem playerItemWithAsset:asset];
player = [AVPlayer playerWithPlayerItem:playerItem];
[player play];
}
All are Global Variables
It's playing all videos when network is good, but unable to play videos with big size, when network is slow.
Means It's not playing for big size videos and it's playing small videos;
I'm using http Server not https;
for ex : 3min video it's playing but for 1hr video it's not.
Why so?
Seems like you have to download the whole video before you can begin playback. It can also be because of your server not AVPlayer.
when you serve videos on a site using plain HTTP – known as
progressive download – the position of the header becomes very
important. Either the header is placed in the beginning of the file or
it’s places in the end of the file. In case of the latter, you’ll have
to download the whole thing before you can begin playback – because
without the header, the player can’t start decoding.
Have look at this guide if your problem is because of videos source.
Have a look at this thread and change you implementation accordingly.
Download video in local and then play in avplayer.
DispatchQueue.global(qos: .background).async {
do {
let data = try Data(contentsOf: url)
DispatchQueue.main.async {
// store "data" in document folder
let fileUrl = URL(fileURLWithPath: <#localVideoURL#>)
let asset = AVAsset(url: fileUrl)
let item = AVPlayerItem(asset: asset)
let player = AVPlayer(playerItem: item)
let layer = AVPlayerLayer(player: player)
layer.bounds = self.view.bounds
self.view.layer.addSublayer(layer)
player.play()
}
} catch {
print(error.localizedDescription)
}
}

Resources