I am doing one application. In that I used the AVPlayerViewController to play the video. But its not dismiss after play the video.
Please check the my source:
_player = [AVPlayer playerWithURL:self.videoURL];
_avVideoController = [[AVPlayerViewController alloc]init];
_avVideoController.view.frame = self.view.frame;
_avVideoController.delegate =self;
_avVideoController.player.actionAtItemEnd = AVPlayerActionAtItemEndAdvance;
_avVideoController.player = _player;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[_avVideoController.player currentItem]];
[_player play];
[self.view addSubview:_avVideoController.view];
[self presentViewController:_avVideoController animated:YES completion:nil];
- (void)playerItemDidReachEnd:(NSNotification *)notification
{
[_avVideoController dismissViewControllerAnimated:YES completion:nil];
[_avVideoController.view removeFromSuperview];
NSLog(#"IT REACHED THE END");
}
After this AVPlayerViewController is not dismissing.
Not hundred percent sure, but might be because of NSNotificationCenter still keeps a reference to self. Try to remove observer in
- (void)playerItemDidReachEnd:(NSNotification *)notification function. As of that
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AVPlayerItemDidPlayToEndTimeNotification
object:nil];
Related
I'm working on a iPad project(xcode 7.21+iOS9) and NSNotificationCenter doesn't work.
When user open my app, the tab bar controller will appear.
- (void)viewWillAppear:(BOOL)animated {
if (false == [[MyClass sharedData] getLoginStatus])
{
LoginViewController *loginViewController = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"myCustomPopoverLoginVC"];
loginViewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:loginViewController animated:YES completion:^{
}];
...
}
}
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(notificationLogin:) name:#"afterLogin" object:nil];
[super viewDidLoad];
...
}
-(void)notificationLogin:(NSNotification *)notification{
NSLog(#"OhOhOh");
}
In my loginView,
-(IBAction)login:(id)sender{
...
[[NSNotificationCenter defaultCenter] postNotificationName:#"afterLogin" object:nil];
...
}
First of all:
- (void)viewWillAppear:(BOOL)animated {
if (false == [[MyClass sharedData] getLoginStatus])
{
LoginViewController *loginViewController = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"myCustomPopoverLoginVC"];
loginViewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:loginViewController animated:YES completion:^{
}];
...
}
}
- (void)viewDidLoad
{
// Add log here to check when its called
-------> NSLog("Add Observer");
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(notificationLogin:) name:#"afterLogin" object:nil];
[super viewDidLoad];
...
}
-(void)notificationLogin:(NSNotification *)notification{
NSLog(#"OhOhOh");
}
then add another log here :
-(IBAction)login:(id)sender{
...
-------> NSLog("Post notification");
[[NSNotificationCenter defaultCenter] postNotificationName:#"afterLogin" object:nil];
...
}
So you can check what called first. Then refer #Sandeep Kumar comment :))
NSNotificationCenter doesn't work in popup view(ios objC)
I want to remove the transition animation that occur in MPMoviePLayerController when the user presses done button . I used to stop it when the movie finishes on its own using the moviePlayBackDidFinish: notification but its doesn't work, as in, the animation occurs. this code i used.
NSURL *fileURL=[NSURL URLWithString:mediaurl1];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController.view setFrame:CGRectMake(0, 150, 320, 270)];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(doneButtonClick:)
name:MPMoviePlayerWillExitFullscreenNotification
object:moviePlayerController];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(donefinished:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[self presentMoviePlayerViewControllerAnimated:moviePlayerController];
[mediaView addSubview:moviePlayerController.view];
[moviePlayerController setFullscreen:YES animated:NO];
moviePlayerController.useApplicationAudioSession = YES;
[moviePlayerController play];
this is my notification method to close the animation effect.
-(void)doneButtonClick:(NSNotification*)aNotification{
[mediaView removeFromSuperview ];
[moviePlayerController setFullscreen:NO animated:NO];
[self dismissViewControllerAnimated:NO completion:NO];
// [moviePlayerController setFullscreen:NO animated:NO];
}
-(void)donefinished:(NSNotification*)aNotification{
[mediaView removeFromSuperview ];
[moviePlayerController setFullscreen:NO animated:NO];
[self dismissViewControllerAnimated:NO completion:NO];
// [moviePlayerController setFullscreen:NO animated:NO];
}
Use it.
-(void)donefinished:(NSNotification*)aNotification
{
[moviePlayerController stop];
[moviePlayerController.view removeFromSuperview];
moviePlayerController = nil;
}
Volume Control is not showing such that slider .
I search many answer on Stackoverflow regarding this but No code is working well.
Most of the Answers I see there is a slider which control volume and added to subview I also try this but no slider.
- (void)play
{
NSURL *url=[[NSURL alloc] initWithString:self.url];
player=[[MPMoviePlayerController alloc] initWithContentURL:url];
[player prepareToPlay];
player.allowsAirPlay = YES;
player.scalingMode = MPMovieScalingModeAspectFit;
player.view.frame = self.view.frame;
player.controlStyle = MPMovieControlStyleFullscreen;
player.useApplicationAudioSession = YES;
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:player];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerWillExitFullscreen:)
name:MPMoviePlayerWillExitFullscreenNotification
object:player];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
if ([UIScreen mainScreen].bounds.size.height == 568)
{
[[player view] setBounds:CGRectMake(0, 0, 568, 320)];
}
else
{
[[player view] setBounds:CGRectMake(0, 0, 480, 320)];
}
// [[player view] setBounds:CGRectMake(0, 0, 480, 320)];
// [[player view] setCenter:CGPointMake(160, 240)];
[[player view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
[[MPMusicPlayerController applicationMusicPlayer] setVolume:0];
[self.view addSubview: player.view];
// [ self.view addSubview: systemVolumeSlider];
[player play];
}
I am trying to play a video stream from the internet on the iPhone by pressing a button.
I used many code samples but nothing worked. With this code it opens a black view without any video stream or controls in it. (The stream itself works.)
NSURL *url = [NSURL URLWithString:#"http://MyStreamURL.com"];
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];
Instead of creating a MPMoviePlayerController and adding that to your view, it is probably simpler to create a MPMoviePlayerViewController and present that view controller modally (since you are trying to show your video full screen anyway). Then the MPMoviePlayerViewController can manage the presentation of your video for you.
MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
mpvc.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[self presentMoviePlayerViewControllerAnimated:mpvc];
[mpvc release];
In your moviePlayBackDidFinish delegate method you can then dismiss the model view controller.
Need to mention movie source type as streaming
moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
Add AVFoundation frame work in Link Libraries section
In your .h file add
#import <MediaPlayer/MediaPlayer.h>
#interface video_liveViewController : UIViewController<MPMediaPickerControllerDelegate,MPMediaPlayback>
In your .m file
NSURL *movieURL = [NSURL URLWithString:#"http://172.31.17.252:1935/live/myStream/playlist.m3u8"];
movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:movieController];
[movieController.moviePlayer play];
just add "MPMovieSourceTypeStreaming" to "moviesourcetype"
I have a problem opening a MPMoviePlayerController in fullscreen mode starting from a presentModalViewController.
I open a presentModalViewController
//Create controller
mDetailFilmController = [[DetailFilmController alloc]
initWithNibName:#"DetailFilmController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:mDetailFilmController];
//Set modal style
navController.modalPresentationStyle = UIModalPresentationPageSheet;
navController.navigationBar.barStyle = UIBarStyleBlack;
//presentModalViewController
[self presentModalViewController:navController animated:YES];
//Dimension
navController.view.superview.bounds = CGRectMake(0, 0, 700, 700);
in DetailFilmController there's a MPMoviePlayerController and this is the code for play button:
self.mPlayer = [[MPMoviePlayerController alloc] init];
//set url
self.mPlayer.contentURL = btn.linked_url;
//set dimension
self.mPlayer.view.frame = self.view.frame;
//AddSubView
[self.mViewForMovie addSubview:mPlayer.view];
//Notify
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_moviePlayerDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:mPlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_moviePlayerDidFinish:) name:MPMoviePlayerDidExitFullscreenNotification object:mPlayer];
//fullscreen
[self.mPlayer setFullscreen:YES animated:YES];
//start play
[self.mPlayer play];
the problem occured when i try to click done button, this doesn't work.
Only in the area of the presentModalViewController below is possible to intercept the click.
What did I do wrong?
thanks for help
Don