使用 MPMoviePlayer 播放视频

Posted

技术标签:

【中文标题】使用 MPMoviePlayer 播放视频【英文标题】:Playing Videos using MPMoviePlayer 【发布时间】:2011-09-22 08:12:35 【问题描述】:

现在我在这里提出与MPMoviePlayer 相关的新问题。我有一个表格视图,其中列出了 ipad 一侧的视频,另一侧 - 我播放列表中选择的视频。这些发生在同一个视图中。现在切换到不同的视频效果很好。现在,当我使用UIVideoEditorController 编辑视频时,我正在替换文档文件夹中的当前视频文件,然后再次播放视频。但后来我的播放器停止工作......

我认为问题在于UIVideoEditorController 中的播放器未正确释放。我发布了编辑器,但我仍然只在那里.. 没有任何作用:(

请尽快帮助我

-(void)playVideoWithIndex:(NSNumber *)index1

int index = [index1 intValue];
indexVideo = [index1 intValue];
if(index > [SourceArray count])
    NSLog(@"dg");
    return;


if (mp) 
    [mp stop];
    [mp.view removeFromSuperview];
    [mp release];
    mp=nil;

//  NSArray *sourcePaths=[[NSArray alloc] initWithArray:[[NSBundle mainBundle] pathsForResourcesOfType:@"mov" inDirectory:nil]];
videoDtls *v =[SourceArray objectAtIndex:index];
lbl_date.text=v.iVideoDate;
lbl_Title.text=v.iVideoTitle;
lbl_description.text=v.iVideoDesc;
lblDesc.text = @"Description";

lable_title.text = v.iVideoTitle;
NSString *documentFolderPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *mainImageFolderPath=[documentFolderPath stringByAppendingPathComponent:@"Videos"];
NSString *urlStr  = [mainImageFolderPath stringByAppendingPathComponent:v.iVideoPath];
self.vpath = urlStr;
NSLog(@"asdsd %@",urlStr);
NSURL *url = [NSURL fileURLWithPath:urlStr];


mp =  [[MPMoviePlayerController alloc] initWithContentURL:url];

if ([mp respondsToSelector:@selector(loadState)]) 

    // Set movie player layout
    //[mp setControlStyle:MPMovieControlStyleFullscreen];

    //[mp setFullscreen:YES];
    // May help to reduce latency
    [mp prepareToPlay];
    mp.useApplicationAudiosession = NO;
//  mp.controlStyle = MPMovieControlStyleDefault;
//  mp.useApplicationAudioSession = YES;
    // Register that the load state changed (movie is ready)
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(moviePlayerLoadStateChanged:) 
                                                 name:MPMoviePlayerLoadStateDidChangeNotification 
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(moviePlayerEnterFullScreen:) 
                                                 name:MPMoviePlayerDidEnterFullscreenNotification 
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(moviePlayerExitFullScreen:) 
                                                 name:MPMoviePlayerWillExitFullscreenNotification 
                                               object:nil];
    [[NSNotificationCenter defaultCenter] 
     addObserver:self 
     selector:@selector(moviePlayerDidFinish:) 
     name:MPMoviePlayerPlaybackDidFinishNotification 
     object:nil];

    [[mp view] setFrame:CGRectMake(0, 0, 480, 320)];

    // Add movie player as subview
    [playerView addSubview:[mp view]];   
    mp.initialPlaybackTime = -1.0;




  
else

    // Register to receive a notification when the movie is in memory and ready to play.
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(moviePreloadDidFinish:) 
                                                 name:MPMoviePlayerContentPreloadDidFinishNotification 
                                               object:nil];


// Register to receive a notification when the movie has finished playing. 
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(moviePlayBackDidFinish:) 
                                             name:MPMoviePlayerPlaybackDidFinishNotification 
                                           object:nil];

从表格视图播放视频时调用此方法..

- (void)videoEditorController:(UIVideoEditorController *)editor didSaveEditedVideoToPath:(NSString *)editedVideoPath

CFShow(editedVideoPath);
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSError *error = nil;
NSString *path = self.vpath;
NSLog(@"PATH %@", self.vpath);
if([fileManager removeItemAtPath:path error:&error] != YES)
    NSLog(@"ERROR 1 : %@",[error localizedDescription]);

else
// can do save here. the data has *not* yet been saved to the photo album
if ([fileManager copyItemAtPath:editedVideoPath toPath:path error:&error] != YES)
    NSLog(@"Can't move file with error: %@", [error localizedDescription]);

[popOver dismissPopoverAnimated:YES];
[self dismissMoviePlayerViewControllerAnimated];
[editor release];
[self performSelector:@selector(playVideoWithIndex:) withObject:[NSNumber numberWithInt:indexVideo] afterDelay:0.2];

编辑器返回成功时调用该方法。 在此之后我的视频无法播放

【问题讨论】:

【参考方案1】:

最后我通过在另一个视图控制器中使用 UIVideoEditorController 解决了这个问题,然后延迟播放视频几分之一秒。这可能是因为我试图播放被替换的视频。

【讨论】:

以上是关于使用 MPMoviePlayer 播放视频的主要内容,如果未能解决你的问题,请参考以下文章

avaudioplayer 干扰 ipad 上的 mpmovieplayer

IPHONE MPMoviePlayer:在播放视频时可以交互的按钮层下播放视频?

无法在 MPMoviePlayer 中播放视频?

如何知道 youtube 视频何时开始播放以及何时在 ios 的 web 视图中按下 mpmovieplayer 的完成按钮

iPhone开发mpmovieplayer崩溃

在没有 UIWebView 或 MPMoviePlayer 的情况下播放 .mov 文件