MPMoviePlayerController Black Window, Not loading audio file - ios

Making an app for my church and need mp3 file to play after being downloaded from xml. NSLog displays the url correctly, but when I run it, I just get a spinning "loading" and a black box. Any suggestions as to why?
Thanks!
-(void)viewDidAppear:(BOOL)animated
{
RSSItem* item = (RSSItem*)self.detailItem;
self.title = item.title;
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:item.link];
NSLog(#"The url is %#", item.link);
moviePlayerController.view.frame = CGRectMake(73, 203, 640, 360);
[self.view addSubview:moviePlayerController.view];
[moviePlayerController prepareToPlay];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];

TRY this one..
RSSItem* item = (RSSItem*)self.detailItem;
self.title = item.title;
if(item.link)
{
//NSURL *url = [NSURL fileURLWithPath:item.link];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:item.link];
moviePlayer.view.frame = CGRectMake(0, 0, 500, 500);
moviePlayer.moviePlayer.shouldAutoplay=YES;
moviePlayer.moviePlayer.controlStyle = MPMediaTypeMusicVideo;
[moviePlayer.moviePlayer setFullscreen:YES animated:YES];
[self.view addSubview:moviePlayer.view];
[moviePlayer.moviePlayer play];
}
else
{
NSLog(#"no url");
}

Related

Play video on screen in full screen mode in ios?

I am trying to play video in full screen mode when screen is just launched but some how i am not able to play the video.I am using below code to play the video but video is not playing please tell me how can i play it in full screen mode.
NSURL *fileURL = [NSURL URLWithString:[IMAGE_BASE_URL stringByAppendingString:self.image_url]];
[self.moviePlayerController.view setFrame:CGRectMake(0, 70, self.view.frame.size.width, self.view.frame.size.height)];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
CGRect movieFrame;
movieFrame.size = self.view_video.frame.size;
[self.moviePlayerController.view setFrame:movieFrame];
[self.moviePlayerController setControlStyle:MPMovieControlStyleEmbedded];
[self.moviePlayerController.view setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.moviePlayerController setScalingMode:MPMovieScalingModeFill];
[self.view_video addSubview:self.moviePlayerController.view];
[self.view_video bringSubviewToFront:self.moviePlayerController.view];
[self.moviePlayerController play];
[self.moviePlayerController setFullscreen:TRUE animated:TRUE];
[self.moviePlayerController setScalingMode:MPMovieScalingModeAspectFill];
[self.moviePlayerController setControlStyle:MPMovieControlStyleFullscreen];
if (movieExists)
{
NSURL *url = [NSURL fileURLWithPath:moviePath];
player = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
player.moviePlayer.fullscreen = YES;
player.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
}
self.moviePlayerController = [[MPMoviePlayerController alloc] init];
[self.moviePlayerController.view setFrame:CGRectMake(0, 70,self.view.frame.size.width, self.view.frame.size.height)];
[self.moviePlayerController setShouldAutoplay:NO];
[self.moviePlayerController setContentURL:self.videoURL];
[self.moviePlayerController setFullscreen:YES animated:YES];
self.moviePlayerController.controlStyle=MPMovieControlStyleFullscreen;
[self.moviePlayerController play];
[self.view addSubview:self.videoController.view];

How to play the Video in iphone

NSURL *url="file:///Users/adi/Library/Developer/CoreSimulator/Devices/F7C9D729-6E67-45C3-9234-629460FDD0A4/data/Containers/Data/Application/319F0627-CF3E-43D4-A44F-3E5E797CCCEA/Library/Caches/Attachments/7157783VID_20150602_185133.mp4"
Here's my code:
MPMoviePlayerController * moviePlayer =[[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(10, 100, 300, 200);
[moviePlayer play];
It shows blank screen.
Hope this may help you
#import<MediaPlayer/MediaPlayer.h>
-(IBAction)VideoPlayer:(id)sender
{
NSString *path = [[NSBundle mainBundle]pathForResource: #"video-en-2b" ofType:#"mp4"];
moviePlayer = [[MPMoviePlayerViewControlle alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
Codin[self presentModalViewController:moviePlayer animated:NO];
}
Well if you have to play video using local file use this code.It is helpful for you.
NSString*thePath=[[NSBundle mainBundle] pathForResource:#"yourVideo" ofType:#"mp4"];//7157783VID_20150602_185133 name
NSURL*theurl=[NSURL fileURLWithPath:thePath];
moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:theurl];
[moviePlayer.view setFrame:CGRectMake(10, 100, 200, 300)];
[moviePlayer prepareToPlay];
[moviePlayer setShouldAutoplay:NO]; // And other options you can look through the documentation.
[self.view addSubview:moviePlayer.view];
If you want to control playback process
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playBackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
Try this :
-(void)playVideo : (NSString*)apath
{
NSURL *videoURL =[NSURL fileURLWithPath:apath];
if ([[NSFileManager defaultManager] fileExistsAtPath:apath]) //Does file exist?
{
MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
videoPlayerView.moviePlayer.movieSourceType=MPMovieSourceTypeFile;
[self presentMoviePlayerViewControllerAnimated:videoPlayerView];
[videoPlayerView.moviePlayer play];
}
}
Note: this method only accept local file path

How to init MPMoviePlayerController with video paused

I'm developing an app for iOS where it shows a movie, my method is a class method this is the code
Functions.m
static MPMoviePlayerController *moviePlayer = nil;
+(void)playMP4Movie:(NSString *)moviename
insideOfView:(UIView *)view
autoplay:(BOOL)autoplay{
NSString *path = [[NSBundle mainBundle] pathForResource:moviename ofType:#"mp4"];
NSURL *videoURL = [NSURL fileURLWithPath:path];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
moviePlayer.repeatMode = MPMovieRepeatModeOne; // for looping
moviePlayer.view.frame = view.bounds;
[moviePlayer prepareToPlay];
if (autoplay) { [moviePlayer play]; }
else { [moviePlayer stop]; }
[view addSubview:moviePlayer.view];
}
and where I want to call it with video paused:
[Functions playMP4Movie:videoName insideOfView:_videoPlayerView autoplay:NO];
it shows my video but no matter if I set autoplay to YES or NO, the result is always the same... any help I'll appreciate
Try using:
if (autoplay) { [moviePlayer setShouldAutoplay:YES]; }
else { [moviePlayer setShouldAutoplay:NO]; }
And see if you have better luck.

Looping video using MPMoviePlayerController somewhere between startTime and endTime of the Video

Suppose the video duration is 50 seconds. In need to loop part of the video between 10th sec to 20th sec.
Very first time when I play the video it works fine. But for 2nd iteration onwards the video plays from the start (i.e from 0th second rather than 10th second).
Following is my code:
self.moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:currentVideoURL];
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
[self.moviePlayer.view setFrame: movieView.bounds];
[self.moviePlayer.view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
[self.moviePlayer setInitialPlaybackTime:10.0];
[self.moviePlayer setEndPlaybackTime:20.0];
[self.moviePlayer prepareToPlay];
self.moviePlayer.shouldAutoplay = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerDidFinish:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:self.moviePlayer];
[self.movieView addSubview:moviePlayer.view];
- (void)moviePlayerDidFinish:(NSNotification *)note {
if (note.object == self.moviePlayer) {
NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
if (reason == MPMovieFinishReasonPlaybackEnded) {
// [self.moviePlayer setInitialPlaybackTime:10.0];
// [self.moviePlayer setEndPlaybackTime:20.0];
[self.moviePlayer play];
}
}
}
Also I tried to reset the InitialPlaybackTime and EndPlaybackTime in by moviePlayerDidFinish Method, but didn't worked.
Depending on the requirement I only need to loop only particular part of the video.
Please help me.. Thanks!!!
set the property repeatMode = MPMovieRepeatModeOne of MPMoviePlayerController self.moviePlayer.
self.moviePlayer repeatMode = MPMovieRepeatModeOne;
See Example
-(void)playMovie:(id)sender
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"1" ofType:#"MOV"]];
_moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
_moviePlayer.initialPlaybackTime = 3.0;
_moviePlayer.endPlaybackTime = 5.0;
_moviePlayer.repeatMode = MPMovieRepeatModeOne;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES];
}

MPMoviePlayerController is functioning wrong

When i tried a video saved on the Xcode project , it worked.
then when i changed it with a url , it always returns this error :
_itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
Here's my code :
NSURL *url = [NSURL fileURLWithPath:#"https://m.youtube.com/watch?v=9MNAeGWd_04"];
movieController = [[MoviePlayerVC alloc]initWithContentURL:url];
movieController.view.frame = self.view.bounds;
[movieController.moviePlayer setShouldAutoplay:YES];
[movieController.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
[[movieController view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
[self presentViewController:movieController animated:NO completion:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playbackStateDidChange:)
name:#"MPAVControllerPlaybackStateChangedNotification"
object:nil];
fileURLWithPath: expects a local path url - /User/blah/blah.mp3
Try to use [NSURL URLWithString:#"https://m.youtube.com/watch?v=9MNAeGWd_04"]
IF YOU PLAY FROM URL THEN TRY THIS
-(void)playFromLiveUrl
{
NSURL *aUrl = [NSURL URLWithString:#"*YOUR URL*"];
_moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:aUrl];
_moviePlayer.controlStyle = MPMovieControlStyleNone;
_moviePlayer.shouldAutoplay = YES;
_moviePlayer.view.frame = asyvideoImage.frame;
_moviePlayer.scalingMode=MPMovieScalingModeFill;
[self.view addsubview _moviePlayer.view];
[_moviePlayer prepareToPlay];
[_moviePlayer setFullscreen:NO animated:NO];
}
IF YOU ARE USE LOCAL FILE THEN USE THIS
-(void)playFromLocal
{
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:#"** LOCAL PATH ** "]];
_moviePlayer.controlStyle = MPMovieControlStyleNone;
_moviePlayer.shouldAutoplay = YES;
_moviePlayer.view.frame = asyvideoImage.frame;
_moviePlayer.scalingMode=MPMovieScalingModeFill;
[self.view addsubview _moviePlayer.view];
[_moviePlayer prepareToPlay];
[_moviePlayer setFullscreen:NO animated:NO];
}
The MPMoviewPlayerController Can't get your format. U r try to load the MPMoviePlayerController with youTube link, it won't work (as u can see :)
Here u can find the iPhone video format support.
Try to load your MoviePlayer something like this (for example .m3u8 format):
[movieController setContentURL:[NSURL URLWithString:#"http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8"]];
MPMoviePlayerController cannot play a Flash Youtube URL. Use a web view instead. UIWebView on iOS is smart enough play youtube content.
You can see the video formats MPMoviePlayer and AVFoundation support here

Resources