以下代码会在单击按钮时打开视频,但是当视频完成时,它不会删除视频。它只是停在最后一帧。收到通知后是否应该将其删除?
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController : UIViewController{
MPMoviePlayerController *moviePlayer;
}
@property (strong, nonatomic) MPMoviePlayerController *moviePlayer;
-(IBAction)playMovie:(id)sender;
- (void)moviePlaybackComplete:(NSNotification *)notification;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize moviePlayer;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *video_btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
video_btn.frame = CGRectMake(0, 0, 100, 50);
[video_btn setTitle:@"Moonrise" forState:UIControlStateNormal];
[video_btn addTarget:self action:@selector(playMovie:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:video_btn];
}
-(IBAction)playMovie:(id)sender
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"moonrise" ofType:@"mp4"]];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
[moviePlayer play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
编辑:
我很想知道这是不是一个错误?这是一个如此简单的功能,所以我不知道怎么会搞砸了。