在开始播放之前取消电影

时间:2010-08-08 06:05:26

标签: iphone uibutton mpmovieplayercontroller

我正在将视频流式传输到iPhone,并且不喜欢MPMoviePlayerController在开始时处理事情的方式。我注意到当你点击行来选择一个特定的电影时,应用程序只是坐在那里直到它被充分加载来播放它。所以我在UIImageView中弹出一个加载图像和一个漂亮的微调器来让用户知情。

我认为如果他们厌倦了等待,他们应该可以选择取消电影的播放。所以我有一个带背景图片的取消按钮。

UIButton *cancel;
    UIImage *cancelImage = [UIImage imageNamed:@"cancel.png"];
    cancel = [UIButton buttonWithType:UIButtonTypeCustom];
    cancel.frame = CGRectMake(0,0, cancelImage.size.width, cancelImage.size.height);
    cancel.center = CGPointMake(35, 20); 
    [cancel setImage:cancelImage forState:UIControlStateNormal];
    [cancel addTarget:self action:@selector(cancelMovie) forControlEvents:UIControlEventTouchUpInside];
    [moviePreviewView addSubview:cancel];

但是我不确定在cancelMovie方法中放什么。我试过复制我在moviePlayBackDidFinish回调方法中所拥有的东西,但它崩溃得相当惊人。

我也尝试过:

-(void)cancelMovie {
[theMovie.view removeFromSuperview];
[theMovie release];
}

但这也没有任何好处。我以为我可以直接调用moviePlayBackDidFinish,但我认为你不能直接调用通知方法。

你的想法?

3 个答案:

答案 0 :(得分:2)

我讨厌回答我自己的问题,但由于这个问题只有7个观点,所以我认为这不是那么重要。也许我不应该在半夜发布它们......当你这样做时,没有人能看到它们。

无论如何,我没有找到解决这个问题的好方法......只是一个黑客。

在取消方法中,我告诉moviePlayer加载一个不同的一秒电影,这是我在previewImage中使用的相同照片。当'电影'结束时,然后继续调用MPMoviePlayerPlaybackDidFinishNotification并清理并将用户返回原来的位置。

-(void)cancelMovie
{
cancelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"cancel" ofType:@"m4v"]];

[theMovie setContentURL:cancelURL];

[theMovie setControlStyle:MPMovieControlStyleFullscreen];
[theMovie setFullscreen:YES];

// for some reason ... this is neccessary
[theMovie prepareToPlay];

// Register that the load state changed (movie is ready)
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(moviePlayerLoadStateChanged:) 
                                             name:MPMoviePlayerLoadStateDidChangeNotification 
                                           object:theMovie];

// Register for the PlayBackDidFinish (movie is finished)
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(moviePlayBackDidFinish:) 
                                             name:MPMoviePlayerPlaybackDidFinishNotification 
                                           object:theMovie];    
 }

不好......但它确实有效。

答案 1 :(得分:0)

@Michael解决方案有效。 可以有改进,不需要播放取消视频。相反,您可以直接直接调用 MPMoviePlayerPlaybackDidFinishNotification

[self moviePlayBackDidFinish:nil];

首先我实施了@Michael的想法,但最终,我成功了,没有播放下一个视频。

答案 2 :(得分:0)

-(void)cancelMovie
{
    if (_mediaPlayer){
        [_mediaPlayer stop];
        _mediaPlayer.initialPlaybackTime = -1.0;
        _mediaPlayer.contentURL = nil;
        [_mediaPlayer prepareToPlay];
    }
}
相关问题