Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Example/SwiftAudio.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = 7U2TUNKNQX;
INFOPLIST_FILE = SwiftAudio/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -555,7 +555,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = 7U2TUNKNQX;
INFOPLIST_FILE = SwiftAudio/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
32 changes: 32 additions & 0 deletions SwiftAudioEx/Classes/AVPlayerWrapper/AVPlayerWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ class AVPlayerWrapper: AVPlayerWrapperProtocol {
applyAVPlayerRate()
}
}

private var _rampSecs: Double = 0.0;
var rampSecs: Double {
get { _rampSecs }
set {
_rampSecs = newValue
applyRampSecs()
}
}

weak var delegate: AVPlayerWrapperDelegate? = nil

Expand Down Expand Up @@ -406,8 +415,31 @@ class AVPlayerWrapper: AVPlayerWrapperProtocol {
}

private func applyAVPlayerRate() {
if (playWhenReady) {
applyRampSecs()
}
avPlayer.rate = playWhenReady ? _rate : 0
}

private func applyRampSecs() {
if let item = self.item {
let currentTime = item.currentTime()
let params = AVMutableAudioMixInputParameters(track: item.asset.tracks(withMediaType: .audio).first)

if (self._rampSecs > 0.0) {
let rampDuration = CMTime(seconds: self._rampSecs, preferredTimescale: 1)

params.setVolumeRamp(fromStartVolume: 0.0, toEndVolume: avPlayer.volume, timeRange: CMTimeRange(start: currentTime, duration: rampDuration))
} else {
params.setVolume(avPlayer.volume, at: currentTime)
}

let audioMix = AVMutableAudioMix()
audioMix.inputParameters = [params]

item.audioMix = audioMix
}
}
}

extension AVPlayerWrapper: AVPlayerObserverDelegate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ protocol AVPlayerWrapperProtocol: AnyObject {
var playbackError: AudioPlayerError.PlaybackError? { get }

var rate: Float { get set }

var rampSecs: Double { get set }

var delegate: AVPlayerWrapperDelegate? { get set }

Expand Down
5 changes: 5 additions & 0 deletions SwiftAudioEx/Classes/AudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ public class AudioPlayer: AVPlayerWrapperDelegate {
get { wrapper.rate }
set { wrapper.rate = newValue }
}

public var rampSecs: Double {
get { wrapper.rampSecs }
set { wrapper.rampSecs = newValue }
}

// MARK: - Init

Expand Down