Improve MPV loading commands

This commit is contained in:
Arkadiusz Fal 2022-06-07 23:20:24 +02:00
parent b90a726f87
commit 09c0d8277f
2 changed files with 50 additions and 6 deletions

View File

@ -168,12 +168,15 @@ final class MPVBackend: PlayerBackend {
self?.isLoadingVideo = true self?.isLoadingVideo = true
} }
} else { } else {
self.onFileLoaded = { [weak self] in self.onFileLoaded = {
updateCurrentStream() updateCurrentStream()
startPlaying() startPlaying()
} }
self.client.loadFile(stream.videoAsset.url, audio: stream.audioAsset.url, time: time) { [weak self] _ in let fileToLoad = self.model.musicMode ? stream.audioAsset.url : stream.videoAsset.url
let audioTrack = self.model.musicMode ? nil : stream.audioAsset.url
self.client.loadFile(fileToLoad, audio: audioTrack, time: time) { [weak self] _ in
self?.isLoadingVideo = true self?.isLoadingVideo = true
self?.pause() self?.pause()
} }
@ -386,4 +389,16 @@ final class MPVBackend: PlayerBackend {
func setSize(_ width: Double, _ height: Double) { func setSize(_ width: Double, _ height: Double) {
self.client?.setSize(width, height) self.client?.setSize(width, height)
} }
func addVideoTrack(_ url: URL) {
self.client?.addVideoTrack(url)
}
func setVideoToAuto() {
self.client?.setVideoToAuto()
}
func setVideoToNo() {
self.client?.setVideoToNo()
}
} }

View File

@ -121,10 +121,6 @@ final class MPVClient: ObservableObject {
command("loadfile", args: args, returnValueCallback: completionHandler) command("loadfile", args: args, returnValueCallback: completionHandler)
} }
func addAudio(_ url: URL, completionHandler: ((Int32) -> Void)? = nil) {
command("audio-add", args: [url.absoluteString], returnValueCallback: completionHandler)
}
func play() { func play() {
setFlagAsync("pause", false) setFlagAsync("pause", false)
} }
@ -228,6 +224,22 @@ final class MPVClient: ObservableObject {
} }
} }
func addVideoTrack(_ url: URL) {
command("video-add", args: [url.absoluteString])
}
func setVideoToAuto() {
setString("video", "auto")
}
func setVideoToNo() {
setString("video", "no")
}
var tracksCount: Int {
Int(getString("track-list/count") ?? "-1") ?? -1
}
private func setFlagAsync(_ name: String, _ flag: Bool) { private func setFlagAsync(_ name: String, _ flag: Bool) {
var data: Int = flag ? 1 : 0 var data: Int = flag ? 1 : 0
mpv_set_property_async(mpv, 0, name, MPV_FORMAT_FLAG, &data) mpv_set_property_async(mpv, 0, name, MPV_FORMAT_FLAG, &data)
@ -244,6 +256,23 @@ final class MPVClient: ObservableObject {
return data return data
} }
private func getInt(_ name: String) -> Int {
var data = Int64()
mpv_get_property(mpv, name, MPV_FORMAT_INT64, &data)
return Int(data)
}
func getString(_ name: String) -> String? {
let cstr = mpv_get_property_string(mpv, name)
let str: String? = cstr == nil ? nil : String(cString: cstr!)
mpv_free(cstr)
return str
}
private func setString(_ name: String, _ value: String) {
mpv_set_property_string(mpv, name, value)
}
private func makeCArgs(_ command: String, _ args: [String?]) -> [String?] { private func makeCArgs(_ command: String, _ args: [String?]) -> [String?] {
if !args.isEmpty, args.last == nil { if !args.isEmpty, args.last == nil {
fatalError("Command do not need a nil suffix") fatalError("Command do not need a nil suffix")