CMTimeGetSeconds duration and addPeriodicTimeObserver doesn't equal at the end
Since the times are coming from different sources, there might be slightly discrepancies on the 'delta' of milliseconds, hence you should round
them doing so:
if floor(currentSeconds) == floor(durationSeconds) {
self.handleAudioFinishPlaying()
}
In iOS AVPlayer, addPeriodicTimeObserverForInterval seems to be missing
Check this func addPeriodicTimeObserver(forInterval interval: CMTime,
queue: DispatchQueue?,
using block: @escaping (CMTime) -> Void) -> Any
It is in the documents also for example check this code snippet
let timeObserverToken = player.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main) { [unowned self] time in
}
Referenced from here
AVMutableAudioMixInputParameters setVolume atTime not working only with kCMTimeZero
You should do something like this:
- (IBAction)maxVolButtonPressed:(id)sender {
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(setVolume:) userInfo:nil repeats:NO];
}
- (void)setVolume:(id)sender{
[audioInputParams1 setVolume:1 atTime:kCMTimeZero];
[self applyAudioMix];
}
In first method you are saying: wait for 1 second and then call method setVolume:.
How to use AVPlayer addBoundaryTimeObserver?
As per the docs
times - An array of NSValue objects containing
CMTime
values representing the times at which to invoke block.
You need to create appropriate CMTime
structs then create an array of NSValue
objects:
let cmtime = CMTime(seconds: 2.18, preferredTimescale: 100)
let cmtimevalue = NSValue(time: cmtime)
let cmtimevalueArray = [cmtimevalue]
Be aware that the seconds for this initialiser is Double
and timescale
is CMTimeScale
aka Int32
Also your arrayOfBeats
is not an Array
, it's a Dictionary
and items in dictionaries are NOT ordered. You probably won't get the order you were looking for.
You might be better served with an array of tuples.
let arrayOfBeats = [("2.18", 3), ("3.38", 5), ("3.63", 6)]
Converting your String
seconds value back to Double
is your (trivial) problem.
Trying to understand CMTime
A CMTime
struct represents a length of time that is stored as rational number (see CMTime Reference). CMTime
has a value
and a timescale
field, and represents the time value/timescale seconds
.
CMTimeMake
is a function that returns a CMTime
structure, for example:
CMTime t1 = CMTimeMake(1, 10); // 1/10 second = 0.1 second
CMTime t2 = CMTimeMake(2, 1); // 2 seconds
CMTime t3 = CMTimeMake(3, 4); // 3/4 second = 0.75 second
CMTime t4 = CMTimeMake(6, 8); // 6/8 second = 0.75 second
The last two time values t3
and t4
represent the same time value, therefore
CMTimeCompare(t3, t4) == 0
If you set the videoMinFrameDuration
of a AVCaptureSession
is does not make a difference if you set
connection.videoMinFrameDuration = CMTimeMake(1, 20); // or
connection.videoMinFrameDuration = CMTimeMake(2, 40);
In both cases the minimum time interval between frames is set to 1/20 = 0.05 seconds.
How do I get current playing time and total play time in AVPlayer?
You can access currently played item by using currentItem
property:
AVPlayerItem *currentItem = yourAVPlayer.currentItem;
Then you can easily get the requested time values
CMTime duration = currentItem.duration; //total time
CMTime currentTime = currentItem.currentTime; //playing time
Swift 5:
if let currentItem = player.currentItem {
let duration = CMTimeGetSeconds(currentItem.duration)
let currentTime = CMTimeGetSeconds(currentItem.currentTime())
print("Duration: \(duration) s")
print("Current time: \(currentTime) s")
}
Related Topics
Dictionary Becomes Nil When Trying to Pass Back Cllocation Object to iOS App Extension
Why Swift Disallows Weak Reference for Non-Optional Type
Swiftui Multiple Animations for Same Element
Solve Equations of Type A*X = B Using Dgtsv_ or Sgtsv_
Decoding Different Type with and Without Array
How to Convert an Array to List in Realm
Can Not Get Correct Position of View in Swiftui
Assigning Values to Tuple in for Loop
List Is Not Conforming to Encodable
Swift Unsafemutablepointer & Unsafemutablepointer<Unsafepointer<Sometype>>
Error Validating Cms Signature
Swiftui App Crashes Every Time When Trying to Add Item to Grouped Items
Biometric Authentication Evaluation with Swiftui
Testing a Class Which Preserves Its State in Private Variables