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];
Related
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];
}];
}
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".
First attempt to integrate video into an app. I've followed a couple of tutorials and also code from a book that I have, but I'm just getting a blank screen with no video (or anything else for that matter). I should also mention that I am attempting to run this in the simulator, if that matters.
I have MediaPlayer.framework linked as a binary library in my Build Phases. This is my code:
I can't understand what I am overlooking. Can anyone see it? Thanks!
MoviePlayerViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface MoviePlayerViewController : UIViewController
#end
MoviePlayerViewController.m
#import "MoviePlayerViewController.h"
#interface MoviePlayerViewController ()
#end
#implementation MoviePlayerViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url=[[NSURL alloc] initWithString:#"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
// MPMoviePlayerController *player = [notification object];
//
// [[NSNotificationCenter defaultCenter] removeObserver:self
// name:MPMoviePlayerPlaybackDidFinishNotification
// object:player];
//
// if ([player respondsToSelector:#selector(setFullscreen:animated:)])
// {
// [player.view removeFromSuperview];
// }
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Try this with moviePlayer variable is iVar
NSURL *movieURL = [NSURL URLWithString:#"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];
self.moviePlayer = [[MPMoviePlayerController alloc] init];
[self.moviePlayer setShouldAutoplay:YES];
[self.moviePlayer setContentURL:movieURL.absoluteURL];
[self.moviePlayer setMovieSourceType:MPMovieSourceTypeFile];
[self.moviePlayer setControlStyle:MPMovieControlModeDefault];
[self.moviePlayer setFullscreen:NO];
[self.moviePlayer setScalingMode:MPMovieScalingModeNone];
self.moviePlayer.view.frame = CGRectMake(40, 40, 240, 350);
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer prepareToPlay];
[self.moviePlayer play];
(1) Try specifying the movie source type as streaming:
moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
(2) Even though it's in fullscreen mode, I think you have to set an initial frame for your player, for example if you want it to take up the fullscreen:
[moviePlayer.view setFrame:self.view.bounds];
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
I'm working on a IOS App (With Storyoard). I have a ViewController:
//movieViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface movieViewController : UIViewController {
MPMoviePlayerViewController *moviePlayer;
}
#property (strong, nonatomic) MPMoviePlayerViewController *moviePlayer;
-(void) playMovie;
#end
//movieViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self playMovie];
}
-(void)playMovie
{
NSString *videoPath = [[NSBundle mainBundle] pathForResource:#"movie"
ofType:#"mov"];
moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL
fileURLWithPath:videoPath]];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
}
When I run I see my viewController with a "Loading..." that takes forever.
You should call -prepareToPlay and/or -play. Per MPMoviePlayerController's documentation:
Starting in iOS 5.0, in order to facilitate finer-grained playback control, a new movie player is not automatically prepared to play.
-(IBAction)startVideo:(id)sender
{
NSURL *video = [[NSURL alloc] initWithString:#"http://www.w3schools.com/html/movie.mp4"];
MPMoviePlayerViewController *controller = [[MPMoviePlayerViewController alloc]initWithContentURL:video];
controller.view.frame = CGRectMake(20, 50, 280, 180);
controller.moviePlayer.view.center = self.view.center;
[self.view addSubview:controller.view];
[controller.moviePlayer prepareToPlay];
[controller.moviePlayer play];
}