我使用MPMoviePlayerViewController在我的ios应用中播放视频。我希望在视频暂停时在播放器上方显示一些广告(图片)。我使用方法here。
然而,当我暂停视频时,没有任何内容显示,我的代码如下:
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()
{
@private
MPMoviePlayerController *player;
UIImageView *image;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 100, 100, 50);
[button setTitle:@"play" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
NSURL *videoStreamURL = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
player = [[MPMoviePlayerController alloc]initWithContentURL:videoStreamURL];
player.view.frame = CGRectMake(0, 0, self.view.frame.size.width, 320);
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playbackStateChanged)
name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void) buttonClick{
[self.view addSubview:player.view];
[player play];
}
- (void)playbackStateChanged{
switch (player.playbackState) {
case MPMoviePlaybackStatePaused:
//add ads
image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ad.jpg"]];
image.frame = CGRectMake(0, 0, self.view.frame.size.width, 320);
[player.view addSubview:image];
break;
default:
break;
}
}
@end
任何人都可以帮助我吗?谢谢!