Hi, @breq
The ItemModel above is almost right, except there is no need to store the AVPlayer in it.
You can instantiate a single AVPlayer on app init, and set it as the player in a SwiftUI VideoPlayer instance.
Then, when you want to play a certain video, you create an AVAsset using the URL in the Item and set it as the AVPlayer current item using:
let asset = AVAsset(url: item.url)
let playerItem = AVPlayerItem(asset: asset)
player.replaceCurrentItem(with: playerItem)
As for knowing when it pauses and plays, you can use a rate change observer in the class holding your AVPlayer like this:
let name = AVPlayer.rateDidChangeNotification
for await _ in NotificationCenter.default.notifications(named: name) {
// in here you can read the player.rate, player.currentTime() and do your processing
}
Just remember that when the player is paused, it's rate == 0.0.
Also, you should never set the rate directly. Use .pause() and .play() instead because the user may have chosen a different playing speed on the UI...
Happy coding!
Post
Replies
Boosts
Views
Activity
Hi!
Just in case this helps others having the same issue in Mac Catalyst:
I was having a very similar issue. The frameworks were there, and I followed the instructions above on the xcarchive app. The only thing missing was the LC_ID_DYLIB, which returned empty.
In the end, I had deleted the Hardened Routine capability (https://developer.apple.com/documentation/security/hardened_runtime) from my (initially multi-platform) target when I found out some of my code would only run under Mac Catalyst and deleted macOS support to include it in the target.
My frameworks were embedding and running fine on other platforms. It was only on Mac that the archived / TestFlight version was crashing and reporting missing frameworks with the following:
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Termination Reason: Namespace DYLD, Code 1 Library missing
Library not loaded: @rpath/[framework_name_deleted].framework/Versions/A/[framework_name_deleted]
Referenced from: <[uuid_deleted]> /Applications/[app_name_deleted]/Contents/MacOS/[app_name_deleted]
Reason: , (security policy does not allow @ path expansion)
(terminated at launch; ignore backtrace)
Including the Hardened Routine back (with no exceptions in my case) made it run perfectly in TestFlight
I have reported this to Apple during WWDC'21 and have not touched the code since then. I might be working now, or not, I'd have to check...
What do you need exactly, @TikiMcFee ? Maybe I can give you some code examples 😀
Worked for me as well, thanks!! Congratulations, Gian Enrico @ingconti and grazie mille!
PS: Curious thing is, my build was working in Xcode 11, and broke on Xcode 12.
Hi, cederachBiB,Did a small change to your code that I'd like to share back. I was having problems with TextFieldWithFocus filling up too much space when I wanted a single-line TextField similar to SwiftUI's.I tried adding a .frame(height: ) to my TextFieldWithFocus element, but it crashed the app. So I added this code to the end of the properties and beginning of the makeUIView(context:) var enablesReturnKeyAutomatically: Bool = false
@State var frameRect: CGRect = .zero
var onCommit: (() -> Void)?
func setFrameRect(_ rect: CGRect) -> some View {
self.frameRect = rect
return self
}
func makeUIView(context: UIViewRepresentableContext) -> UITextField {
let textField = UITextField(frame: self.frameRect)
textField.delegate = context.coordinator
textField.placeholder = NSLocalizedString(placeholder, comment: "")And by using a GeometryReader around the element (or elements in a HStack), it's now possbile to set the frame this way, for example:GeometryReader { geometry in
HStack {
TextFieldWithFocus(text: self.$newString, placeholder: "placeholder", isFirstResponder: self.$textHasFocus,
onCommit: { UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil,
from: nil, for: nil)
self.textHasFocus = false
})
.setFrameRect(geometry.frame(in: .global))
.frame(height: 36)
.border(Color.black, width: 1)
.padding(.horizontal)
...
}Hope this can be of use, and thanks again for sharing your fix to our problem!Best, Tarq
Thank you for sharing, cederacheBiB 😀
I'm having the same problem here. It captures 2-3 letters, then disconnects...
Thanks, you're the best!i Will do that 😊
Thanks for the prompt answer, eskimo,Im a big fan of yours, BTW! See really helpful answers from you all the time 😎One litlte extra help, if you can: most SwiftNIO tutorials I found so far seem to focus only on HTTPS post and/or websockets. Can you suggest any tutorials on SwiftNIO for data streams? Something like WWDC's Tic-Tac-Toe example would be perfect, it really helped along with my coding!Best, Tarq