Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b10236a

Browse files
added support for Local File URL
1 parent 06f075d commit b10236a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Please note that using videos from URLs requires ensuring that you have the righ
124124

125125
| Name | Description | Default |
126126
|---------------|-----------------------------------------------------------------------------------------------------|---------|
127-
| **SourceName** | The URL or local filename of the video. | - |
127+
| **SourceName** | **Direct URL String** If the name represents a valid URL ( HTTP etc). **Local File URL** If the name is a valid local file path (file:// scheme). **Bundle Resource** It tries to locate the file in the main bundle using Bundle.main.url(forResource:withExtension:) | - |
128128
| **Ext** | File extension for the video, used when loading from local resources. This is optional when a URL is provided and the URL ends with the video file extension. | "mp4" |
129129
| **Subtitles** | The URL or local filename of the WebVTT (.vtt) subtitles file to be merged with the video. With a AVMutableComposition approach that is used currently in the package, you cannot directly change the position or size of subtitles. AVFoundation’s built-in handling of "text" tracks simply renders them in a default style, without allowing additional layout options. Take a look on the implementation in the example app *Video8.swift* | - |
130130
| **Gravity** | How the video content should be resized to fit the player's bounds. | .resizeAspect |

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ fileprivate func assetFrom(name: String, fileExtension: String?) -> AVURLAsset?
4848
return AVURLAsset(url: url)
4949
}
5050

51+
if let url = fileURL(from: name){
52+
return AVURLAsset(url: url)
53+
}
54+
5155
// If not a valid URL, try to locate the file in the main bundle with the specified extension.
5256
if let fileExtension = fileExtension,
5357
let fileUrl = Bundle.main.url(forResource: name, withExtension: fileExtension) {
@@ -58,6 +62,27 @@ fileprivate func assetFrom(name: String, fileExtension: String?) -> AVURLAsset?
5862
return nil
5963
}
6064

65+
66+
/// Attempts to create a valid `URL` from a string that starts with `"file://"`.
67+
/// - Parameter rawString: A file URL string, e.g. `"file:///Users/igor/My Folder/File.mp4"`.
68+
/// - Returns: A `URL` if successfully parsed; otherwise `nil`.
69+
func fileURL(from rawString: String) -> URL? {
70+
guard rawString.hasPrefix("file://") else {
71+
// Not a file URL scheme
72+
return nil
73+
}
74+
// Strip off "file://"
75+
let pathIndex = rawString.index(rawString.startIndex, offsetBy: 7)
76+
let pathPortion = rawString[pathIndex...]
77+
78+
guard let encodedPath = pathPortion
79+
.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)
80+
else { return nil }
81+
82+
let finalString = "file://\(encodedPath)"
83+
return URL(string: finalString)
84+
}
85+
6186
/// Checks whether a given filename contains an extension and returns the extension if it exists.
6287
///
6388
/// - Parameter name: The filename to check.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /