play sequential audio files swift - swift

hi im trying to play several audio files one after the other.
here's the code I have to play 1 audio file names "1". can anyone help us play a group of audio files sequentiall?
import UIKit
import AVFoundation
class ViewController: UIViewController {
var ding:AVAudioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
prepareAudios()
ding.play()
}
func prepareAudios() {
var path = NSBundle.mainBundle().pathForResource("1", ofType: "mp3")
ding = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path!), error: nil)
ding.prepareToPlay()
}
}

You can use AVQueuePlayer to play multiple audio sequentially.
class ViewController: UIViewController {
var playerQueue: AVQueuePlayer = {
let url1 = NSBundle.mainBundle().URLForResource("UpTown", withExtension: "mp3")!
let url2 = NSBundle.mainBundle().URLForResource("TurnMeOn", withExtension: "mp3")!
let item1 = AVPlayerItem(URL: url1)
let item2 = AVPlayerItem(URL: url2)
let queue = AVQueuePlayer(items: [item1, item2])
return queue
}()
override func viewDidLoad() {
super.viewDidLoad()
playerQueue.play()
}
}

Related

How to play several sound in a row

The goal is to play several sounds one after another (getReady -> nextExercise -> burpees).
The problem is that only the first one is being played
How it should work:
I call playGetReady() from WorkoutTabataViewController
I plays the first sound
After the first sound is finished, automatically "audioPlayerDidFinishPlaying()" is being called
It triggers "playNextSound()" func, which playing next sound
But audioPlayerDidFinishPlaying() is not being called. Or am I missing something and it should work differently?
class AudioPlayerManager: AVAudioPlayerDelegate {
var description: String
static let shared = AudioPlayerManager()
var audioPlayer: AVAudioPlayer?
var workoutVC: WorkoutTabataViewController?
var mainVC: MainTabataViewController?
var currentSound = 0
let urls: [URL]
init() {
self.description = ""
//First sound
let getReady = Bundle.main.path(forResource: "Get ready", ofType: "mp3")!
let urlGetReady = URL(fileURLWithPath: getReady)
//Second sound
let nextExercise = Bundle.main.path(forResource: "Next Exercise", ofType: "mp3")!
let urlNextExercise = URL(fileURLWithPath: nextExercise)
//Third sound
let burpees = Bundle.main.path(forResource: "Burpees", ofType: "mp3")!
let urlBurpees = URL(fileURLWithPath: burpees)
urls = [urlGetReady, urlNextExercise, urlBurpees]
}
func playGetReady() {
do {
audioPlayer = try AVAudioPlayer(contentsOf: urls[currentSound])
audioPlayer?.delegate = self
audioPlayer?.play()
} catch {
print(error)
}
}
func playNextSound() {
currentSound += 1
if currentSound < urls.count {
do {
audioPlayer = try AVAudioPlayer(contentsOf: urls[currentSound])
audioPlayer?.delegate = self
audioPlayer?.play()
} catch {
print(error)
}
}
}
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
if flag {
playNextSound()
}
}
}
Your audio manager class is not introspectable. Say #objc func audioPlayerDidFinishPlaying or, better, make it an NSObject.

how to have separate videos that play with separate buttons

can anyone please help me, I am trying to add separate buttons to play separate videos in the same view controller but I don't know how to.
this is my code, how do I do this?
import UIKit
import AVKit
class ViewController: UIViewController {
#IBAction func Town(_ sender: Any) {
if let path = Bundle.main.path(forResource: "grey", ofType: "mov") {
let video = AVPlayer(url: URL(fileURLWithPath: path))
let videoPlayer = AVPlayerViewController()
videoPlayer.player = video
self.present(videoPlayer, animated: true, completion: {
video.play()
})
}
func viewDidLoad() {
super.viewDidLoad()
}
}
}
To play 2 videos simultaneously in same viewController, you need to create 2 separate views and 2 respective buttons in your Storyboard.
Remaining functionality will go inside your IBActions.
Please use following code:
class VideoPlaybackViewController: UIViewController {
#IBOutlet weak var videoView1: UIView!
#IBOutlet weak var videoView2: UIView!
#IBAction func playFirstVideo(_ sender: Any) {
guard let path = Bundle.main.path(forResource: "640", ofType: "mov") else {
print("Video Source Not Found")
return
}
playVideo(playbackURL: URL(fileURLWithPath: path), playerView: videoView1)
}
#IBAction func playSecondVideo(_ sender: Any) {
guard let path = Bundle.main.path(forResource: "720", ofType: "mov") else {
print("Video Source Not Found")
return
}
playVideo(playbackURL: URL(fileURLWithPath: path), playerView: videoView2)
}
func playVideo(playbackURL: URL, playerView: UIView) {
let player = AVPlayer(url: playbackURL)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = playerView.bounds
playerView.layer.addSublayer(playerLayer)
player.play()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
Please see the screen with this implementation
I used this code and it worked perfectly
import UIKit
import AVKit
class ViewController: UIViewController {
#IBOutlet weak var videoView1: UIButton!
#IBOutlet weak var videoView2: UIButton!
#IBAction func playFirstVideo(_ sender: Any) {
if let path = Bundle.main.path(forResource: "grey", ofType: "mov") {
let video = AVPlayer(url: URL(fileURLWithPath: path))
let videoPlayer = AVPlayerViewController()
videoPlayer.player = video
self.present(videoPlayer, animated: true, completion: {
video.play()
})
}
}
#IBAction func playSecondVideo(_ sender: Any) {
if let path = Bundle.main.path(forResource: "go", ofType: "mov") {
let video = AVPlayer(url: URL(fileURLWithPath: path))
let videoPlayer = AVPlayerViewController()
videoPlayer.player = video
self.present(videoPlayer, animated: true, completion: {
video.play()
})
}
}
}

"advanceToNextItem()" doesn't allow to loop through ALL videos

I've got auto-playing sequence of video clips with looping behaviour built on KVO. But, in addition to auto-playback, I'd like to advance all the clips using NSButton. So, if I press the Next Video button when my sequence is auto-playing, this action advances me to the next clip in a queue. But it doesn't work a proper way.
The problem is: when the queue comes to end, there's no loop for all the clips in the queue. There's a loop for the first clip only (if I pressed the Next Video button 10 times – only first clip loops 10 times), after that all clips are auto-playing normally.
Question: How to (when advancing) loop through all videos using NSButton?
Here's my code:
import Cocoa
import AVFoundation
import AVKit
class ViewController: NSViewController {
#IBOutlet weak var avPlayerView: AVPlayerView!
#IBOutlet weak var nextVideoButton: NSButton!
#objc private let player = AVQueuePlayer()
let clips = ["One", "Two", "Three", "Four"]
private var token: NSKeyValueObservation?
override func viewDidAppear() {
super.viewDidAppear()
addAllVideosToPlayer()
}
func addAllVideosToPlayer() {
avPlayerView.player = player
for clip in clips {
let urlPath = Bundle.main.path(forResource: clip, ofType: "mov")!
let url = URL(fileURLWithPath: urlPath)
let playerItem = AVPlayerItem(url: url)
player.insert(playerItem, after: player.items().last)
token = player.observe(\.currentItem) { [weak self] player, _ in
if self!.player.items().count == 1 { self?.addAllVideosToPlayer() }
}
player.play()
avPlayerView.controlsStyle = .none
}
}
#IBAction func nextVideo(_ sender: Any) {
var number: Int = 0
let clip = clips[number]
let urlPath = Bundle.main.path(forResource: clip, ofType: "mov")!
let url = URL(fileURLWithPath: urlPath)
let playerItem = AVPlayerItem(url: url)
player.insert(playerItem, after: player.items().last)
player.advanceToNextItem()
number += 1
if number >= clips.count {
number = 0
player.advanceToNextItem()
number += 1
}
}
}
Here's my ViewController:
You always access the first video here:
var number: Int = 0
let clip = clips[number]
number is always zero. Keep it as a member of the class and update it where needed.
Here's the solution of my problem (thanks to #giorashc) :
class ViewController: NSViewController {
var number: Int = 0 // class member
.........................................
.........................................
#IBAction func nextVideo(_ sender: Any) {
let clip = clips[number % 4] // modulus of 4
let urlPath = Bundle.main.path(forResource: clip, ofType: "mov")!
let url = URL(fileURLWithPath: urlPath)
let playerItem = AVPlayerItem(url: url)
player.insert(playerItem, after: player.items().last)
player.advanceToNextItem()
number += 1
}
}

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()
}

Mac - Swift 3 - queuing audio files and playing

I would like to write an app in swift 3 in order to play queued audio files without any gap, crack or noise when passing from one to another.
My first try was using AvAudioPlayer and AvAudioDelegate (AVAudioPlayer using array to queue audio files - Swift), but I don't know how to preload the next song to avoid gap. Even if I know how to do it, I am not certain it is the best way to achieve my goal.
AVQueuePlayer seems to be a better candidate for the job, it is made for that purpose, but I don't find any example to help me out.
Maybe it is only a problem of preloading or buffering? I am a bit lost in this ocean of possibilities.
Any suggestion is welcomed.
It is far to be perfect, specially if you want to do it twice or more ("file exist" error), but it can serve as a base.
What it does is taking two files (mines are aif samples of ap. 4 sec.), encode them in one file and play the resulting files. If you have hundreds of them, assembled aleatory or not, it can make great fun.
All credits for the mergeAudioFiles function goes to #Peyman and #Pigeon_39. Concatenate two audio files in Swift and play them
Swift 3
import Cocoa
import AVFoundation
var action = AVAudioPlayer()
let path = Bundle.main.path(forResource: "audiofile1.aif", ofType:nil)!
let url = URL(fileURLWithPath: path)
let path2 = Bundle.main.path(forResource: "audiofile2.aif", ofType:nil)!
let url2 = URL(fileURLWithPath: path2)
let array1 = NSMutableArray(array: [url, url2])
class ViewController: NSViewController, AVAudioPlayerDelegate
{
#IBOutlet weak var LanceStop: NSButton!
override func viewDidLoad()
{
super.viewDidLoad()
}
override var representedObject: Any?
{
didSet
{
// Update the view, if already loaded.
}
}
#IBAction func Lancer(_ sender: NSButton)
{
mergeAudioFiles(audioFileUrls: array1)
let url3 = NSURL(string: "/Users/ADDUSERNAMEHERE/Documents/FinalAudio.m4a")
do
{
action = try AVAudioPlayer(contentsOf: url3 as! URL)
action.delegate = self
action.numberOfLoops = 0
action.prepareToPlay()
action.volume = 1
action.play()
}
catch{print("error")}
}
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool)
{
if flag == true
{
}
}
var mergeAudioURL = NSURL()
func mergeAudioFiles(audioFileUrls: NSArray) {
//audioFileUrls.adding(url)
//audioFileUrls.adding(url2)
let composition = AVMutableComposition()
for i in 0 ..< audioFileUrls.count {
let compositionAudioTrack :AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())
let asset = AVURLAsset(url: (audioFileUrls[i] as! NSURL) as URL)
let track = asset.tracks(withMediaType: AVMediaTypeAudio)[0]
let timeRange = CMTimeRange(start: CMTimeMake(0, 600), duration: track.timeRange.duration)
try! compositionAudioTrack.insertTimeRange(timeRange, of: track, at: composition.duration)
}
let documentDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! as NSURL
self.mergeAudioURL = documentDirectoryURL.appendingPathComponent("FinalAudio.m4a")! as URL as NSURL
let assetExport = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)
assetExport?.outputFileType = AVFileTypeAppleM4A
assetExport?.outputURL = mergeAudioURL as URL
assetExport?.exportAsynchronously(completionHandler:
{
switch assetExport!.status
{
case AVAssetExportSessionStatus.failed:
print("failed \(assetExport?.error)")
case AVAssetExportSessionStatus.cancelled:
print("cancelled \(assetExport?.error)")
case AVAssetExportSessionStatus.unknown:
print("unknown\(assetExport?.error)")
case AVAssetExportSessionStatus.waiting:
print("waiting\(assetExport?.error)")
case AVAssetExportSessionStatus.exporting:
print("exporting\(assetExport?.error)")
default:
print("Audio Concatenation Complete")
}
})
}
}