Skip to content

Instantly share code, notes, and snippets.

@gatamar
Created June 12, 2017 15:48
Show Gist options
  • Save gatamar/45fdac332a69bdca7a795291c03ead34 to your computer and use it in GitHub Desktop.
Save gatamar/45fdac332a69bdca7a795291c03ead34 to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// so_44390046
//
// Created by Olha Pavliuk on 6/12/17.
// Copyright © 2017 olia. All rights reserved.
//
import UIKit
import AVFoundation
import Foundation
class ViewController: UIViewController {
var currentAsset: AVAsset?
override func viewDidLoad() {
super.viewDidLoad()
self.trimmingFunc()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func trimmingFunc() {
let audioURL = URL.init(fileURLWithPath: Bundle.main.path(forResource: "Be That Man", ofType: "mp3")!)
print("audioURL=\(audioURL)")
currentAsset = AVAsset(url: audioURL)
let options = NSKeyValueObservingOptions([.new, .old, .initial, .prior])
currentAsset!.addObserver(self, forKeyPath: "duration", options: options, context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
// do something upon notification of the observed object
print("\(keyPath): \(change?[.newKey])")
print(CMTimeGetSeconds(currentAsset!.duration)) //This is printing 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment