gpt4 book ai didi

ios - __NSCFString playbackFinished 来自 MPMoviePlayerController 的无法识别的选择器

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:02:53 24 4
gpt4 key购买 nike

我在一个名为 MYVideo 的类中使用 MPMoviePlayerController。这是代码:

#import <MediaPlayer/MediaPlayer.h>
#import "MYVideo.h"

@interface MYVideo()
@property (strong, nonatomic) UIView * viewRef;
@property (strong, nonatomic) NSDictionary * contentData;
@property (strong, nonatomic) MPMoviePlayerController * videoController;
@end

@implementation MYVideo
@synthesize contentData,videoController,viewRef;

- (MYVideo*) initIntoView: (UIView*) view withContent:(NSDictionary*)contentDict{
self=[super init];
viewRef=view;
contentData = contentDict;
NSString *rawUrl = [[NSString alloc] initWithFormat:@"http://....com/app/%@.mp4", [contentDict objectForKey:@"cnid"]];
NSURL *videoUrl = [[NSURL alloc]initWithString:rawUrl];
videoController = [[MPMoviePlayerController alloc] initWithContentURL:videoUrl];
videoController.movieSourceType=MPMovieSourceTypeFile;
videoController.view.frame = viewRef.bounds;
[videoController.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
videoController.controlStyle=MPMovieControlStyleNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playbackFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:videoController];
[viewRef addSubview:videoController.view];
return self;
}

- (void) playbackFinished: (NSNotification*) notification {
NSLog(@"playback finished");
if(videoController){
[videoController play];
}
}

- (void) play: (int) offset {
videoController.initialPlaybackTime=offset;
[videoController play];
}

- (void) stop {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:@"playbackFinished"
object:nil];
if(videoController){
[videoController stop];
}
}

- (void) destroy {
if(videoController){
[videoController stop];
[videoController.view removeFromSuperview];
}
}
@end

我的问题是偶尔会出现以下错误:

playback finished
-[__NSCFString playbackFinished:]: unrecognized selector sent to instance 0x1664e6a0

我猜这是由于 MPMoviePlayerController 在该视频类已发布时触发“playbackFinished”通知引起的。我这样想对吗?

事情是,这个 MYVideo 类应该在视频播放时仍然存在,这个错误只发生在视频正在播放并且在控制台日志中我的“播放完成”的 NSLogging 立即在碰撞。此外,我从来没有在没有先删除“playbackFinished”观察者的情况下关闭类(class)。

谁能告诉我为什么我会遇到这个崩溃?

非常感谢。

最佳答案

是的,看起来你没有移除观察者,因为这段代码:

[[NSNotificationCenter defaultCenter] removeObserver:self
name:@"playbackFinished"
object:nil];

应该是:

[[NSNotificationCenter defaultCenter] removeObserver:self
name:@"playbackFinished:"
object:nil]; // ^

或者更好(因为你并不真正关心被称为什么):

[[NSNotificationCenter defaultCenter] removeObserver:self
name:nil
object:videoController];

同时考虑到您在很多地方使用了像 if (videoController) { ... } 这样的测试,您需要确保它尽快变为 nil:

- (void)destroy {
if(videoController){
[videoController stop];
[videoController.view removeFromSuperview];
videoController = nil; // Add
}
}

关于ios - __NSCFString playbackFinished 来自 MPMoviePlayerController 的无法识别的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19497880/

24 4 0
文章推荐: iphone - iOS 7 是否仍然需要没有 alpha、没有圆角和没有文件扩展名的 iTunesArtwork 图像?
文章推荐: java - Stream 到 InputStream