How to unmute sounds after starting the app? - ios

I've got some problems with video on my app's background.
Here is my program code:
import UIKit
import AVKit
import AVFoundation
class IntroViewController: UIViewController {
var player: AVPlayer?
override func viewDidLoad() {
super.viewDidLoad()
let videoURL: NSURL = NSBundle.mainBundle().URLForResource("intro", withExtension: "mov")!
player = AVPlayer(URL: videoURL)
player?.actionAtItemEnd = .None
player?.muted = false
let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
playerLayer.zPosition = -1
playerLayer.frame = view.frame
view.layer.addSublayer(playerLayer)
player?.play()
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "loopVideo",
name: AVPlayerItemDidPlayToEndTimeNotification,
object: nil)
}
func loopVideo() {
player?.seekToTime(kCMTimeZero)
player?.play()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
After launching my program, the Music app is muted. How do I fix it?
And one more question: after returning to this screen from home screen, without closing the app, video stops. Where is the problem?

Related

IOS 14 AVPlayer plays on simulator but doesn't on a real device

I have splash screen as a short mov video for my application and I use AVPlayer (code below) for that. It stopped working with the IOS 14 physical device, while on the simulator it works fine. Could you help with that?
import UIKit
import AVKit
import AVFoundation
class PlayerVC: UIViewController, AVAudioPlayerDelegate {
lazy var player: AVPlayer = {
let fileName = "test"
let path = Bundle.main.path(forResource: fileName, ofType: "mov")!
let videoURL = URL(fileURLWithPath: path)
let player = AVPlayer(url: videoURL)
return player
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(viewTapped)))
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.shouldRasterize = true
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
NotificationCenter.default.addObserver(self, selector: #selector(self.playerDidFinishPlaying(sender:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)
}
#objc func playerDidFinishPlaying(sender: NSNotification) {
loadRootVC()
}
private func loadRootVC() {
dismiss(animated: false, completion: nil)
self.onVideoFinished();
}
#objc func viewTapped() {
loadRootVC()
}
deinit {
NotificationCenter.default.removeObserver(self)
}
}

Autoplaying an MP4 video in a UIView that is on loop like a GIF?

Once users go to this specific page, I want the video to immediately start playing inside of a UIView with no controls. It will just loop like a gif. This is code I found off of github and it doesn't seem to work. It just shows the white UIView when I pull up the page. My file name is correct as well.
import Foundation
import UIKit
import AVKit
import AVFoundation
class RampWallPush: UIViewController {
#IBOutlet weak var videoView: UIView!
var player: AVPlayer?
override func viewDidLoad() {
super.viewDidLoad()
// Load video resource
if let videoUrl = Bundle.main.url(forResource: "small", withExtension: "mp4") {
// Init video
self.player = AVPlayer(url: videoUrl)
self.player?.isMuted = true
self.player?.actionAtItemEnd = .none
// Add player layer
let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
playerLayer.frame = view.frame
// Add video layer
self.videoView.layer.addSublayer(playerLayer)
// Play video
self.player?.play()
// Observe end
NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidReachEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem)
}
}
// MARK: - Loop video when ended.
#objc func playerItemDidReachEnd(notification: NSNotification) {
self.player?.seek(to: CMTime.zero)
self.player?.play()
}
}
Your code is works you need to verify the file exists in your project and .......
// Load video resource
if let videoUrl = Bundle.main.url(forResource: "sample", withExtension: "mp4") {
// exists
}
else {
print("NoFile")
}
So select the file and
You need to change the frame
playerLayer.frame = CGRect(x:0,y:0,width:200,height:300)
self.videoView.layer.addSublayer(playerLayer)
playerLayer.center = videoView.center
Created a demo Here

AvPlayer is unable to play sound in device

My code perfectly works fine in the simulator and it streams the url without any issue. But when plugged to the actual device it doesnt play the audio. My code as bellow
var playerItem: AVPlayerItem?
var player: AVPlayer?
func initPlayer() {
let url:URL = URL(string:"https://storage.googleapis.com/purelightdatabucket/samples%2F60-Energy%20UPLIFT.mp3")!
self.playerItem = AVPlayerItem(url: url)
self.player=AVPlayer(playerItem: self.playerItem!)
let playerLayer=AVPlayerLayer(player: self.player!)
playerLayer.frame = CGRect(x: 0, y: 0, width: 10, height: 50) // actually this player layer is not visible
self.view.layer.addSublayer(playerLayer)
}
Put this code in didFinishLaunchingWithOptions
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print(error)
}
you can use this too :
You need to add AVKit & AVFoundation to your frameworks path and import them :
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
var player = AVPlayer()
override func viewDidLoad() {
super.viewDidLoad()
let playerItem = AVPlayerItem(url: URL(string: "https://storage.googleapis.com/purelightdatabucket/samples%2F60-Energy%20UPLIFT.mp3")!)
player = AVPlayer(playerItem: playerItem)
player.play()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

AVPlayerViewController play/pause issue in iOS

I am working on the iOS application. This application base on the NEWS media.
I am playing the small video of highlighted News. I am facing a stranger problem with AVPlayerViewController.
When i play the video everything is fine. When i go to offline the buffer video play perfect & after the play buffer video, the video stops but the progress bar(Slider) continuously in play state while the video has been stopped.
For more clearance please watch this GIF image :
Please visit this link for batter understanding.
This is my code for player.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.playVideo()
}
func playVideo(){
let videoURL = NSURL(string: "http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8")
let player = AVPlayer(URL: videoURL!)
let playerController = AVPlayerViewController()
playerController.player = player
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
player.play()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I've tried your code and I couldn't even see picture from your gif.
I've also tried your code with another video link (https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4) and problem wasn't reproduced.
I guess your problem is in the way you try to show your AVPlayerViewController.
Consider next snippet:
let videoURL = NSURL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(URL: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
playerViewController.player!.play()
}
This should work. If you need some ui customization you shouldn't use AVPlayerViewController I guess.
UPDATE:
Try adding Reachaility support:
class MyViewController: UIViewController {
var reachability: Reachability?
override func viewDidLoad() {
super.viewDidLoad()
reachability = try? Reachability.reachabilityForInternetConnection()
reachability?.whenUnreachable = { _ in
player.pause()
}
try? reachability?.startNotifier()

AVPlayer playback with Swift 2.0

I'm having issues with playing back a video. All I want is for the video to playback after the launch image has been displayed. I followed this (https://www.youtube.com/watch?v=iYATp9--VIM) tutorial but when I simulated I received a blank screen.
Here is the code:
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
#IBOutlet var videoView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
self.setupView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setupView() {
let path = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("streetview", ofType: "mov")!)
let player = AVPlayer(URL:path)
let newLayer = AVPlayerLayer(player: player)
newLayer.frame = self.videoView.frame
self.videoView.layer.addSublayer(newLayer)
newLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
player.play()
}
}
Please help me if you can!
If you're just getting a blank black screen, it would generally point to an issue with the source of the video. I'd check that you're spelling the name of your file correctly and that it definitely is a .mov file.
I use this code to launch the AVPlayer and play my video. It plays a video from a URL from the internet. I have it inside my button function so it happens when the user presses the play button I have:
let videoURL = NSURL(string: "YOUR URL") {
let player = AVPlayer(URL: videoURL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
playerViewController.player!.play()
}
I can confirm this works for me in Swift 2.0

Resources