Skip to content

Commit

Permalink
fix video trimmer
Browse files Browse the repository at this point in the history
  • Loading branch information
lihaoyun6 committed Nov 1, 2024
1 parent b57181d commit b788cab
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 23 deletions.
8 changes: 4 additions & 4 deletions QuickRecorder.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@
CODE_SIGN_ENTITLEMENTS = QuickRecorder/QuickRecorder.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 152;
CURRENT_PROJECT_VERSION = 153;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_ASSET_PATHS = "\"QuickRecorder/Preview Content\"";
DEVELOPMENT_TEAM = L4T783637F;
Expand All @@ -462,7 +462,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 12.3;
MARKETING_VERSION = 1.5.2;
MARKETING_VERSION = 1.5.3;
PRODUCT_BUNDLE_IDENTIFIER = com.lihaoyun6.QuickRecorder;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand All @@ -478,7 +478,7 @@
CODE_SIGN_ENTITLEMENTS = QuickRecorder/QuickRecorder.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 152;
CURRENT_PROJECT_VERSION = 153;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_ASSET_PATHS = "\"QuickRecorder/Preview Content\"";
DEVELOPMENT_TEAM = L4T783637F;
Expand All @@ -496,7 +496,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 12.3;
MARKETING_VERSION = 1.5.2;
MARKETING_VERSION = 1.5.3;
PRODUCT_BUNDLE_IDENTIFIER = com.lihaoyun6.QuickRecorder;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down
7 changes: 4 additions & 3 deletions QuickRecorder/QuickRecorderApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, SCStreamDelegate, SCStreamOu
}

func application(_ application: NSApplication, open urls: [URL]) {
print(urls.count)
for url in urls {
createNewWindow(view: VideoTrimmerView(videoURL: url), title: url.lastPathComponent, random: true)
//closeMainWindow()
createNewWindow(view: VideoTrimmerView(videoURL: url), title: url.lastPathComponent, random: true, only: false)
closeMainWindow()
}
}

Expand Down Expand Up @@ -232,7 +233,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, SCStreamDelegate, SCStreamOu
if ud.integer(forKey: "highRes") == 0 { ud.setValue(2, forKey: "highRes") }
if !ud.bool(forKey: "showOnDock") && !ud.bool(forKey: "showMenubar") { ud.setValue(true, forKey: "showOnDock") }
if ud.bool(forKey: "showOnDock") { NSApp.setActivationPolicy(.regular) }
print(SCContext.getDefaultSampleRate())

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
if let error = error { print("Notification authorization denied: \(error.localizedDescription)") }
}
Expand Down
4 changes: 2 additions & 2 deletions QuickRecorder/SCContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ class SCContext {
showNotification(title: "Recording Completed".local, body: String(format: "File saved to: %@".local, url.path), id: "quickrecorder.completed.\(UUID().uuidString)")
if ud.bool(forKey: "trimAfterRecord") {
DispatchQueue.main.async {
AppDelegate.shared.createNewWindow(view: VideoTrimmerView(videoURL: url), title: url.lastPathComponent)
AppDelegate.shared.createNewWindow(view: VideoTrimmerView(videoURL: url), title: url.lastPathComponent, only: false)
}
}
case .failure(let error):
Expand Down Expand Up @@ -448,7 +448,7 @@ class SCContext {
static func trimVideo() {
if ud.bool(forKey: "trimAfterRecord") {
let fileURL = URL(fileURLWithPath: filePath)
AppDelegate.shared.createNewWindow(view: VideoTrimmerView(videoURL: fileURL), title: fileURL.lastPathComponent)
AppDelegate.shared.createNewWindow(view: VideoTrimmerView(videoURL: fileURL), title: fileURL.lastPathComponent, only: false)
}
}

Expand Down
4 changes: 2 additions & 2 deletions QuickRecorder/ViewModel/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,9 @@ extension AppDelegate {
}
}

func createNewWindow(view: some View, title: String, random: Bool = false) {
func createNewWindow(view: some View, title: String, random: Bool = false, only: Bool = true) {
guard let screen = SCContext.getScreenWithMouse() else { return }
closeAllWindow()
if only { closeAllWindow() }
var seed = 0.0
if random { seed = CGFloat(Int(arc4random_uniform(401)) - 200) }
let wX = (screen.frame.width - 780) / 2 + seed + screen.frame.minX
Expand Down
21 changes: 11 additions & 10 deletions QuickRecorder/ViewModel/QmaPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import SwiftUI
struct qmaPlayerView: View {
@Binding var document: qmaPackageHandle
@State var fileURL: URL
@State private var window: NSWindow?
@State private var overPlay: Bool = false
@State private var overStop: Bool = false
@State private var overSave: Bool = false
Expand Down Expand Up @@ -157,7 +156,7 @@ struct qmaPlayerView: View {
audioPlayerManager.sysVol = document.info.sysVol
audioPlayerManager.micVol = document.info.micVol
}
.background(WindowAccessor(window: $window, onWindowOpen: {
.background(WindowAccessor(onWindowOpen: { window in
guard let w = window else { return }
w.setFrame(NSRect(origin: w.frame.origin, size: CGSize(width: 400, height: 100)), display: true)
w.isMovableByWindowBackground = true
Expand Down Expand Up @@ -193,17 +192,20 @@ struct VisualEffectView: NSViewRepresentable {
}

struct WindowAccessor: NSViewRepresentable {
@Binding var window: NSWindow?
var onWindowOpen: () -> Void
var nsWindow: NSWindow? = nil
var onWindowOpen: (NSWindow?) -> Void
var onWindowClose: () -> Void
//var onWindowActive: () -> Void

func makeNSView(context: Context) -> NSView {
let view = NSView()
DispatchQueue.main.async {
self.window = view.window
self.window?.delegate = context.coordinator
self.onWindowOpen()
if let window = view.window {
window.delegate = context.coordinator
self.onWindowOpen(window)
} else {
self.onWindowOpen(nil)
}
}
return view
}
Expand All @@ -215,14 +217,13 @@ struct WindowAccessor: NSViewRepresentable {
}

class Coordinator: NSObject, NSWindowDelegate {
var onWindowOpen: () -> Void
var onWindowOpen: (NSWindow?) -> Void
var onWindowClose: () -> Void
//var onWindowActive: () -> Void

init(onWindowOpen: @escaping () -> Void, onWindowClose: @escaping () -> Void) {
init(onWindowOpen: @escaping (NSWindow?) -> Void, onWindowClose: @escaping () -> Void) {
self.onWindowOpen = onWindowOpen
self.onWindowClose = onWindowClose
//self.onWindowActive = onWindowActive
}

func windowWillClose(_ notification: Notification) { onWindowClose() }
Expand Down
17 changes: 15 additions & 2 deletions QuickRecorder/ViewModel/VideoEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class RecorderPlayerModel: NSObject, ObservableObject {
@Published var playerView: AVPlayerView
var asset: AVAsset?
var fileUrl: URL?
var playerItem: AVPlayerItem!

override init() {
self.playerView = AVPlayerView()
Expand All @@ -19,7 +20,7 @@ class RecorderPlayerModel: NSObject, ObservableObject {
fileUrl = fromUrl
asset = AVAsset(url: fromUrl)
guard let asset = asset else { return }
let playerItem = AVPlayerItem(asset: asset)
playerItem = AVPlayerItem(asset: asset)
playerView.player?.replaceCurrentItem(with: playerItem)
playerView.controlsStyle = .inline

Expand Down Expand Up @@ -99,6 +100,13 @@ class RecorderPlayerModel: NSObject, ObservableObject {
playerItem.removeObserver(self, forKeyPath: #keyPath(AVPlayerItem.status))
}
}

func cleanup() {
// 移除所有观察者
playerItem.removeObserver(self, forKeyPath: #keyPath(AVPlayerItem.status))
playerView.player?.pause()
playerView.player = nil // 移除 player 对象
}
}

struct RecorderPlayerView: NSViewRepresentable {
Expand Down Expand Up @@ -137,7 +145,12 @@ struct VideoTrimmerView: View {
.cornerRadius(5)
)
}.padding([.bottom, .leading, .trailing])
}.padding(.top, -22)
}
.padding(.top, -22)
.background(WindowAccessor(onWindowOpen: {_ in}, onWindowClose: {
playerViewModel.playerView.player?.replaceCurrentItem(with: nil)
playerViewModel.playerView.player = nil
}))
//.navigationTitle(videoURL.lastPathComponent)
//.preferredColorScheme(.dark)
}
Expand Down

0 comments on commit b788cab

Please sign in to comment.