Skip to content

Commit 835bdd0

Browse files
committed
refactoring
1 parent 04c64ee commit 835bdd0

File tree

3 files changed

+37
-28
lines changed

3 files changed

+37
-28
lines changed

Sources/swiftui-loop-videoplayer/fn/fn+.swift

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,49 @@ import CoreImage
1212
#endif
1313

1414
/// Retrieves an `AVURLAsset` based on specified video settings.
15-
/// - Parameter settings: The `VideoSettings` object containing details like name and extension of the video.
16-
/// - Returns: An optional `AVURLAsset`. Returns `nil` if the video cannot be located either by URL or in the app bundle.
15+
/// - Parameter settings: A `VideoSettings` object containing the video name and extension.
16+
/// - Returns: An optional `AVURLAsset`. Returns `nil` if a valid URL cannot be created or the file cannot be found in the bundle.
1717
func assetFor(_ settings: VideoSettings) -> AVURLAsset? {
1818
let name = settings.name
19-
let ext = settings.ext
19+
// If the name already includes an extension, use that; otherwise, use `settings.ext`.
20+
let ext = extractExtension(from: name) ?? settings.ext
2021

21-
// Attempt to create a URL directly from the provided video name string
22+
// Leverage the common helper to construct the `AVURLAsset`.
23+
return assetFrom(name: name, fileExtension: ext)
24+
}
25+
26+
/// Retrieves an `AVURLAsset` for the subtitles specified in `VideoSettings`.
27+
/// - Parameter settings: A `VideoSettings` object containing the subtitle file name.
28+
/// - Returns: An optional `AVURLAsset` for the subtitle file, or `nil` if `subtitles` is empty or cannot be found.
29+
func subtitlesAssetFor(_ settings: VideoSettings) -> AVURLAsset? {
30+
let subtitleName = settings.subtitles
31+
// If no subtitle name is provided, early return `nil`.
32+
guard !subtitleName.isEmpty else {
33+
return nil
34+
}
35+
36+
// Use a default `.vtt` extension for subtitles.
37+
return assetFrom(name: subtitleName, fileExtension: "vtt")
38+
}
39+
40+
/// A common helper that attempts to build an `AVURLAsset` from a given name and optional file extension.
41+
/// - Parameters:
42+
/// - name: The base file name or a URL string.
43+
/// - fileExtension: An optional file extension to be appended if `name` isn't a valid URL.
44+
/// - Returns: An optional `AVURLAsset`, or `nil` if neither a valid URL nor a local resource file is found.
45+
fileprivate func assetFrom(name: String, fileExtension: String?) -> AVURLAsset? {
46+
// Attempt to create a valid URL from the provided string.
2247
if let url = URL.validURLFromString(name) {
2348
return AVURLAsset(url: url)
24-
// If direct URL creation fails, attempt to locate the video in the main bundle using the name and extension
25-
} else if let fileUrl = Bundle.main.url(forResource: name, withExtension: extractExtension(from: name) ?? ext) {
49+
}
50+
51+
// If not a valid URL, try to locate the file in the main bundle with the specified extension.
52+
if let fileExtension = fileExtension,
53+
let fileUrl = Bundle.main.url(forResource: name, withExtension: fileExtension) {
2654
return AVURLAsset(url: fileUrl)
2755
}
2856

57+
// If all attempts fail, return `nil`.
2958
return nil
3059
}
3160

@@ -106,26 +135,6 @@ internal func handleVideoComposition(request: AVAsynchronousCIImageFilteringRequ
106135
request.finish(with: currentImage, context: nil)
107136
}
108137

109-
/// Retrieves an `AVURLAsset` for the subtitles specified in `VideoSettings`.
110-
/// - Parameter settings: The `VideoSettings` object containing details about the subtitle file (e.g., name and extension).
111-
/// - Returns: An optional `AVURLAsset` for the subtitle file, or `nil` if the file does not exist.
112-
func subtitlesFor(_ settings: VideoSettings) -> AVURLAsset? {
113-
let name = settings.subtitles
114-
let ext = "vtt"
115-
116-
if name.isEmpty { return nil }
117-
118-
// Attempt to create a URL directly from the provided video name string
119-
if let url = URL.validURLFromString(name) {
120-
return AVURLAsset(url: url)
121-
// If direct URL creation fails, attempt to locate the video in the main bundle using the name and extension
122-
} else if let fileUrl = Bundle.main.url(forResource: name, withExtension: ext) {
123-
return AVURLAsset(url: fileUrl)
124-
}
125-
126-
return nil
127-
}
128-
129138
/// Merges a video asset with an external WebVTT subtitle file into an AVMutableComposition.
130139
/// Returns a new AVAsset that has both the video/audio and subtitle tracks.
131140
///

Sources/swiftui-loop-videoplayer/protocol/player/AbstractPlayer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ extension AbstractPlayer{
194194

195195
guard currentItem?.status == .readyToPlay else{
196196
/// The case when the video is finished and we are trying to seek back
197-
if let currentItem, let currentAsset, let currentSettings{
197+
if let currentItem{
198198

199199
let callback : ((AVPlayerItem.Status) -> Void)? = { [weak self] status in
200200
if status == .readyToPlay{

Sources/swiftui-loop-videoplayer/protocol/player/LoopingPlayerProtocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ internal extension LoopingPlayerProtocol {
169169
let newItem: AVPlayerItem
170170

171171
// try to retrieve the .vtt subtitle
172-
if let subtitleAsset = subtitlesFor(settings),
172+
if let subtitleAsset = subtitlesAssetFor(settings),
173173
let mergedAsset = mergeAssetWithSubtitles(videoAsset: asset, subtitleAsset: subtitleAsset) {
174174
// Create a new AVPlayerItem from the merged asset
175175
newItem = AVPlayerItem(asset: mergedAsset)

0 commit comments

Comments
 (0)