Mpmovieplayer控制器视频暂停,同时单击按钮以获取其他功能

时间:2012-08-23 09:09:52

标签: ios xcode macos mpmovieplayercontroller

我正在使用MPMovieplayerController播放视频,而我点击其他按钮,即我想暂停我的视频,无论它再次播放。我点击播放意味着我想在它停顿时播放。但是现在当我点击按钮意味着我的视频被停止了。但我想暂停而不是停止。     我的示例代码在这里

    - (IBAction) playvideo
{
    NSURL *url = [NSURL URLWithString:@"http://xyz/video1.mp4"];
    movieplayer = [[[MPMoviePlayerController alloc]initWithContentURL:url] retain]; 
movieplayer.view.frame=CGRectMake(25,54,658,460);    
    [self.view addSubview:movieplayer.view];
    [movieplayer play];
}

-(void)buttononclick
{

    [movieplayer pause];
    [movieplayer.view removeFromSuperview];

    for (int i = 0; i < 13; i++)
    {
CGRect frame;
frame.origin.x = 150 * i;
frame.origin.y = 0;
frame.size = CGSizeMake(140, self.scrollView.frame.size.height);
        [scrollView setShowsHorizontalScrollIndicator:NO];



        UIImageView *temp1 = [[UIImageView alloc] initWithFrame:CGRectMake(25, 7, 75, 75)];
        [temp1 setImage:[UIImage imageNamed:@"sti15.png"]];
        [self.scrollView addSubview:temp1];


        UIImageView *temp2 = [[UIImageView alloc] initWithFrame:CGRectMake(110, 7, 75, 75)];
        [temp2 setImage:[UIImage imageNamed:@"sti16.png"]];
        [self.scrollView addSubview:temp2];


        UIImageView *temp3 = [[UIImageView alloc] initWithFrame:CGRectMake(195, 7, 75, 75)];
        [temp3 setImage:[UIImage imageNamed:@"sti17.png"]];
        [self.scrollView addSubview:temp3];

}
    self.scrollView.contentSize = CGSizeMake(165 * 10, self.scrollView.frame.size.height);
    self.scrollView.pagingEnabled=0;
}

- (void)viewDidDisappear:(BOOL)animated
{
   // [self setDescText:nil];
[super viewDidDisappear:animated];
    [movieplayer pause];
    [movieplayer.view removeFromSuperview];
}

3 个答案:

答案 0 :(得分:3)

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ((movie.playbackState==MPMoviePlaybackStateStopped)||(movie.playbackState==MPMoviePlaybackStatePaused)) 
    {
        [movie play];  
    }
    else
    {
        [movie pause];
    }
}

试试此代码,它会起作用

答案 1 :(得分:0)

查看MPMoviePlaybackStatePlayingMPMoviePlaybackStateStoppedMPMoviePlaybackStatePausedMPMoviePlaybackStateInterrupted的通知。

类似的东西:

MPMoviePlayerController *player = notification.object;
/* Playback is currently stopped. */
    if (player.playbackState == MPMoviePlaybackStateStopped)
    {
        NSLog(@"MPMoviePlaybackStateStopped");
    }

/*  Playback is currently under way. */
    else if (player.playbackState == MPMoviePlaybackStatePlaying)
    {        
        NSLog(@"MPMoviePlaybackStatePlaying");
        }

/* Playback is currently paused. */
    else if (player.playbackState == MPMoviePlaybackStatePaused)
    {
        NSLog(@"MPMoviePlaybackStatePaused");
    }

您可以将目标操作连接起来:

if ((_moviePlayer.playbackState == MPMoviePlaybackStateStopped) || (_moviePlayer.playbackState == MPMoviePlaybackStatePaused)) {
            [_moviePlayer play];
        } else {
            [_moviePlayer pause];
        }

答案 2 :(得分:0)

您可以将目标添加到播放/暂停按钮。 但首先你需要抓住mpmovieplayerview的按钮。

步骤1列出按钮。来自here

的方法参考

从按钮出现时调用此方法(视频已准备就绪)。 但请记住,这也将捕捉全屏按钮和播放按钮。(如果可用)

- (void)CatchSubviewsOfView:(UIView *)view {

// Get the subviews of the view
NSArray *subviews = [view subviews];

for (UIView *subview in subviews) {

    // Do what you want to do with the subview
    NSLog(@"%@", subview);
    if(subview isKindOfClass:[UIButton class]]){
        // add your target here
        [subview addTarget:self action:@selector(extraAction:) forControlEvents:UIControlEventTouchUpInside];
    }

    // List the subviews of subview
    [self listSubviewsOfView:subview];
}
}

第2步实施行动

-(IBAction)extraAction:(id)sender{
    NSLog(@"some extraAction");
}

调用catch方法示例。     [self CatchSubviewOfView:movieplayer.view];