IOS 12.4.4 AVPlayer is not playing audio - ios

I am trying to develop radio streaming app from URL audio using AVPlayer I was testing (simulator IOS 13.0 ) and everything was working.
issue is when I try to run physical device with IOS 12.4.4 nothing is working(playing)
this is what I did so far.
let url = "http://radiostreaming"//streaming url
var isPlaying = false;
var player : AVPlayer!
var playerItem : AVPlayerItem!
override func viewDidLoad() {
super.viewDidLoad()
playerItem = AVPlayerItem(url: URL(string: url)!)
player = AVPlayer(playerItem: playerItem)
player.volume = 1.0
player.rate = 1.0
player.allowsExternalPlayback = true
player.play()
}
any help please

Related

Video not play in AVPlayer or safari but play in chrome

I have open AVPlayer to play the video by url link, but video not play in player and also not play in safari but video play on chrome browser.
My code is >>>>>>>>>
if url != nil {
let player = AVPlayer(url: url!)
player.allowsExternalPlayback = true
let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = .resizeAspectFill
player.play()
}
Try this >>>
let player = AVPlayer(url: url)
player.rate = 1
playerViewController.player = player
playerViewController.view.frame = self.view.frame
playerViewController.showsPlaybackControls = true
addChild(playerViewController)
self.view.addSubview(playerViewController.view)
playerViewController.didMove(toParent: self)
It is work fine .

using avplayerkit issue while playing video black screen coming

hello i am new to swift and i am using AVPlayerViewController for plaing video from my url but issue is that i am not able to load video only black screen showing let me show my code
Code
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
#IBOutlet weak var viewPlayer: UIView!
var player: AVPlayer!
var avpController = AVPlayerViewController()
var url = "https://www.youtube.com/watch?v=HsQvAnCGxzY"
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: self.url)
player = AVPlayer(url: url!)
avpController.player = player
avpController.view.frame.size.height = viewPlayer.frame.size.height
avpController.view.frame.size.width = viewPlayer.frame.size.width
self.viewPlayer.addSubview(avpController.view)
// Do any additional setup after loading the view, typically from a nib.
}
}
Please refer code given and tell me where i am done wrong so that i can able to play video Thanks In Advance
Actually Its problem of URL .
AVPlayer never supports YouTube video. It supports mp4 format.
If you want to play a video in your ViewController, you have to use AVPlayerLayer. You have to implement player controllers yourself.
override func viewDidLoad() {
super.viewDidLoad()
// creating video url
guard let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4") else {
return
}
// create AVPlayer
let player = AVPlayer(url: videoURL)
// setup AVPlayerLayer
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
// start playing
player.play()
}
Otherwise use AVPlayerViewController. Instead of using its view present the viewController itself.
override func viewDidLoad() {
super.viewDidLoad()
// creating video url
guard let videoURL = URL(string: "https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4") else {
return
}
// setup AVPlayer and AVPlayerViewController
let player = AVPlayer(url: videoURL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
}
override func viewDidAppear(_ animated: Bool) {
// presenting playerViewController only after the viewControllers view was added to a view hierarchy.
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
}

Play Local Video File

I have followed a tutorial and trying to get a video to pop up when I press a button. The video pops up but the actual video does not start playing and I just get this screen....
Blank Screen image
the code is as follows:
import UIKit
import AVFoundation
import AVKit
class ViewController: UIViewController {
var playerController = AVPlayerViewController()
var player:AVPlayer?
override func viewDidLoad() {
super.viewDidLoad()
let videoString:String? = Bundle.main.path(forResource: "motorbikes", ofType: ".m4v")
if let url = videoString {
let videoURL = NSURL(fileURLWithPath: url)
self.player = AVPlayer(url: videoURL as URL)
}
}
#IBAction func goButton(_ sender: Any) {
self.present(self.playerController, animated: true, completion: {
self.playerController.player?.play()
})
}
}
You have forgot to set playercontroller's player instance as phuc-nguyen said
or else you can make use of AVPlayerLayer to play the video without using AVPlayerViewController
let player = AVPlayer(url: URL(string: "path/myvideo.mp4")!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
Using AVPlayerLayer you can customize the player like adding your own controls or views and it won't be a full screen view usually though.
But if you are not looking for any customization its good to stick with AVPlayerViewController it has all the controls baked in
Depends on your requirement

play video in background swift 3

hello im trying to play video in background but its give an error and the error is " unexpectedly found nil while unwrapping an Optional value " and im already set the video in Bundle and select copy if need
this the my code
import UIKit
import AVFoundation
class ViewController: UIViewController {
var avPlayer: AVPlayer!
var avPlayerLayer: AVPlayerLayer!
var paused: Bool = false
override func viewDidLoad() {
super.viewDidLoad()
let url = Bundle.main.url(forResource: "Grad_Cap_Toss", withExtension: "mp4")
avPlayer = AVPlayer(url: url!)
avPlayerLayer = AVPlayerLayer(player: avPlayer)
avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
avPlayer.volume = 0
avPlayer.actionAtItemEnd = AVPlayerActionAtItemEnd.none
avPlayerLayer.frame = view.layer.bounds
view.backgroundColor = UIColor.clear;
view.layer.insertSublayer(avPlayerLayer, at: 0)
NotificationCenter.default.addObserver(self,selector: Selector(("playerItemDidReachEnd:")),name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,object: avPlayer.currentItem)
}
please any body have ideas for that , thank you
First, drag and drop video file in your project.
Then import AVFundation.
Add example: var player: AVPlayer?
#IBOutlet weak var videoView: UIView! - Connect view from storyboard to your VC class.
After that, you paste this code into your ViewController.
call "playBackgoundVideo()" in your viewDidLoad()
private func playBackgoundVideo() {
if let filePath = Bundle.main.path(forResource: "your_video_here", ofType:"mp4") {
let filePathUrl = NSURL.fileURL(withPath: filePath)
player = AVPlayer(url: filePathUrl)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.videoView.bounds
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem, queue: nil) { (_) in
self.player?.seek(to: kCMTimeZero)
self.player?.play()
}
self.videoView.layer.addSublayer(playerLayer)
player?.play()
}
}
This is swift 4.0
it works ^__^
I had a similar problem and followed the instructions from Rick's answer to resolve it.
My problem was that I didn't have the video file in the bundle resources.
Check if your video file is listed in Copy Bundle Resources under Build Phases in the project. If not , use the + button to add the file in there.
Also you might be missing the avPlayer.play() statement in your code.
I was also trying to play a video in the background of my login page's view controller. I think the main issue is that the syntax you're using is deprecated.
Your code might look like the following:
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
#IBOutlet var blackOverlay: UIView!
// init video background and its path
var player: AVPlayer?
let videoURL: NSURL = Bundle.main.url(forResource: "videoName", withExtension: "mp4")! as NSURL
override func viewDidLoad() {
super.viewDidLoad()
// Adds a black overlay to the looped video in question
blackOverlay.alpha = 0.75;
blackOverlay.layer.zPosition = 0;
// begin implementing the avplayer
player = AVPlayer(url: videoURL as URL)
player?.actionAtItemEnd = .none
player?.isMuted = true
let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
playerLayer.zPosition = -1
playerLayer.frame = view.frame
view.layer.addSublayer(playerLayer)
player?.play()
// add observer to watch for video end in order to loop video
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.loopVideo), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.player)
// if video ends, will restart
func playerItemDidReachEnd() {
player!.seek(to: kCMTimeZero)
}
}
// maybe add this loop at the end, after viewDidLoad
// func loopVideo() { player?.seek(to: kCMTimeZero) player?.play()}}
Please let me know if this helps!
Drag and drop video file in your project.
Then import AVFundation
List item
#IBOutlet weak var videoView: UIView!, #IBOutlet weak var label: UILabel!,#IBOutlet weak var image: UIImageView!,#IBOutlet weak var loginBtn: UIButton!
Connect view from storyboard to your VC class.
And then paste the given code in your ViewController and call playVideo() function in viewDidLoad().
func playVideo(){
guard let path = Bundle.main.path(forResource: "intro", ofType: "mp4") else {
return
}
let player = AVPlayer(url: URL(fileURLWithPath: path))
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
playerLayer.videoGravity = .resizeAspectFill
self.videoLayer.layer.addSublayer(playerLayer)
player.play()
videoLayer.bringSubviewToFront(image)
videoLayer.bringSubviewToFront(label)
videoLayer.bringSubviewToFront(loginBtn)
videoLayer.bringSubviewToFront(signupBtn)
}
If url is nil and the video is being successfully copied into the bundle, the likely problem is case sensitivity. Make sure the filename is using the correct case. The iOS device filesystem is case sensitive, where as the iPhone simulator is not.
I had a same problem when I tried to play video background. I think it is the problem of video resource.
I resolved the problem like this way.
When you drag your video, you should check both of 'Copy items if needed' and 'Add to targets'.
Or you can just right click on the project navigator, click 'Add' to add resource.

I can't load video file from local server in swift

I'm working with iOS using Swift.
I want to load a video file using AVPlayer in Swift.
I was able to load an image and a text file from the local server. But I couldn't load a video file. This is the code that I'm using.
let movieUrl = NSURL(string: "http://akjfsjf/ajdfl/ajkf/car.mp4")
let player = AVPlayer(URL: movieUrl!)
let playerController = AVPlayerViewController()
playerController.player = player
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
player.play()
I have tried with following URL and function
func playvideo (){
let movieUrl = NSURL(string: "http://www.w3schools.com/html/movie.mp4")
let player = AVPlayer(URL: movieUrl!)
let playerController = AVPlayerViewController()
playerController.player = player
player.play()
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
}
Also from your code i found you are using http rather than https so you need to add following property in plist file.
Try this one,
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
var playerVC : AVPlayerViewController!
var playerItem : AVPlayerItem!
var player : AVPlayer!
var playerLayer: AVPlayerLayer!
override func viewDidAppear(animated: Bool) {
let url = NSURL.init(fileURLWithPath: "your file path")
self.playerItem = AVPlayerItem.init(URL: url!)
self.player = AVPlayer.init(playerItem: self.playerItem)
self.playerVC = AVPlayerViewController.init();
self.playerVC.player = self.player;
self.player.currentItem!.playbackLikelyToKeepUp
self.presentViewController(self.playerVC, animated: true) { () -> Void in
self.playerVC.player?.play()
}
}
}
Hope this helps!

Resources