Well, I want to show a video ad just before starting a video in MPMoviePlayer
This is what I am doing:-
moviePlayer = [MPMoviePlayerController new];
moviePlayer.contentURL = [NSURL URLWithString:#"http://xyz/xyz.m3u8"];
[moviePlayer playPrerollAdWithCompletionHandler:^(NSError *error) {
// Check if error is non-nil during development
[moviePlayer play];
}];
moviePlayer.view.frame=CGRectMake(0, 20, 300, self.view.frame.size.width);
[self.view addSubview:moviePlayer.view];
[self.view layoutIfNeeded];
And in Appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[MPMoviePlayerController preparePrerollAds];
return YES;
}
But what more should I do to show an ad video or how to configure the iAD to let the app understand which specific video to play as ad.
Currently the app is just playing this url "http://xyz/xyz.m3u8" video but not showing any ad.
You need to prepareToPlay your MPMoviePlayerController before calling playPrerollAdWithCompletionHandler and add your MPMoviePlayerController to your view before you play it. Also, you may be playing the video before [MPMoviePlayerController preparePrerollAds] has a chance to download the video ad completely. Check my example:
#import "ViewController.h"
#import iAd;
#import MediaPlayer;
#interface ViewController () {
MPMoviePlayerController *moviePlayer;
}
#end
#implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
// Preload ad
[MPMoviePlayerController preparePrerollAds];
// Create our MPMoviePlayerController
moviePlayer =[[MPMoviePlayerController alloc]init];
[moviePlayer.view setFrame: self.view.bounds];
[moviePlayer setFullscreen:YES animated:YES];
}
-(IBAction)playButton:(id)sender {
// Add MPMoviePlayerController to our view
[self.view addSubview:moviePlayer.view];
// Path of movie you want to play
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"someVideo" ofType:#"MOV"];
// Set the contents of our MPMoviePlayerController to our path
[moviePlayer setContentURL:[NSURL fileURLWithPath:moviePath]];
// Prepare our movie for playback
[moviePlayer prepareToPlay];
// Play our movie with a prerolled ad
[moviePlayer playPrerollAdWithCompletionHandler:^(NSError *error) {
[moviePlayer play];
}];
}
Related
I am using MPMoviePlayerViewController to play video from the server.
#property (strong, nonatomic) MPMoviePlayerViewController *videoPlayer;
When i restart the video i get EXC_BAD_ACCESS (code=1, address=0xc000000c)...
_videoPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:[NSString ... ]]];
[self presentMoviePlayerViewControllerAnimated:_videoPlayer];
How can i fix it?
i did use it to as present ..
but you can--
import mediaPlayer framework
in your .h
MPMoviePlayerController *moviePlayerController;
NSString *strVideoURL;
in your .m
in your ViewDidLoad -
NSURL *urlVideo = [NSURL URLWithString:strVideoURL];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:urlVideo];
[moviePlayerController.view setFrame:CGRectMake(0, 170, 320, 270)]
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
or wherever you want to play, pause, stop or restart
After 3 days of searching answer, got solution!!!
-(void)viewWillAppear:(BOOL)animated{
// just add observer
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieEventFullscreenHandler:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
and i need to stop player then user press Done:
- (IBAction) movieEventFullscreenHandler:(NSNotification*)notification{
[self.player.moviePlayer stop];
[self.player.moviePlayer setFullscreen:NO animated:NO];
}
Thats all!
I am attempting to play a simple mp4 file full screen when a view loads. Here is the code:
I am NOT getting a video at all, but I know the viewDidLoad is being called and that there is a view present.
//
// FirstViewController.m
// WSTR Finale
//
// Created by Chris Muench on 10/6/14.
// Copyright (c) 2014 World Series of Team Roping. All rights reserved.
//
#import "WatchLiveViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#interface WatchLiveViewController ()
#end
#implementation WatchLiveViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *movieURL = [NSURL URLWithString:#"http://download.wavetlan.com/SVV/Media/HTTP/H264/Talkinghead_Media/H264_test1_Talkinghead_mp4_480x360.mp4"];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
moviePlayer.view.transform = CGAffineTransformConcat(moviePlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow];
[moviePlayer.view setFrame:backgroundWindow.frame];
[backgroundWindow addSubview:moviePlayer.view];
[moviePlayer play];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Try this code:
MPMoviePlayerViewController *mpvController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
mpvController.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
mpvController.moviePlayer.scalingMode = MPMovieScalingModeFill;
mpvController.view.transform = CGAffineTransformConcat(mpvController.view.transform, CGAffineTransformMakeRotation(M_PI_2));
[self presentViewController:mpvController animated:NO completion:^{
[mpvController.moviePlayer play];
}];
I think you are missing prepareToPlay. Try something like this:
...
[moviePlayer prepareToPlay];
[moviePlayer play];
I'm trying to use the library https://github.com/arturfelipet/AnimatedLogin
I created my own project and wrote exactly the same code as in their example (but in my project I used Storyboard), but the video only shows a black screen. Here's the way I create the MPMoviePlayerController:
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#import <QuartzCore/QuartzCore.h>
#interface ViewController () {
MPMoviePlayerController *player;
}
#property (nonatomic, strong) MPMoviePlayerController *player;
#end
#implementation ViewController
#synthesize player;
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect screen = [[UIScreen mainScreen] bounds];
NSURL *movieUrl = [[NSBundle mainBundle] URLForResource:#"background" withExtension:#"mp4"];
self.player = [[MPMoviePlayerController alloc] initWithContentURL:movieUrl];
player.view.frame = screen;
player.scalingMode = MPMovieScalingModeFill;
[self.player setControlStyle:MPMovieControlStyleNone];
[self.view addSubview:player.view];
[player prepareToPlay];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playVideo)
name:MPMoviePlayerReadyForDisplayDidChangeNotification
object:player];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.player];
[player play];
}
- (void)moviePlayerDidFinish:(NSNotification *)note
{
if (note.object == self.player) {
NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
if (reason == MPMovieFinishReasonPlaybackEnded)
{
[self.player play];
}
}
}
-(void)playVideo{
[player play];
}
#end
But it's only a black screen when the app shows up. I'm using this code in my LoginViewController which presents from App Delegate like this:
if (![PFUser currentUser])
self.window.rootViewController = [UIStoryboard instansiateVCWithClass:[TPLoginViewController class]];
Maybe it has to do something with that? I've tried so many things and would really appreciate an answer.
Thanks.
That is because you doing all initializing in your viewDidLoad. Which means that processing will block your screen until all the task are done completely.
Put that code in a method and call it after delay using:
[self performSelector:<#(SEL)#> withObject:<#(id)#> afterDelay:<#(NSTimeInterval)#>];
EDIT: As turned out after checking with #tracifycray, there was some problem with URL,it was turning out nil somehow. As he said, "Yes, I got it to work. I tried to use a .mov-file instead, and then it worked".
In my application, I'm trying to play video using URL from my server . I'm using the UITableView to display the video list and by tapping the Cell from the list, the video will play in sub view. Now I want to play the video in landscape mode.
This is the current video code.
_movieplayer = [[MPMoviePlayerController alloc]initWithContentURL: [NSURL URLWithString:[self urlencode:self.strPlayUrl]]];
[[_movieplayer view] setFrame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview: [_movieplayer view]];
[_movieplayer setShouldAutoplay:YES];
[_movieplayer prepareToPlay];
[self.movieplayer play];
In the above code, how do I make it work so that it plays in landscape mode. Please guide me on this. Been stuck on this for a long time.
Add this to your viewcontroller with the movieplayer
#property (nonatomic, strong) MPMoviePlayerController* mpc;
- (void)setUpMPC
{
NSURL* m = [[NSBundle mainBundle] URLForResource:#"YourVideo" withExtension:#"mp4"];
MPMoviePlayerController* mp = [[MPMoviePlayerController alloc] initWithContentURL:m];
self.mpc = mp; // retain policy
self.mpc.shouldAutoplay = NO;
[self.mpc prepareToPlay];
self.mpc.view.frame = CGRectMake(50, 50, self.view.bounds.size.width, self.view.bounds.size.height);
}
-(NSUInteger)supportedInterfaceOrientations {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskLandscape;
}
Probably you should implement the shouldAutorotateToInterfaceOrientation and supportedInterfaceOrientations methods in your viewController
#property (nonatomic, strong) MPMoviePlayerController* moviePlayerController;
#pragma mark # - Init -
- (void) createAndPlayMovieForURL: (NSURL*) movieURL
{
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
[self.moviePlayerController.view setFrame: self.view.bounds];
[self.view addSubview: self.moviePlayerController.view];
[self.view bringSubviewToFront: self.overlayView];
}
#pragma mark - Rotation -
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation
{
return YES;
}
- (NSUInteger) supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
I'm trying to loop a local video, but I keep having issues. The iphone simulator build will succeed but the video won't play or loop. I'm in the middle of making an App that just plays a video on loop after the LaunchImage. Does it have something to do with the MPMoviePlayerController?
Here's the code:
ViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface ViewController : UIViewController
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#property (strong, nonatomic) MPMoviePlayerController *moviePlayer;
#end
#implementation ViewController
#synthesize moviePlayer;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString*thePath=[[NSBundle mainBundle] pathForResource:#"ColoredDiamond" ofType:#".mp4"];
NSURL*theurl=[NSURL fileURLWithPath: thePath];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theurl];
[self.moviePlayer.view setFrame:CGRectMake(40,197,240,160)];
[self.moviePlayer prepareToPlay];
[self.moviePlayer setShouldAutoplay:YES];
[self.view addSubview:self.moviePlayer.view];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playBackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/* - (void) playbackFinished: (NSNotification*) notification{
NSLog(#"playBackFinished");
if(moviePlayer){
[moviePlayer play];
}
}
*/
#end
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theurl];
[self.moviePlayer.view setFrame:CGRectMake(40,197,240,160)];
[self.moviePlayer prepareToPlay];
[self.moviePlayer setRepeatMode:MPMovieRepeatModeOne];
[self.moviePlayer setShouldAutoplay:YES];
[self.view addSubview:self.moviePlayer.view];
RepeatMode determines how the movie player repeats the playback of the movie.
#property (nonatomic) MPMovieRepeatMode repeatMode
The default value of this property is MPMovieRepeatModeNone.
List of available repeat modes,
MPMovieRepeatModeNone,
MPMovieRepeatModeOne
Available in iOS 3.2 and later.
Related Sample Code : MoviePlayer
Declared In : MPMoviePlayerController.h