Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Support background music #1

Merged
merged 11 commits into from
Feb 11, 2025
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
}

}
2 changes: 1 addition & 1 deletion MTTransitions.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Pod::Spec.new do |s|
s.swift_version = '5.0'
s.source_files = 'Source/**/*.{swift,metal,h}'
s.resource = 'Source/**/Assets.bundle'
s.ios.deployment_target = '10.0'
s.ios.deployment_target = '13.0'
s.xcconfig = { 'MTL_HEADER_SEARCH_PATHS' => '${PODS_CONFIGURATION_BUILD_DIR}/MetalPetal/MetalPetal.framework/Headers'}

s.weak_frameworks = 'MetalKit'
Expand Down
12 changes: 8 additions & 4 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 Expand Up @@ -491,7 +495,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -546,7 +550,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = iphoneos;
Expand All @@ -565,7 +569,7 @@
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = F6J8B455GU;
INFOPLIST_FILE = "$(SRCROOT)/MTTransitionsDemo/Supporting Files/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -589,7 +593,7 @@
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = F6J8B455GU;
INFOPLIST_FILE = "$(SRCROOT)/MTTransitionsDemo/Supporting Files/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
Binary file added MTTransitionsDemo/Resources/audio1_trimmed.mp3
Binary file not shown.
20 changes: 13 additions & 7 deletions MTTransitionsDemo/Samples/TimelineSampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ class TimelineSampleViewController: UIViewController {
}

private func setupVideoPlaybacks() {
guard let clip1 = loadVideoAsset(named: "clip1"),
let clip2 = loadVideoAsset(named: "clip2") else {
guard let clip1 = loadAsset(named: "clip1"),
let clip2 = loadAsset(named: "clip2") else {
return
}
clips = [clip1, clip2]
Expand Down Expand Up @@ -174,20 +174,26 @@ class TimelineSampleViewController: UIViewController {
private func makeComposition(renderSize: CGSize = CGSize(width: 720, height: 720)) {
let timeline = Timeline()
timeline.renderSize = renderSize
let resource1 = AVAssetResource(asset: loadVideoAsset(named: "clip1")!, selectedTimeRange: nil)
let resource2 = AVAssetResource(asset: loadVideoAsset(named: "clip2")!, selectedTimeRange: nil)
let resource3 = AVAssetResource(asset: loadVideoAsset(named: "clip3")!, selectedTimeRange: nil)
let resource1 = AVAssetResource(asset: loadAsset(named: "clip1")!, selectedTimeRange: nil)
let resource2 = AVAssetResource(asset: loadAsset(named: "clip2")!, selectedTimeRange: nil)
let resource3 = AVAssetResource(asset: loadAsset(named: "clip3")!, selectedTimeRange: nil)
let resource4 = { () -> Resource in
let image = UIImage(named: "wallpaper01.jpg")!
let ciImage = CIImage(cgImage: image.cgImage!)
return ImageResource(image: ciImage, duration: CMTime(seconds: 5, preferredTimescale: 1000))
return MTTransitions.ImageResource(image: ciImage, duration: CMTime(seconds: 5, preferredTimescale: 1000))
}()
timeline.clips = [
Clip(resource: resource1),
Clip(resource: resource2),
Clip(resource: resource3),
Clip(resource: resource4)
]
timeline.backgroundAudioClip = { () -> Clip in
let audioResource = AVAssetResource(
asset: loadAsset(named: "audio1_trimmed", withExtension: "mp3")!
)
return Clip(resource: audioResource)
}()
timeline.transitionProvider = DefaultTransitionProvider(effect: effect, seconds: 0.5)

let composition = MTTimelineComposition(timeline: timeline)
Expand Down Expand Up @@ -331,7 +337,7 @@ extension TimelineSampleViewController {
// MARK: - Helper
extension TimelineSampleViewController {

private func loadVideoAsset(named: String, withExtension ext: String = "mp4") -> AVURLAsset? {
private func loadAsset(named: String, withExtension ext: String = "mp4") -> AVURLAsset? {
guard let url = Bundle.main.url(forResource: named, withExtension: ext) else {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion Podfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
platform:ios, '10.0'
platform:ios, '13.0'
use_frameworks!

target 'MTTransitionsDemo' do
Expand Down
4 changes: 2 additions & 2 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
MetalPetal: f4325f3a0dbccdcd1b39e385a9c19d4cc9e803c8
MTTransitions: f4818ba485abc477545af4ee483c00b717dfd76e
MTTransitions: de56179ff6ba1a4e67bbc627288c957beafc0ffc

PODFILE CHECKSUM: 1c191667b33e66fe3cb9571c46a03b195023b8c4
PODFILE CHECKSUM: 3cc245b6f1621658d112bb04b7d0547211c12e77

COCOAPODS: 1.11.3
2 changes: 1 addition & 1 deletion Pods/Local Podspecs/MTTransitions.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading