MPMoviePlayerViewController Not playing video by streaming? - ios

I am using the below code to play the video, but its working fine while using the local url that mean resourse file path. Not working for server url.
player = [[MPMoviePlayerViewController alloc] initWithContentURL:urlvalue];
player.view.frame = CGRectMake(10, 10, 800, 800);
player.moviePlayer.shouldAutoplay=YES;
player.moviePlayer.movieSourceType= MPMovieSourceTypeFile;
[player.moviePlayer setControlStyle:MPMovieControlStyleDefault];
[self.view addSubview:player.view];
[player.moviePlayer prepareToPlay];
[player.moviePlayer play];
Kindly correct me if I missed out something. Thanks in advance.

Pay attention on iOS 9 App transport security. If you haven't set it up, streaming will fail with black empty screen.

Change movieSourceType from File to Streaming :
player.moviePlayer.movieSourceType= MPMovieSourceTypeStreaming;
Note : url should be a streaming url not physical file path

Related

Play audio in background (play when using other APP)

I created an app for streaming audio (and send mail as well) but I have a problem, my app will not keep playing after I press the home button.
I use XCODE 6.4 and the mediaplayer.framework to play the stream it is in viewcontroller and I have been activating the background mode Audio & airplay.
Can someone please let me know what I am doing wrong here, I really want to learn it.
code:
NSURL *videoStreamURL = [NSURL URLWithString:#"http://streamlink.m3u"];
_player = [[MPMoviePlayerController alloc] initWithContentURL:videoStreamURL];
_player.view.frame = CGRectMake(0, 530, self.view.frame.size.width, 38);
[self.view addSubview:_player.view];
[_player play];

Play video in app instead of default player

I use the following code to play videos:
MPMoviePlayerViewController *theMovie=
[[MPMoviePlayerViewController alloc] initWithContentURL: movieURL];
theMovie.moviePlayer.repeatMode=MPMovieRepeatModeOne;
[self presentMoviePlayerViewControllerAnimated:theMovie];
This causes a the default movie player to cover the app. Instead I want the video to play inside the app itself as follows:
Can this be achieved?
You can set its frame like this:
theMovie.view.frame = CGRectMake(10, 10, 300, 300);
Then without presenting it add it as subview:
[self.view addSubview: theMovie.view];
Hope this helps.. :)
Yes, this can be achieved by using the AVPlayerLayer. Please see examples here:
"AV Foundation Programming Guide", "Putting It All Together: Playing a Video File Using AVPlayerLayer" section
A tutorial from the iosguy.com
Appuccino's "Playing Movies With AVPlayerLayer In iOS 6" video tutporial

MPMoviePlayerController setFullScreen animated bug on iOS 8.0 but not on iOS 8.1

I have and MPMoviePlayerController in my project. I tried to run it on iOS 8.0. When I play the video for the first time, It works fine and I have the progress bar of the video. Then I quit and try to play the video for the second time. It still works except than I don't have the progress bar of the video. I said in the title "setFullScreen:animated" because I also had the error "CGImageCreate : invalid image size 0x0" so I guess It can come frome here.
Also, it appears to work without any problem on iOS 8.1, so I think it's bug corrected by Apple. Maybe someone have heard something about it.
Here is some code when the Controller is created and setted :
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:
[NSURL fileURLWithPath:self.model.filePath]];
_moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
_moviePlayer.view.backgroundColor = [UIColor blackColor];
_moviePlayer.view.frame = self.imageButton.frame;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES];
[_moviePlayer prepareToPlay];
Any guess ?

iOS AVPlayer won't play

I have the following code set up to play a video in my iOS app, but it just doesn't play. The app compiles and runs, but all I get a red frame where I positioned it. When debugging, I found that the the program doesn't even step into the last line [player play]. Also, the video runs fine in a UIWebView.
NSString *streamingString = [NSString stringWithFormat:#"http://youtu.be/...."];
AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:streamingString]];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:player];
[layer setPlayer:player];
[layer setFrame:CGRectMake(50, 50, 400, 300)];
[layer setBackgroundColor:[UIColor redColor].CGColor];
[layer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[self.view.layer addSublayer:layer];
[player play];
I am a new programmer and this is my first post to Stackoverflow, so please excuse me if I have not given enough information or am missing something obvious! Thank you so much!
assetWithURL: is often, but not always, used with a URL that points to a file already on the device, often within the bundle. If you are using an external URL, such as one on the internet, the url must resolve to a video that is in a format that the AVPlayer can understand. A URL to a YouTube web page will not work. In general, you can't play a YouTube video in AVPlayer.
Check out this document from Apple and this video on YouTube.
EDIT:
Some developers have used this library to play a YouTube video in their app. Another option is to use a UIWebView.

Display MPMoviePlayerController during media loading

I use a MPMoviePlayerController to play audio (ex: http://www.stanford.edu/group/edcorner/uploads/podcast/ballmer090506.mp3) or video (ex: http://video.ted.com/talk/podcast/2011U/None/RicElias_2011U.mp4)
myMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theURL];
myMoviePlayer.view.frame = CGRectMake(-80, 80, 480, 320);
myMoviePlayer.controlStyle = MPMovieControlStyleFullscreen;
myMoviePlayer.movieSourceType = MPMovieSourceTypeFile;
myMoviePlayer.scalingMode = MPMovieScalingModeAspectFit;
myMoviePlayer.useApplicationAudioSession = TRUE;
[myMoviePlayer prepareToPlay];
[myMoviePlayer play];
It works well, my only one problem is during the loading time (between 5s and 20s if I have a bad 3G reception), I have a black screen.
I would like to have MPMoviePlayerController diplayed with the "Loading..." label and the UIActiviyIndicator in his top bar during this time, like YouTube videos.
How can I do that?
Thanks.
Just use MPMoviePlayerViewController instead of MPMoviePlayerController. It should do what you want.

Resources