更改MPMoviePlayerController内容不与performSelectorInBackground一起使用

时间:2014-09-23 06:30:38

标签: ios objective-c multithreading

我正在尝试使用在后台运行的函数更改MPMoviePlayerController方法的内容。但我的内容未得到更改

[self performSelectorInBackground:@selector(updateVideo) withObject:nil];

这是我开始加载视频的 viewDidLoad方法

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    VideoView *videoView = [[VideoView alloc] initWithFrame:CGRectMake(0, 0, 293, 320)];
    NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"My Movie4" ofType:@"mp4"];
    NSURL *videoURL = [NSURL fileURLWithPath:videoPath];
    controller = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
    [controller.view setFrame:videoView.bounds];
    [videoView addSubview:controller.view];
    [self.videoviewer addSubview: videoView];
    [controller prepareToPlay];
    [controller play];
}

其中VideoView是UIView的子类。我的updateVideo函数是这样的。

-(void)updateVideo
{
[controller setContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"video2" ofType:@"mp4"] ]];
[controller prepareToPlay];
[controller play];
}

但调用在后台运行的函数时,视频内容不会改变。

1 个答案:

答案 0 :(得分:2)

您为什么要尝试更改background thread中的内容?

无法更改background thread中的UI元素。您必须在main thread

中执行此任务

尝试这样做:

[self updateVideo]

如果你愿意,可以这样做,但我建议你使用第一种方法。

-(void)updateVideo
{
    [controller setContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"video2" ofType:@"mp4"] ]];
    dispatch_async(dispatch_get_main_queue(), ^{
        [controller prepareToPlay];
        [controller play];
    });
}

我希望这可以帮到你