Iterating Videos - swift

I am trying to get the player to iterate through an array of strings that reference the URL of videos. I can only get it to play one of the videos in the array, if I hardcode it in the code, but I cannot do it to iterate.
I have gotten to play one video while it is hard coded in but not iterate.
var playerArray = [AVQueuePlayer]()
var player = AVQueuePlayer()
// var URLSArray = [URL]()
var ItemArray = [AVPlayerItem]()
var videos = ["video1", "video2","video3"]
var playerController = AVPlayerViewController()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let videoNum = (self.videos.count-1)
// var URLSArray = [URL]()
// for n in 0...videoNum{
// let pathString = (Bundle.main.path(forResource: videos[n], ofType: "mov"))
// let StrToURL = NSURL(string: pathString!)
// URLSArray.append(StrToURL! as URL)
// }
var ItemArray = [AVPlayerItem]()
// var playerController = AVPlayerViewController()
for n in 0...videoNum{
let pathString = (Bundle.main.path(forResource: videos[n], ofType: "mov"))
//print(pathString!)
let StrToURL = URL(string: pathString!)
// print(StrToURL!)
// ItemArray = [AVPlayerItem(url: StrToURL!)]
ItemArray.append(AVPlayerItem(url:StrToURL!))
print(n)
print(ItemArray)
// let player = AVPlayer(playerItem: ItemArray[0])
player = AVQueuePlayer(items: [ItemArray[n]])
player.play()
}
// playerController.player = player
// present(playerController, animated: true) {
// player.play()
}
I want it to finish playing one video and move on to the next video in the array, how ever I hard code the video from the array it works but if I am trying to iterate it acts as if the URL isn't working. This is what it says 2019-06-14 13:08:44.718684-0400 Video[5439:1288491] NSURLConnection finished with error - code -1002

Declare only the player
var player = AVQueuePlayer()
In viewDidAppear map the string array to the URLs in the application bundle and then the URLs to the player items. Then create the queued player and start playing.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let videos = ["video1", "video2", "video3"]
let videoURLs = videos.compactMap{ Bundle.main.url(forResource: $0, withExtension: "mov")}
let itemArray = videoURLs.map{ AVPlayerItem(url: $0) }
player = AVQueuePlayer(items: itemArray)
player.play()
// playerController.player = player
// present(playerController, animated: true) {
// player.play()
// }
}

Related

How to play multiple Audio Files simultaneously using AVPlayer?

I am trying to play multiple audio files using 2 AVPlayer instances, but one of the player stops for a fraction of a second rather than playing all audio files simultaneously.
The logic of the program is as follows:
var player: AVPlayer? will stream an audio file from my database. On its own is playing perfectly.
fileprivate var countPlayer: AVPlayer? plays the count number of the current item being played by var player. The count is a sequence of 1 to 8 and for each digit I am storing/sandobxing a .wav file locally such as 1.wav, 2.wav...8.wav.
When current time of var player is at a certain time, countPlayer is triggered and it plays one of the local file 1.wav, 2.wav..etc.
The problem is that when the var countPlayer starts playing, it causes the background AVPlayer, namely var player to stop for a fraction of a second, similar to what's described in this comment:
Play multiple Audio Files with AVPlayer
var player: AVPlayer? //plays the song
fileprivate var countPlayer: AVPlayer? // plays the count number of song
private func addBoundaryTimeObserver(tableIndexPath: IndexPath) {
let mediaItem = mediaArray[tableIndexPath.row]
guard let url = URL(string: mediaItem.mediaAudioUrlStringRepresentation ?? "") else {return}
let playerItem = AVPlayerItem(url: url)
player = AVPlayer(playerItem: playerItem)
var timesToTransverse = [NSValue]()
//convert string representation of times elements to array
let timesRecorded: [String] = mediaItem.timesRecorded.components(separatedBy: ",")
// Build boundary times from arrayOfBeats keys
let timeDoubles: [Double] = timesRecorded.compactMap {timeString in
if let second = Double("\(timeString)") {
return second
}
return nil
}
guard timeDoubles.count > 0 else {return} //unexpected
timesToTransverse = timeDoubles.map { second in
let cmtime = CMTime(seconds: second, preferredTimescale: CMTimeScale(NSEC_PER_SEC))
return NSValue(time: cmtime)
}
guard timesToTransverse.count != 0 else {return}
guard let playerCell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as? PlayerCell else {return}
startTime = Date().timeIntervalSinceReferenceDate
timeIndex = 0
player?.play()
player?.rate = Float(initialPlaybackRate)
// find the index of time
//reset timeObserverToken
// call a function with the new times sorted
// Queue on which to invoke the callback
let mainQueue = DispatchQueue.main
// Add time observer
timeObserverToken =
player?.addBoundaryTimeObserver(forTimes: timesToTransverse, queue: mainQueue) {
[weak self] in
//because there are no time signature changes, we can simply increment timeIndex with + 1 every time `addBoundaryTimeObserver` completion handler is called and subscript timesToTransverse with timeIndex in order to get the subsequent timeInSeconds
guard let strongSelf = self, strongSelf.timeIndex < timesToTransverse.count else {return}
let timeElement = timesToTransverse[strongSelf.timeIndex]
strongSelf.timeInSeconds = CMTimeGetSeconds(timeElement.timeValue)
//show progress in progressView
let duration = CMTimeGetSeconds(playerItem.duration)
let cmtimeSeconds = CMTime(seconds: strongSelf.timeInSeconds, preferredTimescale: CMTimeScale(NSEC_PER_SEC))
//Total time since timer started, in seconds
strongSelf.timeInSeconds = Date().timeIntervalSinceReferenceDate - strongSelf.startTime
let timeString = String(format: "%.2f", strongSelf.timeInSeconds)
strongSelf.timeString = timeString
//use reminder operator to determine the beat count
let beat = (strongSelf.timeIndex + 1) % 8 == 0 ? 8 : ((strongSelf.timeIndex + 1) % 8)
//play the beat count : 1, 2, ...8
self.preapareToPlayAudio(beatCount: beat)
/*
0: (0 + 1) % 8 = 1
1: (1 + 1) % 8 = 2
6: (6 + 1) % 8 = 7
7: (7 + 1) % 8 = 0
*/
strongSelf.timeIndex += 1
}
}//end addBoundaryTimeObserver
//prepare determine what wav file to play
private func preapareToPlayAudio(beatCount: Int) {
switch beatCount {
case 1:
guard let url = Bundle.main.url(forResource: "1", withExtension: "wav") else {return}
playWith(beatCountURL: url)
//7 more cases go here .....
default: print("unexpected case here")
}
}//end play(beatCount: Int)
private func playWith(beatCountURL: URL) {
let playerItem = AVPlayerItem(url: beatCountURL)
countPlayer = AVPlayer(playerItem: playerItem)
countPlayer?.play()
}
You would be better off using AVAudioPlayerNode, AVAudioMixerNode, AVAudioEngine. Using these classes you won't have problems like you have right now. It's also not that difficult to set up.
You can check out my gist, in order to play the sounds in your Playgrounds you would need to put audio files into Resources folder in Project Navigator:
https://gist.github.com/standinga/24342d23acfe70dc08cbcc994895f32b
The code works without stopping background audio when top sounds are triggered.
Here's also the same code:
import AVFoundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
class AudioPlayer {
var backgroundAudioFile:AVAudioFile
var topAudioFiles: [AVAudioFile] = []
var engine:AVAudioEngine
var backgroundAudioNode: AVAudioPlayerNode
var topAudioAudioNodes = [AVAudioPlayerNode]()
var mixer: AVAudioMixerNode
var timer: Timer!
var urls: [URL] = []
init (_ url: URL, urls: [URL] = []) {
backgroundAudioFile = try! AVAudioFile(forReading: url)
topAudioFiles = urls.map { try! AVAudioFile(forReading: $0) }
engine = AVAudioEngine()
mixer = AVAudioMixerNode()
engine.attach(mixer)
engine.connect(mixer, to: engine.outputNode, format: nil)
self.urls = urls
backgroundAudioNode = AVAudioPlayerNode()
for _ in topAudioFiles {
topAudioAudioNodes += [AVAudioPlayerNode()]
}
}
func start() {
engine.attach(backgroundAudioNode)
engine.connect(backgroundAudioNode, to: mixer, format: nil)
backgroundAudioNode.scheduleFile(backgroundAudioFile, at: nil, completionHandler: nil)
try! engine.start()
backgroundAudioNode.play()
for node in topAudioAudioNodes {
engine.attach(node)
engine.connect(node, to: mixer, format: nil)
try! engine.start()
}
// simulate rescheduling files played on top of background audio
DispatchQueue.global().async { [unowned self] in
for i in 0..<1000 {
sleep(2)
let index = i % self.topAudioAudioNodes.count
let node = self.topAudioAudioNodes[index]
node.scheduleFile(self.topAudioFiles[index], at: nil, completionHandler: nil)
node.play()
}
}
}
}
let bundle = Bundle.main
let beepLow = bundle.url(forResource: "beeplow", withExtension: "wav")!
let beepMid = bundle.url(forResource: "beepmid", withExtension: "wav")!
let backgroundAudio = bundle.url(forResource: "backgroundAudio", withExtension: "wav")!
let audioPlayer = AudioPlayer(backgroundAudio, urls: [beepLow, beepMid])
audioPlayer.start()

Play video from Download URL

I have a URL :- "http://fitnation.theclientdemos.com:9000/media/uploads/videoplayback_3_JtVCHi1"
When I run this URL on browser, My VDO starts downloads.
Please help to play this video in a view (let view name is:- vdoView)
For this I am trying below code:-
import UIKit
import AVKit
import AVFoundation
class VideoViewController: UIViewController {
#IBOutlet weak var vdoView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
getVideo()
}
func getVideo(){
let videoURL = URL(string: "http://fitnation.theclientdemos.com:9000/media/uploads/videoplayback_3_JtVCHi1")
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.vdoView.bounds
self.vdoView.layer.addSublayer(playerLayer)
player.play()
}
First you have to import AVKit, import AVFoundation
Using AVPlayer
if let url = URL(string: "http://fitnation.theclientdemos.com:9000/media/uploads/videoplayback_3_JtVCHi1"){
let player = AVPlayer(url: url)
let controller=AVPlayerViewController()
controller.player=player
controller.view.frame = self.view.frame
self.view.addSubview(controller.view)
self.addChildViewController(controller)
player.play()
}
It's better to put this code into the method: override func viewDidAppear(_ animated: Bool) or somewhere after.
override func viewDidLoad()
{
let videoURL = NSURL(string: "http://fitnation.theclientdemos.com:9000/media/uploads/videoplayback_3_JtVCHi1")
let player = AVPlayer(url: videoURL! as URL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
}
You should try it like this
func getVideo(){
let videoURL = URL(string: "http://fitnation.theclientdemos.com:9000/media/uploads/videoplayback_3_JtVCHi1")
// Create an AVAsset
let videoAsset = AVAsset(url: videoURL!)
// Create an AVPlayerItem with asset
let videoPlayerItem = AVPlayerItem(asset: videoAsset)
// Initialize player with the AVPlayerItem instance.
let player = AVPlayer(playerItem: videoPlayerItem)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.vdoView.bounds
self.vdoView.layer.addSublayer(playerLayer)
player.play()
}
And you should update your plist file to allow the contents from http: Refer here
*All you need is change your code to this:
import AVFoundation
class your class name
var player: AVPlayer?
func playAudio() {
guard let url = URL.init(string: "http://192.168.100.184:9050/uploads/sound/file/1/a1b19343-f785-4d48-b7d5-1abe26c03ff3.mp3" ) else { return }
player = AVPlayer.init(url: url)
}
and at the end call the func in IBAction body*
just like the code below:
#IBAction play (sender: Any) {
playAudio()
}

for loop in swift step by step?

I have the following array with urls:
let KStorePlayURL = [
https://source.s3-us-west-2.amazonaws.com/ENVOI/2018/07/19/ATASTEOFDANCE_S1_EP3.mp4,
https://source.s3-us-west-2.amazonaws.com/ENVOI/2018/05/23/ATasteOfDance_S1E1_Episode.mp4,
https://source.s3-us-west-2.amazonaws.com/ENVOI/2018/05/23/ATasteOfDance_S1E1_Episode.mp4,
https://source.s3-us-west-2.amazonaws.com/ENVOI/2018/05/23/ATasteOfDance_S1E1_Episode.mp4
]
How can I get the URLs one after the other in the for loop? Also when one video is played and followed by other videos in this Bitmovin player?
Here is the code I have already tried:
#objc func setUpPlayerVideos1() {
print(KStorePlayURL)
for i in 0..<KStorePlayURL.count {
let streamURL = URL(string: KStorePlayURL[i])
playlist.append(PlaylistItem(url: streamURL!, title: "player"))
// Create player based with a default configuration
let player = BitmovinPlayer()
// Create player view and pass the player instance to it
let playerView = BMPBitmovinPlayerView(player: player, frame: .zero)
// Listen to player events
player.add(listener: self)
playerView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
playerView.frame = view.bounds
view.addSubview(playerView)
view.bringSubview(toFront: playerView)
// store the reference to the player
self.Bitmovinplayer = player
}
// Start the playlist
playNextItem()
}
You shouldn't be using a for-loop, because you are going to be overriding the player in each iteration. What you need to do is fill the playlistusing a for-loop or a more functional style like this :
KStorePlayURL.forEach { urlString in
//Make sure that the url address is correct
guard let streamURL = URL(string: urlString) else {
fatalError("Error in stream url")
}
playlist.append(PlaylistItem(url: streamURL!, title: "player"))
}
Here I am supposing that KStorePlayURL is an array of strings:
let KStorePlayURL = [
"https://source.s3-us-west-2.amazonaws.com/ENVOI/2018/07/19/ATASTEOFDANCE_S1_EP3.mp4",
"https://source.s3-us-west-2.amazonaws.com/ENVOI/2018/05/23/ATasteOfDance_S1E1_Episode.mp4",
"https://source.s3-us-west-2.amazonaws.com/ENVOI/2018/05/23/ATasteOfDance_S1E1_Episode.mp4",
"https://source.s3-us-west-2.amazonaws.com/ENVOI/2018/05/23/ATasteOfDance_S1E1_Episode.mp4"
]
Your final code should look like this:
#objc func setUpPlayerVideos1() {
KStorePlayURL.forEach { urlString in
guard let streamURL = URL(string: urlString) else {
fatalError("Error in stream url")
}
playlist.append(PlaylistItem(url: streamURL!, title: "player"))
}
let player = BitmovinPlayer()
// Create player view and pass the player instance to it
let playerView = BMPBitmovinPlayerView(player: player, frame: .zero)
// Listen to player events
player.add(listener: self)
playerView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
playerView.frame = view.bounds
view.addSubview(playerView)
view.bringSubview(toFront: playerView)
// store the reference to the player
self.Bitmovinplayer = player
}
// Start the playlist
playNextItem()
}
You can find a complete sample code for playing a playlist with the bitmovin player here.

AVPlayer not working properly - Swift

//Firstly my English very bad, I'm sorry...
I'm implementing AVPlayer my app. Player is working fine but if I play another videos many times Player is not working and only just this screen looks.
Here's my code
let player:AVPlayer?
override func viewDidLoad() {
super.viewDidLoad()
let videoFile = object?.objectForKey("video") as? PFFile
videoFile?.getFilePathInBackgroundWithBlock({ (filePath, error) in
if error == nil {
let videoURL = NSURL(fileURLWithPath: filePath!)
self.player = AVPlayer(URL: videoURL)
let playerController = AVPlayerViewController()
playerController.player = self.player
playerController.showsPlaybackControls = false
playerController.videoGravity = AVLayerVideoGravityResizeAspectFill
playerController.view.frame = self.videoView.bounds
self.videoView.addSubview(playerController.view)
self.addChildViewController(playerController)
self.player!.play()
} else {
print(error)
}
})
}
I don't understand the way you fetch the video object but you can try the following code. It works fine.
var player:AVPlayer! // this shouldn't be let.
override func viewDidLoad() {
super.viewDidLoad()
let query = PFQuery(className:"TestClass") //your classname in parse. Change it with your clas name
query.getObjectInBackgroundWithId("SALe4gX2nk") { // get the video file with id. Change it with your id.
(object: PFObject?, error: NSError?) -> Void in
let videoFile = object?.objectForKey("video") as? PFFile
let url = NSURL(string: (videoFile?.url)!)
self.player = AVPlayer(URL: url!)
let playerController = AVPlayerViewController()
playerController.player = self.player
playerController.showsPlaybackControls = false
playerController.videoGravity = AVLayerVideoGravityResizeAspectFill
playerController.view.frame = self.view.bounds
self.view.addSubview(playerController.view)
self.addChildViewController(playerController)
self.player!.play()
}
}

NSNotificationCenter to AVQueuePlayer in swift

I added list of AVQueuePlayer to UITableViewCell. I added array of videos to AVQueuePlayer as follows:
if let urls = delegate.arrayVideofeedItemsList[indexPath.row] as? NSArray
{
let player = AVQueuePlayer()
for currentVideoObject in urls {
let avAsset = AVURLAsset(URL: NSURL(fileURLWithPath: "\(currentVideoObject)"))
avAsset.loadValuesAsynchronouslyForKeys(["playable", "tracks", "duration"], completionHandler: {
dispatch_async(dispatch_get_main_queue(), {
// self.enqueue(avAsset)
let item = AVPlayerItem(asset: avAsset)
player.insertItem(item, afterItem: nil)
})
})
}
let playerLayer = AVPlayerLayer(player: player)
// playerLayer.backgroundColor = UIColor.greenColor().CGColor
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
playerLayer.frame = CGRectMake(0, 0,SCREEN_WIDTH, 250)
cell.contentView.layer.addSublayer(playerLayer)
player.play()
player.actionAtItemEnd = AVPlayerActionAtItemEnd.Advance
player.volume = 0.0
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerItemDidReachEnd:", name: AVPlayerItemDidPlayToEndTimeNotification,object:player.currentItem)
}
func playerItemDidReachEnd(notification: NSNotification) {
let p: AVPlayerItem = notification.object as! AVPlayerItem
p.seekToTime(kCMTimeZero)
}
I have three videos in AVQueuePlayer. Three videos are played sequentially but After completion of this first video is not playing. What I need to add to this ? Please help me to complete it.
Try to add this line after initializing player:
player.actionAtItemEnd = AVPlayerActionAtItemEnd.None