Black Screen when stream video with avkit - swift

Hi I want to stream remote video on my app. URL is: http://test.ext/myvideo.mkv
I use AVKit with this code but screen and audio not works. I see only progress bar that go forward.
let player = AVPlayer(url: url)
// Create a new AVPlayerViewController and pass it a reference to the player.
let controller = AVPlayerViewController()
controller.player = player
controller.player?.automaticallyWaitsToMinimizeStalling = false
controller.player?.playImmediately(atRate: 1.0)
// Modally present the player and call the player's play() method when complete.
self.present(controller, animated: true) {
player.play()
}

AVPlayer don't support mkv video format.
Anyone have a solution for playing all video format ?
I've tested MobileVLCKit but it's too heavy (1,5Go)...

Related

AV Player not working in a container view Swift

I am trying to get a video to play within a container view controller. I would like the video to play only within the container view but have the option to full screen it while playing.
I have a piece of code that can play a video with the screen automatically going full screen using an ‘off container’ button but, I cannot get the video to play within the container. Also, I am unaware of how to use the play button showing on the container view in my image above. The AV Player controller is embedded in the container.
Code below:
#IBAction func playButton(_ sender: Any) {
if let path = Bundle.main.path(forResource: "SampleVideo", ofType: "mp4") {
let video = AVPlayer(url: URL(fileURLWithPath: path))
let videoPlayer = PlayerViewController()
videoPlayer.player = video
present(videoPlayer, animated: true, completion: {
video.play()
})
}
}
No longer an issue. I found out I should have been using AV Player Layer. Now I have the video playing in a UIImageView and is doing what I needed.

How to stream remote audio in iOS 13? (SwiftUI)

This code using AVPlayer works only on Playground
import AVFoundation
var player = AVPlayer()
let playerItem = AVPlayerItem(url: URL(string: "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3")!)
player = AVPlayer(playerItem: playerItem)
player.play()
When I tried to run it on my SwiftUI App on my physical device, using this code:
Button(action:{
var player = AVPlayer()
let playerItem = AVPlayerItem(url: URL(string: "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3")!)
player = AVPlayer(playerItem: playerItem)
player.play()
print("Works")
},label:{
Image("play")
})
It prints Works to the console. However, it does not play any sound on the device.
Would appreciate any help, can't find anything yet here.
Thank you so much!
In SwiftUI, Views are value types. They are only data that describe the things on screen. They can be created or destroyed or copied at any time. AVPlayer is a reference to a specific player object. You're assuming here that it will continue to exist, and there will only be one of them. That's not something that a SwiftUI View provides.
You need to move your AVPlayer outside of the View (into Model objects), and just bind UI actions to it.

Is it possible to make preview image while scroll video timeline like in Netflix app?

Is it possible to make peview image while listing video timeline, like in Netflix App with AVPlayer?
exmple here
I found that mp4 video has that preview window, but m3u8 format has not it
func playVideo(cell: MovieCell) {
guard let urlVideo = URL(string: cell.movieSrc) else { return }
// Create an AVPlayer, passing it the HTTP Live Streaming URL.
let player = AVPlayer(url: urlVideo)
// Create a new AVPlayerViewController and pass it a reference to the player.
let controller = AVPlayerViewController()
controller.player = player
// Modally present the player and call the player's play() method when complete.
self.present(controller, animated: true) {
player.play()
}
}
This is called "Trick Play" and must be included in the HLS manifest for AVPlayer to handle this for you.
See the Trick Play section in HLS Authoring Specification for Apple Devices:
6.1. I-frame playlists (EXT-X-I-FRAME-STREAM-INF) MUST be provided to support scrubbing and scanning UI.

tvOS: AVPlayer Audio Plays but there's no video

I have this code as part of a tvOS app
import Foundation
import UIKit
import AVFoundation
import AVKit
class videoPlayer: UIViewController {
var thePlayer: AVPlayer?
var movieToPlay: AVPlayerItem?
let movieURL="http://trailers.apple.com/movies/independent/walkingwiththeenemy/walkingwiththeenemy-tlr1_720p.mov"
override func viewDidLoad(){
let theVideoPlayer = AVPlayerViewController()
theVideoPlayer.player = thePlayer
self.addChildViewController(theVideoPlayer)
self.view.addSubview(theVideoPlayer.view)
theVideoPlayer.view.frame = self.view.frame
let movieToPlay = AVPlayerItem(URL: NSURL(string: movieURL)!)
thePlayer = AVPlayer(playerItem: movieToPlay)
self.thePlayer!.play()
}
}
When the view is loaded the code plays audio but no video.
When one swipes the Apple TV remote a timeline appears with 0:00 duration at the right and 0:00 at the left as the audio plays. There is also a red no-smoking-like sign that appears at the left end of the timeline. My understanding is that the sign means that the video is in the wrong format, but the audio plays (and the clip is from Apple). The video plays plays in other AppleTV apps.
I think the code is correct (it works in another app) and I can't figure out next steps to debug. Can anyone see an error?
The code above worked just as expected when I placed it in a separate viewController and then presented it from the viewController where is had resided. I don't understand why, exactly, but it solved the problem video with audio issue.

I need to implement music player using AV Player in SWIFT

I need to implement streaming music player using AV Player in SWIFT
that music file Stored in server.Please help me to any one..How to implement this one
According to this: http://www.techotopia.com/index.php/IOS_8_Video_Playback_using_AVPlayer_and_AVPlayerViewController
You could add following code in your ViewController, like viewDidLoad method or viewWillAppear, depends on the view will show repeatedly or not. If Yes, add it in the viewWillAppear method.
let player = AVPlayer(URL: url)
let playerController = AVPlayerViewController()
playerController.player = player
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
player.play()