diff --git a/kplayer.xcodeproj/project.pbxproj b/kplayer.xcodeproj/project.pbxproj index ad6048f..d644e40 100644 --- a/kplayer.xcodeproj/project.pbxproj +++ b/kplayer.xcodeproj/project.pbxproj @@ -25,6 +25,7 @@ 1C7365C59F72C29EA41C8717 /* SVideoModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C736EEC570C71AAC50F2E95 /* SVideoModel.swift */; }; 1C7365CE76693E7772585CA8 /* SVideoPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C73621E431C9BEC1440B936 /* SVideoPlayer.swift */; }; 1C73666A07CF2416B1B8D3F0 /* KSettingsModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C736C94157754DE1C808173 /* KSettingsModel.swift */; }; + 1C736690D123BD4B24874394 /* pathfinder.scpt in Sources */ = {isa = PBXBuildFile; fileRef = 1C7369BED02028D8564E82D5 /* pathfinder.scpt */; }; 1C7366A0CFD2B55BF8C3BAF0 /* NetworkDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C7364F10BED5DA0F1C0423C /* NetworkDelegate.swift */; }; 1C7367084839D2E8B180DB74 /* UIViewController+Alert.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C7360295486647982CFEACF /* UIViewController+Alert.swift */; }; 1C73671FC2CCCACAA2FFC153 /* ThumbnailCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C736EA15A11AF7D57F85824 /* ThumbnailCache.swift */; }; @@ -120,6 +121,7 @@ 1C7367ECBD369A2A0C94C499 /* FileHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileHelper.swift; sourceTree = ""; }; 1C73685B4BBFDAFBF08C032C /* readme.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = readme.md; sourceTree = ""; }; 1C73688DAB88F9360FB62A49 /* MediaItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MediaItem.swift; sourceTree = ""; }; + 1C7369BED02028D8564E82D5 /* pathfinder.scpt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.scpt; path = pathfinder.scpt; sourceTree = ""; }; 1C7369EC16B19B32B515169E /* NetData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetData.swift; sourceTree = ""; }; 1C7369F53095B7A4D65679C2 /* DetailViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DetailViewController.swift; sourceTree = ""; }; 1C736A6E8396EE306B1AD3A8 /* KSettingsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KSettingsView.swift; sourceTree = ""; }; @@ -312,6 +314,7 @@ 8052F5B3AAC2535E5C08A529 /* Pods */, 1C73685B4BBFDAFBF08C032C /* readme.md */, 1C7363E0DDA5854D55F8836E /* scratch.txt */, + 1C7369BED02028D8564E82D5 /* pathfinder.scpt */, ); sourceTree = ""; }; @@ -588,6 +591,7 @@ 1C7367084839D2E8B180DB74 /* UIViewController+Alert.swift in Sources */, 1C7363C2E744C2318879127D /* FlexibleView.swift in Sources */, 1C736C8DAD6C2FBB9A2EA625 /* SearchItemView.swift in Sources */, + 1C736690D123BD4B24874394 /* pathfinder.scpt in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/kplayer/video/SVideoPlayer.swift b/kplayer/video/SVideoPlayer.swift index 753b3ec..b021b9c 100644 --- a/kplayer/video/SVideoPlayer.swift +++ b/kplayer/video/SVideoPlayer.swift @@ -6,12 +6,14 @@ import Foundation import SwiftUI import AVKit +import CoreMotion struct SVideoPlayer: View, EditItemDelegate { // url: URL(string: "https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8")! var player = AVQueuePlayer(items: [AVPlayerItem]()) var playerLooper : AVPlayerLooper var completionHandler: ((Bool) -> Void)? + let motionManager = CMMotionManager() @ObservedObject var model: SVideoModel @@ -22,9 +24,13 @@ struct SVideoPlayer: View, EditItemDelegate { @State var savetext = "save" @State var confirmationShown = false @State var seekSmoothly = false + @State var upsidedown = false + @State var tilt = false @State var smoothTime = -1.0 @State var smoothSeekTime = -1.0 @State var timeCounter = 0 + @State var xoffs = 0.0 + @State var rotazero = -1000.0 @State var orientation = UIDevice.current.orientation @@ -74,6 +80,22 @@ struct SVideoPlayer: View, EditItemDelegate { Text("zoom") }) .foregroundColor(model.zoomed ? Color.yellow : Color.blue).buttonStyle(BorderlessButtonStyle()) + Button(action: { + upsidedown.toggle() + }, label: { + Text("flip") + }).foregroundColor(upsidedown ? Color.yellow : Color.blue).buttonStyle(BorderlessButtonStyle()) + Button(action: { + tilt.toggle() + if tilt { + motionManager.startDeviceMotionUpdates(using: .xMagneticNorthZVertical) + } + else { + motionManager.stopDeviceMotionUpdates() + } + }, label: { + Text("tilt") + }).foregroundColor(tilt ? Color.yellow : Color.blue).buttonStyle(BorderlessButtonStyle()) Button(action: { model.favorite.toggle() }, label: { @@ -149,7 +171,8 @@ struct SVideoPlayer: View, EditItemDelegate { let v = VideoPlayerView(model: model, player: player) .scaleEffect(model.scale) - .offset(model.dragOffset) + .rotation3DEffect(.degrees(upsidedown ? 180 : 0), axis: (x: 1, y: 0, z: 0)) + .offset(model.dragOffset).offset(x: xoffs, y:0) .gesture( DragGesture() .onChanged { gesture in @@ -195,7 +218,7 @@ struct SVideoPlayer: View, EditItemDelegate { } .onAppear() { - model.observer = player.addPeriodicTimeObserver(forInterval: CMTime(seconds: 0.1, preferredTimescale: 600), queue: nil) { time in + model.observer = player.addPeriodicTimeObserver(forInterval: CMTime(seconds: 0.02, preferredTimescale: 600), queue: nil) { time in if timeCounter >= 1 { timeCounter -= 1 } @@ -204,7 +227,36 @@ struct SVideoPlayer: View, EditItemDelegate { seekTime(model.currentSnapshot.time) } } + if tilt { + if let data = motionManager.deviceMotion { + var rotation = atan2(data.gravity.x, + data.gravity.y) - .pi + if rotation < (-1 * .pi) { + rotation += .pi + .pi + } + rotation *= 100.0 + if rotazero == -1000 { + rotazero = rotation + } + + rotation -= rotazero + + // let roll = accelerometerData.attitude.yaw + if rotation > 30 { + xoffs += 3; + // print(xoffs) + } + if rotation < -30 { + xoffs -= 3; + // print(xoffs) + } + // print(Int(rotation * 100.0) ) + }} + else { + xoffs = 0.0 + rotazero = -1000 + } } player.automaticallyWaitsToMinimizeStalling = false