I am newbie for ios development, I am working on a project ,where i need to show some sample videos.
i Would like to show the video in streaming format(youtube player) .
-(void) playVideoOfURL:(NSString*)videoPath
{
NSString *url = [NSString stringWithFormat:#"%#%#",TipsService,videoPath];
moviePlayer = [[ MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
if (moviePlayer)
{
moviePlayer.view.frame = CGRectMake(0, 0, 320, 460);
moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
moviePlayer.useApplicationAudioSession = YES;
//Register for the playback finished notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallback:) name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];
//setup device rotation notification observer
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
UIViewController *player = [[UIViewController alloc] init];
player.view = moviePlayer.view;
[self.navigationController pushViewController:player animated:YES];
[moviePlayer play];
[moviePlayer setFullscreen:TRUE];
}
}
I tried this one, but no use. Please help me...
Try this
[moviePlayer play];
[moviePlayer setFullscreen:TRUE];
UIViewController *player = [[UIViewController alloc] init];
player.view = moviePlayer.view;
[self.navigationController pushViewController:player animated:YES];
Related
I am porting app from iOS6 to iOS7. There is a weird problem that makes the screen go black after the call from a button is being done. I have tried out this, this and this.
There is no apt answer and I don't feel theoretically there should be any problem in using the previous methods.
Kindly provide me some thread to why this problem is occuring.
Thanks
This can help
Import MediaPlayer in your .h file
#import <MediaPlayer/MediaPlayer.h>
Make a property
#property (strong, nonatomic) MPMoviePlayerController *moviePlayer;
After that you can play video by this
-(void)playMovie:(id)sender
{
NSURL *url = [NSURL URLWithString:
#"http://www.xyz.com/ios_book/movie/movie.mov"];
_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];
}
After that for removing the video view add this
- (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];
}
}
Add your movie player controller view on main windows like:
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:#"map.mp4"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview: player.view];
[player play];
Hope so it will work for you!
Try this way..
ViewController.m
MPMoviePlayerViewController *mp=[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:[[arr_videos objectAtIndex:indexPath.row]valueForKey:#"Video_path"]]];
MPMoviePlayerController *pc=[mp moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self presentViewController:mp animated:YES completion:nil];
[pc prepareToPlay];
[pc play];
I have problem with MPMoviePlayerViewController , when app enters background and then I launch it again or go another viewControllers the movie became black ! I have movie which plays in the background of my menus , here is my code :
EIDTED CODE :
-(void)viewDidLoad {
[self moviePlayer2];
}
- (void) moviePlayer2 {
NSString *path = [[NSBundle mainBundle] pathForResource:#"cloud" ofType:#"mp4"];
player = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
player.view.userInteractionEnabled = YES;
player.moviePlayer.repeatMode = YES;
player.moviePlayer.scalingMode = MPMovieScalingModeFill;
player.moviePlayer.controlStyle = MPMovieControlStyleNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackStateChange:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:[player moviePlayer]];
[[player moviePlayer] play];
[self.view addSubview:player.view];
}
-(void) moviePlayBackStateChange: (NSNotification *) note {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:[player moviePlayer]];
[[player moviePlayer] play];
//[player release];
NSLog(#"FINISHED");
}
thank you .
I think you may need to add codes below:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackStateChange:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:[player moviePlayer]];
and handle the movie state in the moviePlayBackStateChange method.
The movie will be paused when the movie is playing and the app enters in background, so you need to make the movie resume like below when the app come back from background. If not,the movie will keep the pause state. That's why your app becomes black.
[[player moviePlayer] play];
then the movie will continue to play.
adding two methods which you should invote when the app comes into background and backs from background:
-(void) pauseMovieInBackGround
{
[player moviePlayer] pause];
[player.view removeFromSuperview];
}
-(void) resumeMovieInFrontGround
{
[self.view addSubview:player.view];
[[player moviePlayer] play];
}
Hope this can help you guy.
Try changing this:
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:[player moviePlayer]];
[player release];
To this:
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:[player moviePlayer]];
[movieController.view removeFromSuperview];
[player release];
See if that works :D
I am having problem with MPMovieplayerController .
mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
now when I try to write mp. the list does not contain the "setControlStyle" ! is there any thing wrong ?
and if I write directly [mp setControlStyle:MPMovieControlStyleFullscreen]; than video starts in full screen but done button is not displayed but ya if I click on the place where done button should be than it works fine !!!!
EDITED :
- (id)initWithPath:(NSString *)moviePath
{
// Initialize and create movie URL
if (self = [super init])
{
movieURL = [NSURL URLWithString:moviePath];
[movieURL retain];
}
return self;
}
- (void) readyPlayer
{
mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[mp setControlStyle:MPMovieControlStyleFullscreen];
[mp setFullscreen:YES];
if ([mp respondsToSelector:#selector(loadState)])
{
// Register that the load state changed (movie is ready)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
}
else
{
[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePreloadDidFinish:)
name:MPMoviePlayerContentPreloadDidFinishNotification
object:nil];
}
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
- (void) loadView
{
[self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]];
[[self view] setBackgroundColor:[UIColor blackColor]];
}
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification
{
//[self makeButton];
// Unless state is unknown, start playback
if ([mp loadState] != MPMovieLoadStateUnknown)
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
// Rotate the view for landscape playback
[[self view] setBounds:CGRectMake(0, 0, 480, 320)];
[[self view] setCenter:CGPointMake(160, 240)];
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
// Set frame of movieplayer
[[mp view] setFrame:CGRectMake(0, 0, 480, 320)];
// Add movie player as subview
[[self view] addSubview:[mp view]];
// Play the movie
[mp play];
}
}
need guidance.
As the property in question is called controlStyle, you could use mp.controlStyle=..; or [mp setControlStyle:..];
For the second question / issue, see mpmovieplayercontroller-audio-show-done-button for a quick workaround solution.
I now have a strange problem when I try to play movie through MPMoviePlayerViewController on iOS 4.3.
It appears just a white screen and nothing happens. My code works well on iOS 4.2. So I created a new
OpenGLES project and just use my movie code, but it can not work either. So can anybody give some tips?
Thanks in advance.
My code is below:
//-------------------------------------TestViewController.h---------------------------------------
#import <MediaPlayer/MediaPlayer.h>
#interface TestViewController : UIViewController
{
bool _isMovieEnded;
MPMoviePlayerViewController* _theMovie;
}
- (void)playMovie:(NSString*)filename;
- (void)movieFinishedCallback:(NSNotification*)aNotification;
- (bool)isMovieEnd;
//-------------------------------------TestViewController.m---------------------------------------
- (void)playMovie:(NSString*)filename
{
NSURL* theURL = [NSURL fileURLWithPath:filename];
_theMovie = [[MPMoviePlayerViewController alloc] init];// initWithContentURL:theURL];
[_theMovie.moviePlayer setContentURL:theURL];
_theMovie.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
[_theMovie.moviePlayer setControlStyle:MPMovieControlStyleNone];
[_theMovie.moviePlayer setFullscreen:YES];
// register notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_theMovie];
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[_theMovie.moviePlayer play];
[self.view addSubview:_theMovie.view];
}
- (void)movieFinishedCallback:(NSNotification*)aNotification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:_theMovie];
// release movie
[_theMovie.view removeFromSuperview];
[_theMovie release];
_isMovieEnded = true;
[self startAnimation];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}
- (bool)isMovieEnd
{
return _isMovieEnded;
}
//-------------------------------------TestAppDelegate.m---------------------------------------
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
[self.window addSubview:self.viewController.view];
NSString* movieFile = [[NSBundle mainBundle] pathForResource:#"logo" ofType:#"mp4"];
[viewController playMovie:movieFile];
[viewController stopAnimation];
return YES;
}
// Determines if the movie is presented in the entire screen (obscuring all other application content). Default is NO.
// Setting this property to YES before the movie player's view is visible will have no effect.
#property(nonatomic, getter=isFullscreen) BOOL fullscreen;
- (void)setFullscreen:(BOOL)fullscreen animated:(BOOL)animated;
From MPMoviePlayerController.h
so here you are the code:
- (void)playVideoFromUrl:(NSString*)url {
MPMoviePlayerViewController *moviePlayer = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:url]] autorelease];
[((UIWindow*)[[UIApplication sharedApplication].windows objectAtIndex:0]).rootViewController presentMoviePlayerViewControllerAnimated:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[moviePlayer.moviePlayer setFullscreen:YES animated:YES];
[moviePlayer.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
}
- (void)moviePlaybackComplete:(NSNotification *)notification {
MPMoviePlayerViewController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[((UIWindow*)[[UIApplication sharedApplication].windows objectAtIndex:0]).rootViewController dismissMoviePlayerViewControllerAnimated];
}
I am using MPMoviePlayerController currently to play a video inside IPhone and now I wish to play this video in a small area of the view (not the full screen). I think there is a frame way of doing it but I couldn't find the required tutorial somewhere. Have you been across any? That would be great.
UPDATED
I have reached to this point but still it doesn't show the player to play on screen.
-(IBAction)startVideo {
//start video here
NSURL *path = [[NSURL alloc] initWithString:[self localVideoPath:NO]];
// Create custom movie player
MPMoviePlayerController *moviePlayer = [[[MPMoviePlayerController alloc] initWithContentURL:path] autorelease];
[moviePlayer setScalingMode:MPMovieScalingModeAspectFill];
[moviePlayer setControlStyle:MPMovieControlStyleNone];
[moviePlayer setFullscreen:FALSE];
// May help to reduce latency
[moviePlayer prepareToPlay];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(onMSAASDone:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
//---play partial screen---
//moviePlayer.view.frame = CGRectMake(0, 0, 200, 300);
moviePlayer.view.frame = image.frame;
//[[moviePlayer view] setFrame: [image bounds]];
[image removeFromSuperview];
[self.view addSubview:moviePlayer.view];
// Show the movie player as modal
//[self presentModalViewController:moviePlayer animated:YES];
// Prep and play the movie
[moviePlayer play];
}
Here is 'windowed' code...
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[player setControlStyle:MPMovieControlStyleNone];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player setScalingMode:MPMovieScalingModeAspectFill];
[player setFullscreen:FALSE];
//---play partial screen---
player.view.frame = CGRectMake(0, 0, 200, 300);
[self.view addSubview:player.view];