Developer
Developer

Reputation: 6465

How to pass float value where datatype is CMTime?

I am creating an application in which I have to mix the songs. I have accomplished this but the problem is when I am using the following function.

- (BOOL)insertTimeRange:(CMTimeRange)timeRange ofTrack:(AVAssetTrack *)track atTime:(CMTime)startTime error:(NSError **)error;

I have to pass CMTime type value in the atTime parameter but it doesn't takes Float value and I have to add the another song at some floating point value. Is it possible any how?

Upvotes: 10

Views: 10212

Answers (4)

Peter Lapisu
Peter Lapisu

Reputation: 21005

sadly every answer uses hard coded CMTimeScale(1000), the right way is

let t = CMTimeMake(value: 53425, timescale: CMTimeScale(NSEC_PER_SEC))

Upvotes: 0

Yang_____
Yang_____

Reputation: 213

Swift version

CMTime(seconds: 5.3425, preferredTimescale: CMTimeScale(1000))

Set preferredTimescale larger to get more precision.


Availability

iOS 7.0+ macOS 10.9+ Mac Catalyst 13.0+ tvOS 9.0+ watchOS 6.0+ Xcode 7.1+

Also check Apple Documentation.

Upvotes: 0

Naresh
Naresh

Reputation: 17932

In Swift 4.2 and Xcode 10.1

CMTimeMake(value: 53425, timescale: 10000)// @ 5.3425 sec

Upvotes: 2

user529758
user529758

Reputation:

You can use one of the CMTimeMake...() functions. You have to supply a time point and a timescale value. The former is a 64-bit integer; you can just truncate or round your float to convert it to an integer, or use a necessarily high timescale:

CMTime tm = CMTimeMake(53425, 10000); // @ 5.3425 sec

Upvotes: 20

Related Questions