-
-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix crash on attaching metadata output #74
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've left some comments / thoughts. Overall looks good to me though, thanks for this @Fonos-development
@@ -47,7 +47,6 @@ class AVPlayerItemObserver: NSObject { | |||
|
|||
override init() { | |||
super.init() | |||
metadataOutput.setDelegate(self, queue: .main) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can also remove the private let metadataOutput = AVPlayerItemMetadataOutput()
above right?
// Create and add a new metadata output to the item. | ||
let metadataOutput = AVPlayerItemMetadataOutput() | ||
metadataOutput.setDelegate(self, queue: .main) | ||
item.add(metadataOutput) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder whether we need to add a check in the delegate method func metadataOutput()
to make sure we're receiving metadata from the "currently focused' item and output before we broadcast it. We could still do it by keeping a reference to the latest created output currentMetadataOutput
and comparing the output given in the delegate to it.
I think once we reassign currentMetadataOutput
any old metadata outputs will keep hanging in memory until we call remove(metadataOutput)
in the stopObservingCurrentItem
. We should probably do a small check on that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated, thanks for your advice. Kindly have a look at the new commit.
observingItem.remove(metadataOutput) | ||
|
||
// Remove metadata output if it's attached. | ||
if let metadataOutput = observingItem.outputs.first(where: { $0 is AVPlayerItemMetadataOutput }) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is clever. We could probably put this in an extension and remove all outputs for safety.
extension AVPlayerItem {
func removeAllMetadataOutputs() {
for output in self.outputs.filter({ $0 is AVPlayerItemMetadataOutput }) {
self.remove(output)
}
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍 thanks for this @Fonos-development
The issue: doublesymmetry/react-native-track-player#1818
The previous fix: #68
The previous attempt to fix it was not ideal cause we don't know the exact observation removal time.
My approach: