Skip to content

Commit

Permalink
feat: loop background audio
Browse files Browse the repository at this point in the history
  • Loading branch information
yyjim committed Nov 28, 2022
1 parent 1be621b commit bd84275
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 102 deletions.
26 changes: 26 additions & 0 deletions CMTime+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// CMTime+Extensions.swift
// MTTransitions
//
// Created by Jim Wang on 2022/11/28.
//

import CoreMedia
import Foundation

extension CMTime {

static func makeLoopTime(timeRange: CMTimeRange, at: CMTime = .zero, until: CMTime) -> [(at: CMTime, timeRange: CMTimeRange)] {
let duration = timeRange.duration
var t = at
var times = [(at: CMTime, timeRange: CMTimeRange)]()
while t < until {
let adjustedDuration = CMTimeMinimum(CMTimeSubtract(until, t), duration)
let adjustedTimeRange = CMTimeRange(start: timeRange.start, duration: adjustedDuration)
times.append((at: t, timeRange: adjustedTimeRange))
t = CMTimeAdd(t, adjustedDuration)
}
return times
}

}
4 changes: 4 additions & 0 deletions MTTransitionsDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
A3F2636A243EF67B00A093D2 /* PickImageTransitionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3F26369243EF67B00A093D2 /* PickImageTransitionViewController.swift */; };
FBB33DD328461993006FA36F /* TimelineSampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FBB33DD228461993006FA36F /* TimelineSampleViewController.swift */; };
FBD7D02D2847486500E62822 /* APLCompositionDebugView.m in Sources */ = {isa = PBXBuildFile; fileRef = FBD7D02C2847486500E62822 /* APLCompositionDebugView.m */; };
FBE7123C29348D6500BC77FB /* audio1_trimmed.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = FBE7123B29348D6500BC77FB /* audio1_trimmed.mp3 */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -96,6 +97,7 @@
FBD7D02A2847486400E62822 /* MTTransitionsDemo-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "MTTransitionsDemo-Bridging-Header.h"; sourceTree = "<group>"; };
FBD7D02B2847486500E62822 /* APLCompositionDebugView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = APLCompositionDebugView.h; sourceTree = "<group>"; };
FBD7D02C2847486500E62822 /* APLCompositionDebugView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = APLCompositionDebugView.m; sourceTree = "<group>"; };
FBE7123B29348D6500BC77FB /* audio1_trimmed.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = audio1_trimmed.mp3; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -154,6 +156,7 @@
children = (
A3C9E22024373E4D002F82D9 /* audio1.mp3 */,
A3C9E1F42431D640002F82D9 /* audio2.mp3 */,
FBE7123B29348D6500BC77FB /* audio1_trimmed.mp3 */,
A369F7DC242A035C00546190 /* cut1.mp4 */,
A369F7DD242A035C00546190 /* cut2.mp4 */,
A3CFA84A242B96E900F2E660 /* cut3.mp4 */,
Expand Down Expand Up @@ -293,6 +296,7 @@
A3E0AD2D2205BEA6002DD092 /* 5.jpg in Resources */,
A3E0AD282205BEA6002DD092 /* 8.jpg in Resources */,
A3913EC3242886280083E7D3 /* clip1.mp4 in Resources */,
FBE7123C29348D6500BC77FB /* audio1_trimmed.mp3 in Resources */,
A3E0AD2F2205BEA6002DD092 /* 6.jpg in Resources */,
A3E0AD272205BEA6002DD092 /* 3.jpg in Resources */,
A374124121F98D9B0019F3A3 /* LaunchScreen.storyboard in Resources */,
Expand Down
Binary file added MTTransitionsDemo/Resources/audio1_trimmed.mp3
Binary file not shown.
4 changes: 1 addition & 3 deletions MTTransitionsDemo/Samples/TimelineSampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,8 @@ class TimelineSampleViewController: UIViewController {
// Clip(resource: resource4)
]
timeline.backgroundAudioClip = { () -> Clip in
let timeRange = CMTimeRange(start: .zero, duration: CMTime(seconds: 5, preferredTimescale: 1000))
let audioResource = AVAssetResource(
asset: loadAsset(named: "audio1", withExtension: "mp3")!,
selectedTimeRange: timeRange
asset: loadAsset(named: "audio1_trimmed", withExtension: "mp3")!
)
return Clip(resource: audioResource)
}()
Expand Down
192 changes: 97 additions & 95 deletions Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions Source/Model/Clip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,24 @@ extension AVMutableComposition {
try compositionTrack.insertTimeRange(selectedTimeRange, of: resourceTrackInfo.track, at: time)
}

func addResource(trackID: Int32, with resourceTrackInfo: ResourceTrackInfo, at: CMTime, timeRange: CMTimeRange, until: CMTime) throws {
let assetTrack = resourceTrackInfo.track

let compositionTrack: AVMutableCompositionTrack? = {
if let track = track(withTrackID: trackID) {
return track
}
return addMutableTrack(withMediaType: assetTrack.mediaType, preferredTrackID: trackID)
}()

guard let compositionTrack = compositionTrack else {
throw MTTimelineCompositionError.noCompositionTrack
}

let times = CMTime.makeLoopTime(timeRange: timeRange, at: at, until: until)
for time in times {
print("at: \(time.at), range: \(time.timeRange)")
try compositionTrack.insertTimeRange(time.timeRange, of: resourceTrackInfo.track, at: time.at)
}
}
}
6 changes: 3 additions & 3 deletions Source/Model/MTTimelineComposition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ public class MTTimelineComposition {
try composition.addResource(trackID: trackID, with: resourceInfo, at: time)
case .backgroundAudio:
let resourceInfo = clip.resource.trackInfo(for: .audio, at: trackInfo.index)
try composition.addResource(trackID: trackID, with: resourceInfo, at: time, duration: trackInfo.timeRange.duration)

let endTime = time + trackInfo.timeRange.duration
let endTime = trackInfo.timeRange.end
try composition.addResource(trackID: trackID, with: resourceInfo,
at: time, timeRange: resourceInfo.selectedTimeRange, until: endTime)

let track = composition.track(withTrackID: trackID)
let inputParameter = AVMutableAudioMixInputParameters(track: track)
Expand Down
2 changes: 1 addition & 1 deletion Source/Model/Timeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public class Timeline {
if let backgroundAudioClip = backgroundAudioClip {
let start = clipTimeRanges.first!.start
let end = clipTimeRanges.last!
let timeRange = CMTimeRange(start: .zero, duration: end.end)
let timeRange = CMTimeRange(start: start, duration: end.end)

for index in 0..<backgroundAudioClip.numberOfAudioTracks() {
let trackID = makeTrackID(trackIndex: index, clipIndex: 0, mediaType: .backgroundAudio)
Expand Down

0 comments on commit bd84275

Please sign in to comment.