Skip to content

Commit

Permalink
fix crash
Browse files Browse the repository at this point in the history
  • Loading branch information
kingslay committed Oct 8, 2023
1 parent 936d77a commit be67950
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 45 deletions.
37 changes: 0 additions & 37 deletions Sources/KSPlayer/AVPlayer/KSVideoPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,48 +108,15 @@ extension KSVideoPlayer: UIViewRepresentable {

public var subtitleModel = SubtitleModel()
public var timemodel = ControllerTimeModel()
public var selectedAudioTrack: MediaPlayerTrack? {
didSet {
if oldValue?.trackID != selectedAudioTrack?.trackID {
if let track = selectedAudioTrack {
playerLayer?.player.select(track: track)
playerLayer?.player.isMuted = false
} else {
playerLayer?.player.isMuted = true
}
}
}
}

public var selectedVideoTrack: MediaPlayerTrack? {
didSet {
if oldValue?.trackID != selectedVideoTrack?.trackID {
if let track = selectedVideoTrack {
playerLayer?.player.select(track: track)
playerLayer?.options.videoDisable = false
} else {
oldValue?.isEnabled = false
playerLayer?.options.videoDisable = true
}
}
}
}

// 在SplitView模式下,第二次进入会先调用makeUIView。然后在调用之前的dismantleUIView.所以如果进入的是同一个View的话,就会导致playerLayer被清空了。最准确的方式是在onDisappear清空playerLayer
public var playerLayer: KSPlayerLayer? {
didSet {
oldValue?.delegate = nil
oldValue?.pause()
subtitleModel.url = nil
selectedAudioTrack = nil
selectedVideoTrack = nil
audioTracks.removeAll()
videoTracks.removeAll()
}
}

public var audioTracks = [MediaPlayerTrack]()
public var videoTracks = [MediaPlayerTrack]()
fileprivate var onPlay: ((TimeInterval, TimeInterval) -> Void)?
fileprivate var onFinish: ((KSPlayerLayer, Error?) -> Void)?
fileprivate var onStateChanged: ((KSPlayerLayer, KSPlayerState) -> Void)?
Expand Down Expand Up @@ -209,10 +176,6 @@ extension KSVideoPlayer.Coordinator: KSPlayerLayerDelegate {
public func player(layer: KSPlayerLayer, state: KSPlayerState) {
if state == .readyToPlay {
playbackRate = layer.player.playbackRate
videoTracks = layer.player.tracks(mediaType: .video)
audioTracks = layer.player.tracks(mediaType: .audio)
selectedAudioTrack = audioTracks.first { $0.isEnabled }
selectedVideoTrack = videoTracks.first { $0.isEnabled }
if let subtitleDataSouce = layer.player.subtitleDataSouce {
// 要延后增加内嵌字幕。因为有些内嵌字幕是放在视频流的。所以会比readyToPlay回调晚。
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) { [weak self] in
Expand Down
28 changes: 20 additions & 8 deletions Sources/KSPlayer/SwiftUI/KSVideoPlayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -453,26 +453,38 @@ struct VideoSettingView: View {
} label: {
Label("Playback Speed", systemImage: "speedometer")
}
if !config.audioTracks.isEmpty {

if let audioTracks = config.playerLayer?.player.tracks(mediaType: .audio), !audioTracks.isEmpty {
Picker(selection: Binding {
config.selectedAudioTrack?.trackID
audioTracks.first { $0.isEnabled }?.trackID
} set: { value in
config.selectedAudioTrack = config.audioTracks.first { $0.trackID == value }
if let track = audioTracks.first(where: { $0.trackID == value }) {
config.playerLayer?.player.select(track: track)
config.playerLayer?.player.isMuted = false
} else {
config.playerLayer?.player.isMuted = true
}
}) {
ForEach(config.audioTracks, id: \.trackID) { track in
ForEach(audioTracks, id: \.trackID) { track in
Text(track.description).tag(track.trackID as Int32?)
}
} label: {
Label("Audio track", systemImage: "waveform")
}
}
if !config.videoTracks.isEmpty {

if let videoTracks = config.playerLayer?.player.tracks(mediaType: .video), !videoTracks.isEmpty {
Picker(selection: Binding {
config.selectedVideoTrack?.trackID
videoTracks.first { $0.isEnabled }?.trackID
} set: { value in
config.selectedVideoTrack = config.videoTracks.first { $0.trackID == value }
if let track = videoTracks.first(where: { $0.trackID == value }) {
config.playerLayer?.player.select(track: track)
config.playerLayer?.options.videoDisable = false
} else {
config.playerLayer?.options.videoDisable = true
}
}) {
ForEach(config.videoTracks, id: \.trackID) { track in
ForEach(videoTracks, id: \.trackID) { track in
Text(track.description).tag(track.trackID as Int32?)
}
} label: {
Expand Down

0 comments on commit be67950

Please sign in to comment.