Skip to content
This repository was archived by the owner on Aug 12, 2022. It is now read-only.

Commit b0b8442

Browse files
Merge pull request #4 from iaomw/#4
Fixes the parsing logic of audio clip, remove unnecessary code in MediaOverlayNode
2 parents 597dbd8 + 321fca9 commit b0b8442

File tree

2 files changed

+14
-58
lines changed

2 files changed

+14
-58
lines changed

r2-shared-swift/MediaOverlayNode.swift

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ public struct Clip {
2424
/// End time in seconds.
2525
public var end: Double!
2626
/// Total clip duration in seconds (end - start).
27+
@available(iOS, deprecated: 9.0, message: "Don't use it when the value is negative, because some information is missing in the original SMIL file. Try to get the duration from file system or APIs in Fetcher, then minus the start value.")
2728
public var duration: Double!
29+
30+
public init() {}
2831
}
2932

3033
/// The Error enumeration of the MediaOverlayNode class.
@@ -39,13 +42,15 @@ public enum MediaOverlayNodeError: Error {
3942
/// Represents a MediaOverlay XML node.
4043
public class MediaOverlayNode {
4144
public var text: String?
42-
public var audio: String?
45+
public var clip: Clip?
46+
4347
public var role = [String]()
4448
public var children = [MediaOverlayNode]()
4549

46-
public init(_ text: String? = nil, audio: String? = nil) {
50+
public init(_ text: String? = nil, clip: Clip? = nil) {
4751
self.text = text
48-
self.audio = audio
52+
self.clip = clip
53+
self.clip?.fragmentId = self.fragmentId()
4954
}
5055

5156
// Mark: - Internal Methods.
@@ -59,53 +64,4 @@ public class MediaOverlayNode {
5964
}
6065
return text.components(separatedBy: "#").last
6166
}
62-
63-
/// Generate a `Clip` from self.
64-
///
65-
/// - Returns: The generated `Clip`.
66-
/// - Throws: `MediaOverlayNodeError.audio`,
67-
/// `MediaOverlayNodeError.timersParsing`.
68-
public func clip() throws -> Clip {
69-
var newClip = Clip()
70-
71-
// Retrieve the audioString (containing timers + audiofile url), then
72-
// retrieve both.
73-
guard let audioString = self.audio,
74-
let audioFileString = audioString.components(separatedBy: "#").first,
75-
let audioFileUrl = URL(string: audioFileString) else
76-
{
77-
throw MediaOverlayNodeError.audio
78-
}
79-
// Relative audio file URL.
80-
newClip.relativeUrl = audioFileUrl
81-
guard let times = audioString.components(separatedBy: "#").last else {
82-
throw MediaOverlayNodeError.timersParsing
83-
}
84-
try parseTimer(times, into: &newClip)
85-
newClip.fragmentId = fragmentId()
86-
return newClip
87-
}
88-
89-
/// Parse the time String to fill `clip`.
90-
///
91-
/// - Parameters:
92-
/// - times: The time string ("t=S.MS,S.MS")
93-
/// - clip: The Clip instance where to fill the parsed data.
94-
/// - Throws: `MediaOverlayNodeError.timersParsing`.
95-
fileprivate func parseTimer(_ times: String, into clip: inout Clip) throws {
96-
var times = times
97-
// Remove "t=" prefix from times string.
98-
times = times.substring(from: times.index(times.startIndex, offsetBy: 2))
99-
// Parse start and end times.
100-
guard let start = times.components(separatedBy: ",").first,
101-
let end = times.components(separatedBy: ",").last,
102-
let startTimer = Double(start),
103-
let endTimer = Double(end) else
104-
{
105-
throw MediaOverlayNodeError.timersParsing
106-
}
107-
clip.start = startTimer
108-
clip.end = endTimer
109-
clip.duration = endTimer - startTimer
110-
}
11167
}

r2-shared-swift/MediaOverlays.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ public class MediaOverlays {
4444
/// - Returns: The `Clip`, representation of the associated SMIL element.
4545
/// - Throws: `MediaOverlayNodeError.audio`,
4646
/// `MediaOverlayNodeError.timersParsing`.
47-
public func clip(forFragmentId id: String) throws -> Clip {
48-
let clip: Clip
47+
public func clip(forFragmentId id: String) throws -> Clip? {
48+
let clip: Clip?
4949

5050
do {
5151
let fragmentNode = try node(forFragmentId: id)
5252

53-
clip = try fragmentNode.clip()
53+
clip = fragmentNode.clip
5454
}
5555
return clip
5656
}
@@ -68,12 +68,12 @@ public class MediaOverlays {
6868
/// one designated by `id`.
6969
/// - Throws: `MediaOverlayNodeError.audio`,
7070
/// `MediaOverlayNodeError.timersParsing`.
71-
public func clip(nextAfterFragmentId id: String) throws -> Clip {
72-
let clip: Clip
71+
public func clip(nextAfterFragmentId id: String) throws -> Clip? {
72+
let clip: Clip?
7373

7474
do {
7575
let fragmentNextNode = try node(nextAfterFragmentId: id)
76-
clip = try fragmentNextNode.clip()
76+
clip = fragmentNextNode.clip
7777
}
7878
return clip
7979
}

0 commit comments

Comments
 (0)