I configure a MPMoviewPlayerController to display iAd before I play a video. So basicaly my code looks like this
player = [MPMoviePlayerController new];
player.contentURL = videoURL;
[player playPrerollAdWithCompletionHandler:^(NSError *error) {
NSLog(#"error playing ad %#", error.userInfo);
[player play];
}];
[self.contentView addSubview:player.view];
[self.contentView layoutIfNeeded];
And in AppDelegate
[MPMoviePlayerController preparePrerollAds];
But the problem is that i receive the sound, but player does not display a video. Also I don't get any error in completion handler… Any help is very appreciated!
For me it only works when the player is set to fullscreen.
[parent.view addSubview: player.view];
[parent.view bringSubviewToFront:player.view];
[player setFullscreen:YES animated:YES];
I was having the same problem (I could hear the audio, but no video played), and I got a response on the Apple Developer Forums.
It appears you have to make sure there's only one reference to the movie player controller. This is the code I used, which now works:
#interface UIViewController ()
#property (strong, nonatomic) MPMoviePlayerController *moviePlayer;
#end
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] init];
moviePlayer.contentURL = movieURL;
if (moviePlayer)
{
self.moviePlayer = moviePlayer;
moviePlayer = nil;
[self.moviePlayer prepareToPlay];
[self.view addSubview:self.moviePlayer.view];
}
Then later, I use:
- (void)playVideo
{
[self.moviePlayer playPrerollAdWithCompletionHandler:^(NSError *error) {
if (!error)
{
[self.moviePlayer play];
}
}];
}
I hope that helps.
Related
I want to play video on clicking a button. My AVPlayerViewController is open but not playing an video .Please help.
- (IBAction)btnPlay_Click:(id)sender {
NSURL *videoURL = [NSURL URLWithString:#"https://player.vimeo.com/video/143506410"];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
[self presentViewController:playerViewController animated:YES completion:nil];
}
If you are using vimeo video you can use third party tools which helps you load the vimeo video.
Solution 1: Please try this, It works for me, just some lines of code.
- (void)viewDidLoad
{
[super viewDidLoad];
vimeoHelper = [[VimeoHelper alloc] init];
[vimeoHelper getVimeoRedirectUrlWithUrl:#"http://vimeo.com/52760742" delegate:(id)self];
}
- (void)finishedGetVimeoURL:(NSString *)url
{
_moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:url]];
[self presentViewController:_moviePlayerController animated:NO completion:nil];
}
Solution 2: YTVimeoExtractor
May be it will help you.
Use MPMoviePlayerController and add this line of code:
NSURL *videoURL = [NSURL URLWithString:#"https://player.vimeo.com/video/143506410"];
MPMoviePlayerViewController *player =[[MPMoviePlayerViewController alloc] initWithContentURL: videoURL];
[self.view addSubview: [player view]];
[self presentMoviePlayerViewControllerAnimated:player];
[player.moviePlayer prepareToPlay];
Add one more line : [self.view addSubview: [player view]];
movieController is an instance of MPMoviePlayerViewController declared in the .h file.
[movieController.moviePlayer play] is not required and the player will start regardless if you didn't set autoplay to NO, but I observed that if you put play in it it starts a bit quicker. This could be just a coincidence.
--------- If this is not working you can just do that following ----------
player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[player.view setFrame:CGRectMake(0, 0, 320, 320)];
[player play];
[self.view addSubview:player.view];
I am using MPMoviePlayerController to stream audio clip from a url. Currently the MPMoviePlayerController working fine on the current ViewController where the MPMoviePlayerController initiate and the play action initiate.
Also the streaming working fine when the app goes background.
But, if I dismiss or go back from the Current ViewController, MPMoviePlayerController stop playing.
But my requirement is MPMoviePlayerController should continue playing/streaming throughout the app regardless of the navigation through view controllers.
In other words, each and every view controller should have MPMoviePlayerController controls visible for user to pause or stop the play manually.
How do I do it?
This is my current code.
// Initiate Audio Player in ViewDidLoad of SecondViewController
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive:YES error:nil];
self.moviePlayer = [[MPMoviePlayerController alloc] init];
self.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
self.moviePlayer.view.hidden = NO;
[self.moviePlayer.view setFrame:CGRectMake(0, 0, videoContainer.frame.size.width, videoContainer.frame.size.height)];
[self.moviePlayer setControlStyle:MPMovieControlStyleDefault];
self.moviePlayer.backgroundView.hidden = YES;
[videoContainer addSubview:self.moviePlayer.view];
// Play method in SecondViewController
NSURL *audioUrl = [NSURL URLWithString:strMessageUrl];
self.moviePlayer.contentURL=audioUrl;
[self.moviePlayer prepareToPlay];
[self.moviePlayer play];
If user go back to FirstViewController, it stops
Put condition to check movie player is null or not.
if(!self.moviePlayer)
{
self.moviePlayer = [[MPMoviePlayerController alloc] init];
self.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
self.moviePlayer.view.hidden = NO;
[self.moviePlayer.view setFrame:CGRectMake(0, 0, videoContainer.frame.size.width, videoContainer.frame.size.height)];
[self.moviePlayer setControlStyle:MPMovieControlStyleDefault];
self.moviePlayer.backgroundView.hidden = YES;
[videoContainer addSubview:self.moviePlayer.view];
}
I am working on an app in Xcode which uses AVAudioPlayer.
#property (nonatomic, strong) AVAudioPlayer *player;
on tap of an image I call a function which plays the audio file:
-(void)playMusic{
if(self.player.currentTime == 0.0){
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/sound.mp3", [[NSBundle mainBundle] resourcePath]]] error:nil];
self.player.numberOfLoops = 0;
[self.player play];
}
}
on viewWillDisappear I need to stop the player, so I call:
-(void)viewWillDisappear:(BOOL)animated{
[self.player stop];
}
But the above code does not work, and the audio does not stop playing.
Can anyone please tell me what the issue could be ?
Why don't you stop Playing song with same image tap as,
if([player isPlaying] )
{
[player pause];
}
else
[player play];
How to play video on MPMoviePlayerController using ALAsset URL?
I tried to directly give the ALAsset URL to MPMoviePlayer using setContentURL: method, but it did not work.
if (!self.moviePlayer)
{
self.moviePlayer = [[MPMoviePlayerController alloc] init];
}
[self.moviePlayer setContentURL:[NSURL fileURLWithPath:playPath]];
self.moviePlayer.allowsAirPlay=YES;
self.moviePlayer.controlStyle=MPMovieControlStyleDefault;
[ScrollView addSubview: [self.moviePlayer view]];
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[self.moviePlayer prepareToPlay];
[self.moviePlayer play];
#Mahesh can you please try with this :
self.moviePlayer.moviePlayer.movieSourceType = MPMovieSourceTypeFile ;
Here i want to play videos from server using MediaPlayer framework. 1)I am adding mediaplayer framework. 2)I am importing #import header file 3)I implemented code by using google.
My Code is :
- (void)act_GoToVideoPlayer:(UIButton*)sender
{
NSString* resourcePath = [NSString stringWithFormat:#"http://%#",[postMediaFileArr objectAtIndex:sender.tag]]; //Video file URL is getting from server and stored it in a MutableArray
NSURL *url = [NSURL URLWithString: resourcePath];
mediaplayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:str]];
[mediaplayer.view setFrame:CGRectMake(20, 100, 250, 150)];
[self.view addSubview:mediaplayer.view];
mediaplayer.fullscreen = YES;
mediaplayer.allowsAirPlay = YES;
mediaplayer.shouldAutoplay = YES;
mediaplayer.controlStyle = MPMovieControlStyleEmbedded;
}
The view goes to blank screen and infinite loading when this function is called. I have tried many other versions of this implementation and the results are all fail. The log in the is particular case is:
_itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
If i passed any other videos from websites. it will be playing audios not videos.
Got ideas on the cause? This is my first venture into playing video and it has turned out to be a nightmare at this point.
-(void)notifyCompletelyVisible
{
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];
}
this is working just import
import "MediaPlayer/MediaPlayer.h"
you try in .h:
#import <MediaPlayer/MediaPlayer.h>
and you add delegate
interface YourViewcontroler : UIViewController <MPMediaPlayback>
{
MPMoviePlayerController *player;
}
and in your .m file:
-(void) launchVideo{
YourAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSString* myURL =#"http://yourUrl/file.mp4";
NSURL *urlString = [NSURL URLWithString:myURL];
player = [[MPMoviePlayerController alloc] initWithContentURL:urlString];
[player view].alpha=1;
[appDelegate.window.rootViewController.view addSubview:[player view]];
[player play];
}
and the delegate methode for finishing:
-(void)videoPlayBackDidFinish:(NSNotification*)notification {
[player.view removeFromSuperview];
[player stop];
player=nil;
[player release];
}
i hope this help you