Skip to content

Commit

Permalink
fix Seeking through a saved .ts file
Browse files Browse the repository at this point in the history
  • Loading branch information
kingslay committed Mar 10, 2023
1 parent a941b17 commit 2fca7a5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
18 changes: 9 additions & 9 deletions Demo/SwiftUI/Shared/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct ContentView: View {
ForEach(resources.filter { searchText.count == 0 || $0.name.contains(searchText) }, id: \.self) { resource in
NavigationLink(resource.name, destination: KSVideoPlayerView(resource: resource))
}.onDelete { indices in
indices.forEach { self.resources.remove(at: $0) }
indices.forEach { resources.remove(at: $0) }
}
}
.searchable(text: $searchText)
Expand All @@ -34,9 +34,9 @@ struct ContentView: View {
}
}
}.onAppear {
self.loadCachem3u8()
if self.resources.count == 0 {
self.updatem3u8("https://iptv-org.github.io/iptv/index.nsfw.m3u")
loadCachem3u8()
if resources.count == 0 {
updatem3u8("https://iptv-org.github.io/iptv/index.nsfw.m3u")
}
}.sheet(isPresented: $showAddActionSheet) {} content: {
Form {
Expand All @@ -45,9 +45,9 @@ struct ContentView: View {
TextField("play list", text: $playList)
Button("Done") {
if let url = URL(string: playURL.trimmingCharacters(in: NSMutableCharacterSet.whitespacesAndNewlines)) {
self.resources.insert(KSPlayerResource(url: url, options: MEOptions(), name: "new add"), at: 0)
resources.insert(KSPlayerResource(url: url, options: MEOptions(), name: "new add"), at: 0)
} else if !playList.isEmpty {
self.updatem3u8(playList.trimmingCharacters(in: NSMutableCharacterSet.whitespacesAndNewlines))
updatem3u8(playList.trimmingCharacters(in: NSMutableCharacterSet.whitespacesAndNewlines))
}
showAddActionSheet = false
}
Expand All @@ -71,7 +71,7 @@ struct ContentView: View {
guard let string = String(data: data, encoding: .utf8) else {
return
}
self.resources.append(contentsOf: parsem3u8(string: string))
resources.append(contentsOf: parsem3u8(string: string))
} catch {}
}
}
Expand All @@ -85,8 +85,8 @@ struct ContentView: View {
guard let data, let string = String(data: data, encoding: .utf8) else {
return
}
self.saveToDocument(data: data, filename: "cache.m3u8")
self.resources.append(contentsOf: self.parsem3u8(string: string))
saveToDocument(data: data, filename: "cache.m3u8")
resources.append(contentsOf: parsem3u8(string: string))
}.resume()
}

Expand Down
6 changes: 3 additions & 3 deletions Demo/demo-iOS/demo-iOS/DetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ class DetailViewController: UIViewController, DetailProtocol {
playerView.backBlock = { [unowned self] in
#if os(iOS)
if UIApplication.shared.statusBarOrientation.isLandscape {
self.playerView.updateUI(isLandscape: false)
playerView.updateUI(isLandscape: false)
} else {
self.navigationController?.popViewController(animated: true)
navigationController?.popViewController(animated: true)
}
#else
self.navigationController?.popViewController(animated: true)
navigationController?.popViewController(animated: true)
#endif
}
playerView.becomeFirstResponder()
Expand Down
4 changes: 2 additions & 2 deletions Sources/KSPlayer/AVPlayer/PlayerDefines.swift
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ open class KSOptions {
AVSEEK_FLAG_ANY: 4
AVSEEK_FLAG_FRAME: 8
*/
public var seekFlags = Int32(1)
public var seekFlags = Int32(0)
// ffmpeg only cache http
public var cache = false
public var outputURL: URL?
Expand Down Expand Up @@ -599,7 +599,7 @@ public extension KSOptions {
/// 是否开启秒开
static var isSecondOpen = false
/// 开启精确seek
static var isAccurateSeek = true
static var isAccurateSeek = false
/// Applies to short videos only
static var isLoopPlay = false
/// 是否自动播放,默认false
Expand Down
4 changes: 2 additions & 2 deletions Sources/KSPlayer/MEPlayer/MEPlayerItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,13 @@ extension MEPlayerItem {
let timeStamp = Int64(time * TimeInterval(AV_TIME_BASE))
// can not seek to key frame
// let result = avformat_seek_file(formatCtx, -1, Int64.min, timeStamp, Int64.max, options.seekFlags)
let result = av_seek_frame(formatCtx, -1, timeStamp, options.seekFlags)
var result = av_seek_frame(formatCtx, -1, timeStamp, options.seekFlags)
// When seeking before the beginning of the file, and seeking fails,
// try again without the backwards flag to make it seek to the
// beginning.
if result < 0, options.seekFlags & AVSEEK_FLAG_BACKWARD > 0 {
options.seekFlags &= ~AVSEEK_FLAG_BACKWARD
av_seek_frame(formatCtx, -1, timeStamp, options.seekFlags)
result = av_seek_frame(formatCtx, -1, timeStamp, options.seekFlags)
}
if state == .closed {
break
Expand Down
9 changes: 6 additions & 3 deletions Sources/KSPlayer/Video/VideoPlayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -440,19 +440,22 @@ extension VideoPlayerView {

let videoTracks = playerLayer?.player.tracks(mediaType: .video) ?? []
let videoMenu = KSMenuBuilder.audioVideoChangeMenu(videoTracks.first(where: { $0.isEnabled }),
availableTracks: videoTracks) { [weak self] track in
availableTracks: videoTracks)
{ [weak self] track in
self?.playerLayer?.player.select(track: track)
}

let audioTracks = playerLayer?.player.tracks(mediaType: .audio) ?? []
let audioMenu = KSMenuBuilder.audioVideoChangeMenu(audioTracks.first(where: { $0.isEnabled }),
availableTracks: audioTracks) { [weak self] track in
availableTracks: audioTracks)
{ [weak self] track in
self?.playerLayer?.player.select(track: track)
}

let subtitles = srtControl.filterInfos { _ in true }
let srtMenu = KSMenuBuilder.srtChangeMenu(srtControl.view.selectedInfo,
availableSubtitles: subtitles) { [weak self] selectedSrt in
availableSubtitles: subtitles)
{ [weak self] selectedSrt in
guard self?.srtControl.view.selectedInfo?.subtitleID != selectedSrt?.subtitleID else { return }
self?.srtControl.view.selectedInfo = selectedSrt
}
Expand Down

0 comments on commit 2fca7a5

Please sign in to comment.