how to get current time in AVAudioplayer in a label while playing - swift

I tried to build an app to play the MP3 files. I want to get current time and duration to show it in 2 labels while playing.
I used this code:
let duration = Int((player1?.duration)!)
let minutes2 = duration/60
let seconds2 = duration - minutes2 * 60
durLabel.text = NSString(format: "%02d:%02d", minutes2,seconds2) as String
let currentTime1 = Int((player1?.currentTime)!)
let minutes = currentTime1/60
let seconds = currentTime1 - minutes * 60
curLabel.text = NSString(format: "%02d:%02d", minutes,seconds) as String
In duration it shows the half time of the song.
For example:
If the duration of the song is 20 minutes and 40 seconds, it shows the half like that 10:20. But it did not make progress in the case of current time, it shows 00:00
Thanks at all.

// this is to compute and show remaining time
let duration = Int((player1?.duration - (player1?.currentTime))!)
let minutes2 = duration/60
let seconds2 = duration - minutes2 * 60
durLabel.text = NSString(format: "%02d:%02d", minutes2,seconds2) as String
//This is to show and compute current time
let currentTime1 = Int((player1?.currentTime)!)
let minutes = currentTime1/60
let seconds = currentTime1 - minutes * 60
curLabel.text = NSString(format: "%02d:%02d", minutes,seconds) as String

The player's current time will not automatically update. Instead, you have to periodically update the current time using a timer. Take a look at the following thread if you want more details.
check avaudioplayer's current playback time

If you have Slider to seek on mp3 file, like this :
#IBOutlet weak var slidetOutlet: UISlider!
You can update your UILabel like this:
self.lblPlayerTime.text = self.secondsToHoursMinutesSeconds(seconds: Double( self.slidetOutlet.value ))
to initial your Player and Slider :
func playerInit() {
guard let url = URL(string: urlAudio) else {
return
}
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
print("AVAudioSession Category Playback OK")
do {
try AVAudioSession.sharedInstance().setActive(true)
print("AVAudioSession is Active")
playerItem = AVPlayerItem(url: url)
player = AVPlayer(playerItem: playerItem)
player!.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, 1), queue: DispatchQueue.main) { (CMTime) -> Void in
if self.player!.currentItem?.status == .readyToPlay {
let time : Float64 = CMTimeGetSeconds(self.player!.currentTime());
self.slidetOutlet.value = Float ( time )
self.slidetOutlet.minimumValue = 0
let duration : CMTime = self.playerItem!.asset.duration
let seconds : Float64 = CMTimeGetSeconds(duration)
self.slidetOutlet.maximumValue = Float(seconds)
self.lblStartTimePlayer.text = self.secondsToHoursMinutesSeconds(seconds: seconds)
self.lblPlayerTime.text = self.secondsToHoursMinutesSeconds(seconds: Double( self.slidetOutlet.value ))
}
}
} catch let error as NSError {
print(error.localizedDescription)
}
} catch let error as NSError {
print(error.localizedDescription)
}
}
and for convert the time :
func secondsToHoursMinutesSeconds (seconds : Double) -> (String) {
let (hr, minf) = modf (seconds / 3600)
let (min, secf) = modf (60 * minf)
return ("\(Int(hr)):\(Int(min)):\(Int(60 * secf))")
}

Related

swift how to convert date component in a double to get upload speed

I try to check the speed of upload by sending an image to a server via FTP, I know it is not very sharp, but I have no alternative. Issue number one is to test time, this code is always giving me 0 seconds maybe it is right maybe not, but the main issue id that I cannot even divide the size in mb of image by time in seconds, since time elapsed is expressed in dateComponent, how to do it?
using this code
func pushfileUpload() {
print("uploading...")
let startDate = NSDate()
//*****************************************************************
//find file in app bundle
let imageChecked = findFileInBundle(nameWithNoEx: "image", extension: "jpg")
//convert to Data
let imageData = imageChecked.jpegData(compressionQuality: 1)
//instanziate the class
let ftpUploader = FTPUpload.init(baseUrl: Constants.kHostname, userName: Constants.kUsername, password: Constants.kPassword, directoryPath: Constants.kFolder)
ftpUploader.send(data: imageData!, with: "image") { (result) in
if result {
print("result is \(result)")
} else {
print("no result")
}
}
//*****************************************************************
print("...uploaded")
let endDate = NSDate()
let difference = timeDifference(date1: startDate as Date, date2: endDate as Date)
// print("Time difference is : \(difference)")
//1 converto to string
let differenceString = String(difference)
//2 pick first 3 ints
let array = differenceString.compactMap{Int(String($0))}
//3 create new int
let newInt = array[0...3]
var newString = ""
for i in newInt {
newString.append(i.description)
}
var fromIntToString = Int(newString)
fromIntToString = fromIntToString! * 1000
let speed = 1500 / fromIntToString!
print("speed: \(speed)")
}
func timeDifference(date1: Date, date2: Date) -> Int {
let calendar = NSCalendar.current
var compos:Set<Calendar.Component> = Set<Calendar.Component>()
// compos.insert(.second)
compos.insert(.nanosecond)
let difference = calendar.dateComponents(compos, from: date1, to: date2)
// print("diff in seconds= \(difference.second!)") // difference in seconds
print("diff in nanoseconds = \(difference.nanosecond!)") // difference in nanoseconds
let newValue = difference.nanosecond!
return newValue
}
//UPADTED code
func pushfileUpload() {
print("uploading...")
let startDate = Date()
//*****************************************************************
//find file in app bundle
let imageChecked = findFileInBundle(nameWithNoEx: "image", extension: "jpg")
//convert to Data
let imageData = imageChecked.jpegData(compressionQuality: 1)
//instanziate the class
let ftpUploader = FTPUpload.init(baseUrl: Constants.kHostname, userName: Constants.kUsername, password: Constants.kPassword, directoryPath: Constants.kFolder)
ftpUploader.send(data: imageData!, with: "image") { (result) in
if result {
print("result is \(result)")
//-----------------------------------------------------
//Your code to calculate elapsed time belongs here
let endDate = Date()
let elapsed = endDate.timeIntervalSinceReferenceDate -
startDate.timeIntervalSinceReferenceDate
print("The download took \(elapsed) seconds.")
print("speed is \(1500 / elapsed)")
//-----------------------------------------------------
} else {
print("no result")
}
}}
prints on console
The download took 1.281269907951355 seconds.
speed is 1170.7135168720042
As others have said, you need to move your code that calculates total time inside your closure:
func pushfileUpload() {
print("uploading...")
let startDate = Date()
//*****************************************************************
//find file in app bundle
let imageChecked = findFileInBundle(nameWithNoEx: "image", extension: "jpg")
//convert to Data
let imageData = imageChecked.jpegData(compressionQuality: 1)
//instanziate the class
let ftpUploader = FTPUpload.init(baseUrl: Constants.kHostname, userName: Constants.kUsername, password: Constants.kPassword, directoryPath: Constants.kFolder)
ftpUploader.send(data: imageData!, with: "image") { (result) in
if result {
print("result is \(result)")
//-----------------------------------------------------
//Your code to calculate elapsed time belongs here
let endDate = Date()
let elapsed = endDate.timeIntervalSinceReferenceDate -
startDate.timeIntervalSinceReferenceDate
print("The download took \(elasped) seconds."
//-----------------------------------------------------
} else {
print("no result")
}
}
...
As others have mentioned, there's no reason to deal with date components. The method timeIntervalSinceReferenceDate gives you a double precision count of seconds for a date, so it's easy to do math on dates to figure out the difference between them. You can evaluate the difference to as many decimal places as you want.

AVPlayer currentTime wrong when rewind

I have AVPlayer when I rewind an audio file, then the current time is longer than the total file duration. Who knows what the problem is, why is the current time wrong?
#objc func updateProgressBar(){
guard let value = AppDelegate.avPlayer.currentItem?.currentTime().seconds else { return }
let time = Func.getHoursMinutesSecondsFrom(seconds: value)
DispatchQueue.main.async {
self.startTime.text = time.fullTime
}
}
func durationAudio(){
// расчитывает время аудиозвука
guard let duration = AppDelegate.avPlayer.currentItem?.asset.duration else { return }
let time = Func.getHoursMinutesSecondsFrom(seconds: CMTimeGetSeconds(duration))
DispatchQueue.main.async {
self.endTime.text = time.fullTime
}
}
converts to hours, minutes, seconds
static func getHoursMinutesSecondsFrom(seconds: Double) -> (hours: Int, minutes: Int, seconds: Int, fullTime:String) {
let secs = Int(seconds)
let hours = secs / 3600
let minutes = (secs % 3600) / 60
let seconds = (secs % 3600) % 60
let duration:String!
if hours != 0 {
duration = String(format:"%02d:%02d:%02d", hours, minutes, seconds)
} else {
duration = String(format:"%02d:%02d", minutes, seconds)
}
return (hours, minutes, seconds, duration)
}
rewind audio
func seekTo(completion:Bool){
let duration = CMTimeGetSeconds(AppDelegate.avPlayer.currentItem!.asset.duration)
let value = self.sliderSong.value
let durationToSeek = Float(duration) * value
let timeScale = AppDelegate.avPlayer.currentItem!.duration.timescale
AppDelegate.avPlayer.seek(to: CMTimeMakeWithSeconds(Float64(durationToSeek), preferredTimescale: timeScale), toleranceBefore: CMTime.zero, toleranceAfter: CMTime.zero) { [weak self](bool) in
guard completion else { return }
self?.seeking = false
}
}

Date not getting the current time of the phone Swift 4

I have this function that calculate the amount of time from the current minute of the phone. I am trying to have a timer go off after this specific amount of time. The way this function is called is through a switch that the user flips in the app and the time should reset it self. Well Im having trouble with my NSDate object getting old values when the switch was flipped before. Is there a way to reset the NSDate object to zero?
Here is my code for the calculating the current time of the phone.
func GetInitialTime(){
finalTime = 0
firstTimeCounter = 0
timeInSeconds = 0
let calendar = NSCalendar.current
var minutes = calendar.component(.minute, from: date)
var timeDifference = Int()
if(minutes == 00 || minutes == 30) {
print("The minute hand is at zero or thirty.")
}
else {
print("The minute hand is NOT ar zero or thirty")
print("The minute hand ia at:")
if minutes < 30 {
while (!(minutes == 30)) {
minutes += 1
timeDifference += 1
}
print("Therefore we make the minute hand at zero or thiry: ", minutes)
print("The time difference we add to the minute is: ", timeDifference)
}
else {
var i = minutes
while i < 60 {
i += 1
minutes += 1
timeDifference += 1
}
print("Therefore we make the minute hand at zero or thirty: ", minutes)
print("The Time difference we add to the minute is: ", timeDifference)
}
}
finalTime = Double(timeDifference * 60)
print("The time difference in seconds is:", finalTime)
}
And I declare the Date() object here
let center = UNUserNotificationCenter.current()
let button = UIButton(type: UIButtonType.custom)
let date = Date()
var timeInSeconds = Int()
var finalTime = Double()
var halfHour = Double(1800)
var firstTimeCounter = Int()
var firstTimer = Timer()
var repeatingTimer = Timer()
var backgroundTask = BackgroundTask()
let dispatchGroup = DispatchGroup()
var urlWeb = "http://morrowrenewablesflowdata.com/iOSConnections/Notifications.php"
var downtimes = [String]()
var flows = [String]()
Your code is using an instance variable date that is a let constant. You don't show the context in which it's set, but I'm assuming it's an instance variable of your class. The fact that it's a let constant means it will never change in the scope in which it's declared. That's almost certainly your problem.

Slider Crashes program on Iphone, but worked on simulator

I am using this code to track and update slider value, then convert to string and show on label. The issue is that, This code works on the simulator, but crashed on the actual phone. With error "unexpectedly found nil while unwrapping an Optional value". I can't figure out what could cause this, any help would be appreciated.
func updateSlider () {
sliderBar.value = Float(CMTimeGetSeconds(self.audioPlayer.currentItem!.currentTime()))
let currentTime = Int((CMTimeGetSeconds(self.audioPlayer.currentItem!.currentTime())))
let duration = Int((CMTimeGetSeconds(self.audioPlayer.currentItem!.asset.duration)))
//let total = currentTime - duration
let minutes = currentTime/60
let seconds = currentTime - minutes * 60
let minutes2 = duration/60
let seconds2 = duration - minutes2 * 60
self.lblPastTime.text = NSString(format: " %02d:%02d / %02d:%02d ",minutes2,seconds2, minutes,seconds) as String
}
Avoiding force unwrapping variables is a good way to avoid errors like that:
func updateSlider () {
guard let currentItem = self.audioPlayer.currentItem else { return }
sliderBar.value = Float(CMTimeGetSeconds(currentItem.currentTime()))
let currentTime = Int((CMTimeGetSeconds(currentItem.currentTime())))
let duration = Int((CMTimeGetSeconds(currentItem.asset.duration)))
//let total = currentTime - duration
let minutes = currentTime/60
let seconds = currentTime - minutes * 60
let minutes2 = duration/60
let seconds2 = duration - minutes2 * 60
self.lblPastTime.text = NSString(format: " %02d:%02d / %02d:%02d ",minutes2,seconds2, minutes,seconds) as? String
}
Using guard let I safely unwrap the variable, so if it's nil, the function returns.
ANSWER
Had to use audioPlayer.currentTime()
Instead of audioPlayer.currentItem!.currentTime()
func updateSlider () {
sliderBar.value = Float(CMTimeGetSeconds(audioPlayer.currentTime()))
// sliderBar.value = Float(CMTimeGetSeconds(self.audioPlayer.currentItem!.currentTime()))
let currentTime = Int((CMTimeGetSeconds(self.audioPlayer.currentItem!.currentTime())))
let duration = Int((CMTimeGetSeconds(self.audioPlayer.currentItem!.asset.duration)))
//let total = currentTime - duration
let minutes = currentTime/60
let seconds = currentTime - minutes * 60
let minutes2 = duration/60
let seconds2 = duration - minutes2 * 60
self.lblPastTime.text = NSString(format: " %02d:%02d / %02d:%02d ",minutes2,seconds2, minutes,seconds) as String
}

Setting Timer Countdown From When Object was Created

I'm querying the createdAt date from several objects in Parse. I want to create a 24 hour (or 48/72, etc.) countDown timer that counts down from when the object was created to 24 hours later. (I'm also then formatting it, and displaying it on a cell UILabel.)
Example: If an object was created at 19:34:33, I want it to expire 24 hours after that (or how ever many hours i specify after it was created). Ultimately showing on the UILabel the hours left until the object expires.
Currently, I'm retrieving when it was created making it repetitively count down from when it was created.
However, I want to make the logic so that it takes when the object was created and then shows how many hours are left until the 24 hours or 48 hours, 72 hours, etc are up.
EDIT
Thanks to #pulsar I added a few more lines of code to the description below. The problem now is that I can only retrieve and correctly countDown the createdAt date and time for 1 object, because only the first object is queried, making all the other objects have the same expiration countDown timer in their respective indexPath.row as the first object in Parse.
I can't figure out how to add all the objects so that they all have their own respective countDown expiration times that is triggered by the expiresAt function.
Here's how i'm querying it and formatting it (in the viewDidLoad):
This is the question I asked that help me format the dates: Swift countDown timer Logic
Please see the comments in the code!
var createdAt = object.createdAt
if createdAt != nil {
//assuming this is where i have to set expiration logic?
let calendar = NSCalendar.currentCalendar()
let comps = calendar.components([.Hour, .Minute, .Second], fromDate: createdAt as NSDate!)
let hours = comps.hour * 3600
let minutes = comps.minute * 60
let seconds = comps.second
//I'm adding these two lines below but not sure what to do with them considering I need to add all the objects to an array that will then display it on indexPath.row(s)
let twentyFourHours = NSTimeInterval(60 * 60 * 24)
self.expiresAt = NSDate(timeInterval: twentyFourHours, sinceDate: object.createdAt!!)
self.timerInt.append(hours + minutes + seconds)
//What do i append to the timerInt array? How can i append the objects while triggering the the expiresAt function?
}
Here's my countDown function:
func countDown() {
//timerInt is an array where I'm storing the Int values.
for i in 0 ..< timerInt.count {
let hours = timerInt[i] / 3600
//have to somehow add the expiresAt method while looping through each value [i]...?
let minsec = timerInt[i] % 3600
let minutes = minsec / 60
let seconds = minsec % 60
print(String(format: "%02d:%02d:%02d", hours, minutes, seconds))
timerInt[i]--
//Im assuming best practice would be to loop through the values in order to change the values/set the expiration time to each object (the expiresAt method). Any ideas of how and where I can add this in this loop so that it reflects the countDown I want to set?
}
self.tableView.reloadData()
}
Lastly, for my indexPath.row, I am formatting it and displaying it like this:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! cell
//I'm formatting the hours, minutes, seconds. However I'm missing the expiresAt function and I have no clue as to where and how to include it... Should it be here or in the countDown() loop?
let hours = timerInt[indexPath.row] / 3600
let minsec = timerInt[indexPath.row] % 3600
let minutes = minsec / 60
let seconds = minsec % 60
myCell.secondLabel.text = String(format: "%02d:%02d:%02d", hours, minutes, seconds)
return myCell
}
Ideas on how to set it to countdown 24/48/72 hours later from when it was created?
Any and all help is much appreciated!
Sounds like what you need is to set the expiry date and then get the date components between the current date and the expiry date. Then you can use an NSTimer to refresh the display. (Don't forget to call NSTimer.invalidate() when you're done).
An example:
class YourViewController: UIViewController {
var expiresAt: NSDate?
func viewDidLoad() {
// your parse logic here
let twentyFourHours = NSTimeInterval(60 * 60 * 24)
expiresAt = NSDate(timeInterval: twentyFourHours, sinceDate: createdAt)
scheduleTimer()
}
func scheduleTimer() {
NSTimer.scheduledTimerWithTimeInterval(1.0 / 30.0, target: self, selector: "tick:", userInfo: nil, repeats: true)
}
#objc
func tick(timer: NSTimer) {
guard let expiresAt = expiresAt else {
return
}
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
if let components = calendar?.components([.Hour, .Minute, .Second], fromDate: NSDate(), toDate: expiresAt, options: []) {
print(formatDateComponents(components))
}
}
func formatDateComponents(components: NSDateComponents) -> String {
let hours = components.hour
let minutes = components.minute
let seconds = components.second
return "\(hours):\(minutes):\(seconds)"
}
}
You could also make your life much easier by using a structure to store the date components rather than doing that complicated parsing of your timerInt strings.
struct Time {
let hours: String
let minutes: String
let seconds: String
}
//: Playground - noun: a place where people can play
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
import UIKit
class MyView: UIView {
weak var l1: UILabel?
weak var l2: UILabel?
weak var l3: UILabel?
let validFor: NSTimeInterval
var validTo: NSDate = NSDate()
lazy var timer: NSTimer = NSTimer(timeInterval: self.validFor, target: self, selector: "done", userInfo: nil, repeats: false)
init(validFor: NSTimeInterval) {
self.validFor = validFor
super.init(frame: CGRect(x: 0, y: 0, width: 500, height: 100))
validTo = timer.fireDate
let ll1 = UILabel(frame: CGRect(x: 1, y: 1, width: 498, height: 30))
ll1.text = "created at: \(NSDate())"
self.addSubview(ll1)
l1 = ll1
let ll2 = UILabel(frame: CGRect(x: 1, y: 33, width: 498, height: 30))
ll2.text = "valid to: \(validTo)"
self.addSubview(ll2)
l2 = ll2
let ll3 = UILabel(frame: CGRect(x: 1, y: 66, width: 498, height: 30))
ll3.text = "valid for next: \(validTo.timeIntervalSinceNow) second"
self.addSubview(ll3)
l3 = ll3
NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// when MyView expires it trigers this function
// and give me a chance to update UI
func done() {
// update UI
dispatch_async(dispatch_get_main_queue(), { [unowned self] () -> Void in
self.l2?.text = " EXPIRED"
self.l3?.text = ""
if let text1 = self.l1?.text,
let text2 = self.l2?.text,
let text3 = self.l3?.text
{
print("")
print(text1, text2, text3)
}
})
}
func updateState() {
// periodically updating UI on request
if timer.valid {
let v = validTo.timeIntervalSinceNow
// update UI
dispatch_async(dispatch_get_main_queue(), { [unowned self] () -> Void in
self.l3?.text = "valid for next: \(v) second"
if let text1 = self.l1?.text,
let text2 = self.l2?.text,
let text3 = self.l3?.text
{
print(text1, text2, text3)
}
})
}
}
}
let c = MyView(validFor: 10.0) // in seconds
let queue = dispatch_queue_create("update", DISPATCH_QUEUE_SERIAL)
// periodic action tu update UI
// in regular intervals
// this is just for demonstration, avoid using use sleep
// in real application
dispatch_async(queue) { () -> Void in
repeat {
c.updateState()
sleep(3)
} while true
}
dispatch_async(dispatch_get_main_queue(), { () -> Void in
print("the app is running and responding on user actions")
print("MyView labels are updating 'automatically'\n")
})
print("playground continue ....\n")