I use MPMoviePlayerViewController to play video in my ios app. I want to show some ads(pictures) above the player when the video is paused. I used method here.
However, nothing shows up when I pause the video, my code is below:
#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
Can anybody help me? Thanks!