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];
Related
Nothing happens when I tap on my MPMoviePlayerController. Can anyone see what I am doing wrong in setting up the UITapGestureRecognizer?
- (void) playMovie : (NSString *) urlString{
self.movieURLString = urlString;
NSURL *movieURL = [NSURL URLWithString:self.movieURLString];
self.moviePlayer = [[MPMoviePlayerController alloc] init];
[self.moviePlayer setShouldAutoplay:YES];
[self.moviePlayer setContentURL:movieURL.absoluteURL];
[self.moviePlayer setMovieSourceType:MPMovieSourceTypeFile];
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
self.moviePlayer.view.frame = self.movieView.frame;
/* add tap handler */
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showControls:)];
singleFingerTap.delegate = self;
//EDIT: this line somehow didn't make into my OP
[self.moviePlayer.view addGestureRecognizer:singleFingerTap];
self.moviePlayer.view.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer prepareToPlay];
// respond to changes in movie player status
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
}
- (void) showControls : (UITapGestureRecognizer *) recognizer {
NSLog(#"here");
self.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
}
You never added the gesture recognizer to a view, that's what's wrong.
[self.moviePlayer.view addGestureRecognizer:singleFingerTap];
There's also no need to set the delegate unless you're going to implement some of the methods in the UIGestureRecognizerDelegate protocol.
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".
I'm trying to play a video in mp4 format from URL over HTTPS using MPMoviePlayerController, but video is not playing and I receive an error in logs:
_itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
Is any way to play this kind of video on iOS?
Here's my code:
#import "FirstViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#interface FirstViewController ()
#property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
#end
#implementation FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self playBtnPressed];
}
-(void)playBtnPressed
{
NSURL *url=[[NSURL alloc] initWithString:#"https://....mp4"];
_moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:_moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDonePressed:) name:MPMoviePlayerDidExitFullscreenNotification object:_moviePlayer];
_moviePlayer.controlStyle=MPMovieControlStyleDefault;
//moviePlayer.shouldAutoplay=NO;
[_moviePlayer play];
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES];
}
- (void) moviePlayBackDonePressed:(NSNotification*)notification
{
[_moviePlayer stop];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:_moviePlayer];
if ([_moviePlayer respondsToSelector:#selector(setFullscreen:animated:)])
{
[_moviePlayer.view removeFromSuperview];
}
_moviePlayer=nil;
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[_moviePlayer stop];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:_moviePlayer];
if ([_moviePlayer respondsToSelector:#selector(setFullscreen:animated:)])
{
[_moviePlayer.view removeFromSuperview];
}
}
#end
Probably video aspect ratio issue try setting aspect ratio
[player setScalingMode:MPMovieScalingModeAspectFit];
player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
Or check if the video file is there in the specified URL location
It seems to be an iOS 7 issue.
Apparently, moviePlayer.movieSourceType = MPMovieSourceTypeStreaming; does not work anymore.
For me, replacing it with MPMovieSourceTypeFile solved this error.
Use following code it will work for you. Be sure that you have the notification observer declared before playing movie, as follows:
[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController.moviePlayer];
moviePlayerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[moviePlayerController.moviePlayer prepareToPlay];
[self presentMoviePlayerViewControllerAnimated:moviePlayerController];
[moviePlayerController.moviePlayer play];
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