Stop AvAudioPlayer in other View iOS - ios

I would like to stop the audio with AvAudioPlayer but when another view dissapear, There are two views:
The problem is that when view2 disappear, the audio doesn't stop... How I should do?
View1.h
#property (nonatomic, retain) AVAudioPlayer *audioPlayer;
- (IBAction)playAudio:(id)sender;
- (IBAction)stopAudio:(id)sender;
View1.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self playSound];
}
- (void) playSound
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"simpsonsTheme"
ofType:#"mp3"]];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
//[self.audioPlayer play];
[self playAudio:audioPlayer];
}
- (IBAction)playAudio:(id)sender {
[audioPlayer play];
}
- (IBAction)stopAudio:(id)sender {
[audioPlayer stop];
}
And View2.m
-(void)viewWillDisappear:(BOOL)animated{
view1 = (View1 *)[[UIApplication sharedApplication] delegate];
[view1 stopAudio:view1.audioPlayer];
}
In the View2 I import the View1 to do that.
Thanks

Your app delegate is not a view controller. Maybe you want [[UIApplication sharedApplication] delegate].window.rootViewController.
As you've seen, that's fragile. Instead of having pointers between your viewcontrollers, you could use have looser coupling and use a "stop" NSNotification. It may even be useful elsewhere in your application.
Define
NSString *StopPlayingVideoNotification = #"StopPlayingVideo";
So both views can see it, so put extern NSString *StopPlayingVideoNotification; in one of your header files.
In View1.m's init:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(stopAudio:) name:StopPlayingVideoNotification object:nil];
Add a dealloc to View1:
-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:StopPlayingVideoNotification object:nil];
}
In View2.m
-(void)viewWillDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] postNotificationName:StopPlayingVideoNotification object:self];
}

Related

How do I dismiss the AVPlayer automatically when playback has completed?

I have an app that plays local videos using the AVPlayerViewController.
But when the video has finished playing I want it to close automatically (like the MPMoviePlayerViewController used to do).
After posting this question, I managed to solve this partially thanks to the answers below and partially from Apples "Guides and Sample code".
This is the code that worked for me:
#import "ViewController.h"
#import AVFoundation;
#import AVKit;
#interface ViewController ()
#property(nonatomic, readonly) AVPlayerItem *currentItem;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)playerItemDidReachEnd:(NSNotification *) notification {
//remove the player
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)playVideo:(id)sender {
// grab a local URL to our video
NSURL *videoURL = [[NSBundle mainBundle]URLForResource:#"bagare" withExtension:#"mp4"];
// create an AVPlayer
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
// create a player view controller
AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
controller.player = player;
[player play];
[self presentViewController:controller animated:YES completion:nil];
// show the view controller
controller.view.frame = self.view.frame;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:_currentItem];
}
#end
NSURL *url=[NSURL URLWithString:#"URLLINK"];
AVAsset *avAsset = [AVAsset assetWithURL:url];
AVPlayerItem *playerItem = [[AVPlayerItem alloc]initWithAsset:avAsset];
Subscribe to the AVPlayerItem's DidPlayToEndTime notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
Pass the AVPlayerItem to a new player
AVPlayer *player = [AVPlayer playerWithURL:videoURL]
AVPlayerViewController *controller=[[AVPlayerViewController alloc]init];
controller.player=player;
[player play];
[self presentViewController:controller animated:YES completion:nil];
Here remove Player dismiss or Player added view
-(void)itemDidFinishPlaying:(NSNotification *) notification {
[self dismissViewControllerAnimated:YES completion:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
}
This should work:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(videoDidFinish:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[controller.player currentItem]];
- (void)videoDidFinish:(id)notification
{
AVPlayerItem *p = [notification object];
//do something with player if you want
[[NSNotificationCenter defaultCenter] removeObserver:self];
//fade out / remove subview
}
In .h file
#property(nonatomic, strong)AVPlayerViewController *controller;
In .m file:
- (IBAction)playVideo:(id)sender {
// grab a local URL to our video
NSURL *videoURL = [[NSBundle mainBundle]URLForResource:#"bagare" withExtension:#"mp4"];
// create an AVPlayer
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
// create a player view controller
self.controller = [[AVPlayerViewController alloc]init];
controller.player = player;
[player play];
[self presentViewController:controller animated:YES completion:nil];
// show the view controller
controller.view.frame = self.view.frame;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:_currentItem];
}
And for dismiss:
- (void)playerItemDidReachEnd:(NSNotification *)notification {
[self.controller dismissViewControllerAnimated:YES completion:nil];
}

How to play video without the full screen mode?

Is there a way to add video to a view like you would with an image and an image view? I don't want the full screen mode like in the YouTube app.
You can use AVPlayer from AVFoundation Framework
AVPlayer Demo
Try this code.
1.Import media player framework- #import
2.Declare Instance variable MPMoviePlayerController *player;
-(void)viewDidLoad
{
//you can try other api to get movie file path in ios 8
NSString *url = [[NSBundle mainBundle]
pathForResource:#"Trailer"
ofType:#"m4v"];
player = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
//—set the size of the movie view and then add it to the View window—
player.view.frame = CGRectMake(10, 10, 300, 300);
[self.view addSubview:player.view];
//—play movie—
[player play];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//—called when the movie is done playing—
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *moviePlayer = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer.view removeFromSuperview];
}
//-(NSString *)path
//{
// NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, //NSUserDomainMask, YES);
// NSString *documentDir=[paths objectAtIndex:0];
// return [documentDir stringByAppendingPathComponent:#"Trailer.m4v"];
//}

How to resume music after call

I have an online music player. I want to add a feature to it that if the song is being played and a call (incoming or outgoing) is made, it should pause the music which call is going on and after the call is disconnected, the music should start again.
here is the code I have:
//
// FirstViewController.m
#import "FirstViewController.h"
CM_EXPORT const CMTime kCMTimeZero;
#interface FirstViewController ()
#end
#implementation FirstViewController
#synthesize metadatas;
#synthesize toggleButton;
#synthesize slider;
#synthesize mpVolumeView = _mpVolumeView;
#synthesize viewVolume;
- (void)viewDidLoad
{
//[super viewDidLoad];
//slider.transform = CGAffineTransformRotate(slider.transform,270.0/180*M_PI);
//[slider setMaximumValue:2];
//[slider setMinimumValue:0];
//[slider setSelected:YES];
//[[self mpVolumeView] setBackgroundColor:[UIColor clearColor]];
//MPVolumeView *myVolumeView = [[MPVolumeView alloc] initWithFrame: [[self mpVolumeView] bounds]];
//[[self mpVolumeView] addSubview:myVolumeView];
//toggleIsOn =TRUE;
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
toggleIsOn=TRUE;
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:self.viewVolume.bounds] ;
[self.viewVolume addSubview:volumeView];
[volumeView sizeToFit];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(IBAction)playButtonPressed:(id)sender
{
if(toggleIsOn){
toggleIsOn=!toggleIsOn;
player = nil;
NSString *stringurl = #"";
stringurl = #"http://majestic.wavestreamer.com:6221/listen.pls";
NSURL *url = [NSURL URLWithString:stringurl];
asset = [AVURLAsset URLAssetWithURL:url options:nil];
playerItem = [AVPlayerItem playerItemWithAsset:asset];
player = [AVPlayer playerWithPlayerItem:playerItem];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[playerItem addObserver:self forKeyPath:#"timedMetadata" options:NSKeyValueObservingOptionNew context:nil];
[playerItem addObserver:self forKeyPath:#"status" options:NSKeyValueObservingOptionNew context:nil];
[player play];
[self.toggleButton setImage:[UIImage imageNamed:#"reload.png"] forState:UIControlStateNormal];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
else {
[self.toggleButton setImage:[UIImage imageNamed:#"playMusic.png"] forState:UIControlStateNormal];
self->player.rate=0.0;
toggleIsOn=!toggleIsOn;
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context {
[playerItem removeObserver:self forKeyPath:keyPath];
if ([keyPath isEqualToString:#"status"]) {
AVPlayerItem *pItem = (AVPlayerItem *)object;
if (pItem.status == AVPlayerItemStatusReadyToPlay)
{
metadatas.text = #"";
}
}
if ([keyPath isEqualToString:#"timedMetadata"]) {
for (AVAssetTrack *track in playerItem.tracks) {
for (AVPlayerItemTrack *item in player.currentItem.tracks) {
if ([item.assetTrack.mediaType isEqual:AVMediaTypeAudio]) {
NSArray *meta = [playerItem timedMetadata];
for (AVMetadataItem *metaItem in meta) {
NSString *source = metaItem.stringValue;
metadatas.text = source;
}
}
}
}
}
[self.toggleButton setImage:[UIImage imageNamed:toggleIsOn ? #"playMusic.png" :#"stop.png"] forState:UIControlStateNormal];
}
-(IBAction)fbButtonPressed:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://www.facebook.com"];
if (![[UIApplication sharedApplication] openURL:url])
NSLog(#"%#%#",#"Failed to open url:",[url description]);
}
-(IBAction)inButtonPressed:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://www.linkedin.com"];
if (![[UIApplication sharedApplication] openURL:url])
NSLog(#"%#%#",#"Failed to open url:",[url description]);
}
-(IBAction)tweetButtonPressed:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://www.twitter.com"];
if (![[UIApplication sharedApplication] openURL:url])
NSLog(#"%#%#",#"Failed to open url:",[url description]);
}
-(IBAction) sliderChanged:(id)sender
{
}
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
#end
Also, the code for inserting volume view is there, then also there is no volume controller in the UI. Why so.?
// // FirstViewController.m
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#class AVPlayer;
#class AVPlayerItem;
#interface FirstViewController : UIViewController
{
UIView *viewVolume;
AVAsset *asset;
AVPlayerItem *playerItem;
AVPlayer *player;
NSURL *mURL;
MPVolumeView *_mpVolumeView;
IBOutlet UILabel *metadatas;
IBOutlet UIButton *toggleButton;
BOOL toggleIsOn;
IBOutlet UISlider *slider;
}
-(IBAction)playButtonPressed:(id)sender;
-(IBAction)fbButtonPressed:(id)sender;
-(IBAction)inButtonPressed:(id)sender;
-(IBAction)tweetButtonPressed:(id)sender;
-(IBAction) sliderChanged:(id)sender;
#property (strong, nonatomic) IBOutlet UISlider *slider;
#property (nonatomic, retain) IBOutlet MPVolumeView *mpVolumeView;
#property (nonatomic, retain) IBOutlet UILabel *metadatas;
#property (nonatomic, retain) IBOutlet UIButton *toggleButton;
#property (nonatomic, strong) IBOutlet UIView *viewVolume;
#end
am unable to start the music after call. Please help with possible solutions.
You need to add observer in FirstViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(resumePlayMusic) name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (void) resumePlayMusic
{
.......
}
And don't forget to remove observer.
Go to your appDelegate file and there you will find the UIApplicationDelegate methods automatically having been implemented for you.
Simply add your music pause and resume code in the methods and everything else will be handled accordingly. Just make sure that you can access your music instance player from the app delegate.
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

MPMoviePlayerController.view isn't removed from superview

here is my code
- (void)viewDidLoad
{
...
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"movie" ofType:#"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[self.view addSubview:_moviePlayerController.view];
_moviePlayerController.fullscreen = YES;
_moviePlayerController.scalingMode = MPMovieScalingModeAspectFit;
_moviePlayerController.controlStyle=MPMovieControlStyleDefault;
[_moviePlayerController play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieIsOver:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
- (void)movieIsOver:(NSNotification *)notification
{
NSLog(#"movie is over");
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self.moviePlayerController.view removeFromSuperview];//moviePlayerController is MPMoviePlayerController
}
When the movie plays to the end, I can't see "movie is over" log and the moviePlayerController.view isn't removed. I don't know why.
EDIT:
MPMoviePlayerPlaybackDidFinishNotification works well.I see the "movie is over" log.The problem is moviePlayerController.view isn't removed.
I found out the solution:add
_moviePlayerController.fullscreen = NO;
before removing view from superview
Instead of adding _moviePlayerController.view as subview
[self.view addSubview:_moviePlayerController.view];
you can present _moviePlayerController just like this :
[self presentMoviePlayerViewControllerAnimated: _moviePlayerController];
and in movieIsOver method you simply dismiss
[self dismissMoviePlayerViewControllerAnimated];
I hope its help

How to Call Second XIB in IPad at Runtime?

In my application i run the video in my first class XIB using MPMoviePlayerController.my video durtion is about 20 seconds.i want that when my video ends its automatically call the Second Class XIB.here is my code.
-(void)viewWillAppear:(BOOL)animated
{
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"3idiots.mov" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:videoPlayer.view];
videoPlayer.view.frame = CGRectMake(0, 0,768, 1000);
[videoPlayer play];
[self performSelector:#selector(gotonextview)];
}
-(void)gotonextview
{
secondview *sec=[[secondview alloc] initWithNibName:#"secondview" bundle:nil];
[self presentModalViewController:sec animated:YES];
[sec release];
}
This code Give me No Error,but its not call the Second Class after video completion.can any body guide me. Thanx in advance
This is all explained in the docs... also behaviour various between iOS versions.
Do not call gotonextview from viewWillAppear. Instead register your view controller as an observer for MPMoviePlayerPlaybackDidFinishNotification and MPMoviePlayerDidExitFullscreenNotification in viewDidLoad with gotonextview:(NSNotification *)notification as the selector.
Also, I would suggest you launch the movie player from viewDidAppear rather than viewWillAppear.
EDIT: Adapted original posters code (untested)...
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(gotonextview:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(gotonextview:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
}
-(void)viewDidAppear:(BOOL)animated
{
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"3idiots.mov" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:videoPlayer.view];
videoPlayer.view.frame = CGRectMake(0, 0,768, 1000);
[videoPlayer play];
}
-(void)gotonextview:(NSNotification *)notification
{
NSDictionary *notifDict = notification.userInfo; // Please refer Apple's docs for using information provided in this dictionary
secondview *sec=[[secondview alloc] initWithNibName:#"secondview" bundle:nil];
[self presentModalViewController:sec animated:YES];
[sec release];
}
One option is:
Use a tabBarController with two tabs. Place your video in one tab and place your "second view" in the second tab. then use
self.tabBarController.selectedIndex=2;
The better way is to use a Timer or register an event listener to the video player.
Examples can be found under http://mobiledevelopertips.com/cocoa/basics-of-notifications.html

Resources