IOS SDK: playing music in the background and switching views

I am trying to play music in my application. The music works great, but after switching the viewing modes and returning to the main menu, my music plays again! This means that several identical sounds play together! How can i solve this? Here is my code:

- (void)viewDidLoad { NSString *music = [[NSBundle mainBundle] pathForResource:@"1music" ofType:@"mp3"]; myMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:music] error:NULL]; myMusic.delegate = self; myMusic.numberOfLoops = -1; [myMusic play]; } - (IBAction) scoreView { ScoreViewController *scoreView = [[ScoreViewController alloc] initWithNibName:@"ScoreViewController" bundle:nil]; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:1]; [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES]; [self.view addSubview: scoreView.view]; [UIView commitAnimations]; } 

EDITED CODE:

  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { NSString * musicSonati = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"]; myMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicSonati] error:NULL]; myMusic.delegate = self; myMusic.numberOfLoops = -1; [myMusic play]; } return self; } //toggle button - (IBAction)MusicPlaying:(id)sender { if ((isPlay = !isPlay)) { UIImage *buttonImageNormal = [UIImage imageNamed:@"play.png"]; UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:0 topCapHeight:0]; [MusicButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal]; [myMusic pause]; }else { UIImage *buttonImageNormal = [UIImage imageNamed:@"pause.png"]; UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:0 topCapHeight:0]; [MusicButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal]; NSLog(@"Music play"); [myMusic play]; } } 
+4
source share
5 answers

Make sure myMusic is correctly declared as a saved property and synthesized, then try something like this:

 -(void) commonInit { NSString * musicSonati = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"]; self.myMusic = [[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicSonati] error:NULL] autorelease]; self.myMusic.delegate = self; self.myMusic.numberOfLoops = -1; //You probably don't want to play music on init, rather defer to viewDidLoad. // [self.myMusic play]; } - (id)initWithCoder:(NSCoder *)coder { self = [super initWithCoder:coder]; if (self) { [self commonInit]; } return self; } - (id)init { self = [super init]; if (self) { [self commonInit]; } return self; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { [self commonInit]; } return self; } 

Then try this in your DidLoad view:

 - (void)viewDidLoad { ... if (!self.myMusic.playing) { [self.myMusic play]; } ... } 

Also, do not forget to free the player in dealloc!

+4
source

If you have sound to play in multiple places, then one simple way to achieve this is to declare an AVAudioPlayer instance in the delegate with the synthesized property. If you want to reproduce it anywhere, do the following: -

 MyAppDelegate *app_delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate]; if(app_delegate.audioPlayer) { [app_delegate.audioPlayer release]; app_delegate.audioPlayer = nil; } app_delegate.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; 

and if you want to stop the sound, do the following: -

 if(app_delegate.audioPlayer) { if([app_delegate.audioPlayer isPlaying]) { [app_delegate.audioPlayer stop]; [app_delegate.audioPlayer setCurrentTime:0]; } } 

and play the new version of the sound file and zero of the same player, if it is already selected, or transfer it again using the new file of the sound file.

+5
source

Firstly, viewDidLoad is probably not a good place to initialize your AVAudioPlayer , as the system may need to unload some of your components to recover memory (in viewDidUnload ). You should AVAudioPlayer create AVAudioPlayer in your init method or similar.

So what happens, you create a second AVAudioPlayer when the focus returns to your application, and you actually lose the link to your first. (Thus, also a memory leak. )

If you want to start playing music when downloading a presentation, you must additionally check its status before doing this:

 - (void)viewDidLoad { ... if (!myMusic.playing) { [myMusic play]; } ... } 
+1
source

It seems that you are developing the game (on behalf of your function After viewLoad (ScoreView) .;))

Well, for me, AvAudioplayer is not a good option if you want to implement the game.

Use cocosdenshion, Download the latest cocos2d folder, find these 9 files, put them in your resources folder,

And you finished .. !!!

 #import "SimpleAudioEngine.h" // wherever you want to call in head part of the file. 

Step1 call the short file.!

 [[SimpleAudioEngine sharedEngine] playEffect:@"blast.mp3"]; 

step2 call the background loop.

 [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"background.mp3" loop:YES]; 

NOTE. List of available functions http://www.cocos2d-iphone.org/api-ref/0.99.5/interface_simple_audio_engine.html , it is very simple and simple!

The whole implementation will take 20 minutes!

+1
source

Initialize the player in the application delegate file and play background music. Just set one variable in the delegate of the application that checks the music playing true or false ... and enjoy your application without restarting the music ......

In the code that you initialized, you played in boot and initialization mode ... why do you reload the music ... just call it only once in the app delegation applicationdidfinshLaunching () .......

+1
source

Source: https://habr.com/ru/post/1385790/


All Articles