Here is my Code(ARC enabled) where memory leak is seen.
Please help me to solve this issue.
- (void) setMusic
{
/*
Initialise and set the music for the game.
*/
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"actionMusic" ofType:#"caf"]];
NSError *error = nil;
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (!error)
{
_audioPlayer.delegate = self;
if(_musicFlag)
{
[_audioPlayer play];
}
[_audioPlayer setNumberOfLoops:INT32_MAX];
}
error = nil;
url =nil;
url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"pew" ofType:#"wav"]];
_moveMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (!error)
{
_audioPlayer.delegate = self;
}
error = nil;
url =nil;
url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Won" ofType:#"wav"]];
_winningMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (!error)
{
_winningMusic.delegate = self;
[_winningMusic setNumberOfLoops:1];
}
}
I found the answer for the leak.
This leak is due to AVAudioPlayer foundation class which is yet to be fixed by Apple.
Thus Usage of AVAudioPlayer foundation class leads to leaks.
Check This link for more detail
Related
All device is playing the sound in foreground except iPod touch not playing the sound in foreground, When the app receive notification,
Below we have paste the exact code
NSString *playSoundOnAlert = #"Sound.wav";
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/%#",[[NSBundle mainBundle] resourcePath],playSoundOnAlert]];
NSError *error;
aVAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
aVAudioPlayer.numberOfLoops = 0;
[aVAudioPlayer play];
check the code:
NSString *playSoundOnAlert = #"Sound";
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle] pathForResource: playSoundOnAlert ofType:#".wav"];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
NSError *audioError = [[NSError alloc] init];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&audioError];
if (!audioError) {
[audioPlayer play];
NSLog(#"playing!");
}
else {
NSLog(#"Error!");
}
- (void)play {
NSURL *imgPath = [[NSBundle mainBundle] URLForResource:#"sound" withExtension:#"mp3"];
NSString *path = [imgPath path];
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
NSLog(#"%#",data);
NSError *error;
self.avPlayer = [[AVAudioPlayer alloc] initWithData:data error:&error];
[self.avPlayer play];
}
Rather then converting into NSData, you can pass URL to AVAudioPlayer.
NSURL *imgPath = [[NSBundle mainBundle] URLForResource:#"sound" withExtension:#"mp3"];
NSError *error;
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:imgPath
error:&error];
self.player.numberOfLoops = 0; //Infinite
self.player.delegate = self;
[self.player prepareToPlay];
[self.player play];
Your code works fine. The problem is while you try to Log the Data.
Or else try with initWithContentsOfURL method.
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#interface ViewController ()
#property (nonatomic, strong) AVAudioPlayer *avPlayer;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *imgPath = [[NSBundle mainBundle] URLForResource:#"1" withExtension:#"mp3"];
NSString *path = [imgPath path];
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
// NSLog(#"%#",data);
NSError *error;
self.avPlayer = [[AVAudioPlayer alloc] initWithData:data error:&error];
// self.avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:imgPath error:&error];
[self.avPlayer play];
}
I ran the allocations instruments and I definitely have a retain cycle, the stairs-like graph is a dead giveaway. However, I'm new to iOS programming and have no idea how to fix my retain cycle issue even after sifting through the internet for documentation. To my knowledge there are some strong references that are preventing unused memory from being deallocated in my ARC project. The app is a simple soundboard but has a lot of content, which is why I haven't had to deal with this issue before. Memory is a real problem now. For ViewController.m I have
#import GoogleMobileAds;
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#interface ViewController () <GADInterstitialDelegate, UIAlertViewDelegate>
{
AVAudioPlayer *_audioPlayer;
}
#property(nonatomic, strong) GADInterstitial *interstitial;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createAndLoadInterstitial];
BannerManager *sharedManager = [BannerManager sharedManager];
GADBannerView* bView = [sharedManager setupBannerAds:self.view];
bView.delegate = self;
bView.rootViewController = self;
GADRequest *request = [GADRequest request];
[bView loadRequest:request];
}
- (void)adViewDidReceiveAd:(GADBannerView *)bannerView {
NSLog(#"adViewDidReceiveAd: %#", bannerView.adNetworkClassName);
}
int counter = 0;
#pragma Interstitial button actions
- (IBAction)playAgain:(id)sender {
if (counter % 15 == 0) {
if (self.interstitial.isReady) {
[self.interstitial presentFromRootViewController:self];
} else {
}
}
counter++;
}
- (void)createAndLoadInterstitial {
self.interstitial =
[[GADInterstitial alloc] initWithAdUnitID:#"mygoogleidgoeshere"];
self.interstitial.delegate = self;
GADRequest *request = [GADRequest request];
[self.interstitial loadRequest:request];
}
#pragma mark GADInterstitialDelegate implementation
- (void)interstitial:(GADInterstitial *)interstitial
didFailToReceiveAdWithError:(GADRequestError *)error {
NSLog(#"interstitialDidFailToReceiveAdWithError: %#", [error localizedDescription]);
}
- (void)interstitialDidDismissScreen:(GADInterstitial *)interstitial {
NSLog(#"interstitialDidDismissScreen");
[self createAndLoadInterstitial];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)areyoufinishedyet:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"areyoufinishedyet"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
}
- (IBAction)areyoulooking:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"areyoulooking"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
}
- (IBAction)blueeyes:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"blueeyes"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
}
- (IBAction)crosshairs:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"crosshairs"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
}
- (IBAction)doyoumind:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"doyoumind"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
}
- (IBAction)dontblink:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"dontblink"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
}
- (IBAction)growontrees:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"growontrees"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
}
- (IBAction)iamagenius:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"iamagenius"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];}
- (IBAction)imarealboy:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"imarealboy"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
}
- (IBAction)importantbusiness:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"importantbusiness"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
}
- (IBAction)inmyeye:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"inmyeye"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
}
- (IBAction)justblinked:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"justblinked"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
}
- (IBAction)mustyoureally:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"mustyoureally"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
}
- (IBAction)nottheenemy:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"nottheenemy"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
}
- (IBAction)oddlyenough:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"oddlyenough"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];}
- (IBAction)staringcontest:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"staringcontest"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
}
- (IBAction)youreaklutz:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"youreaklutz"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];}
- (IBAction)youreamoron:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"youreamoron"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];}
- (IBAction)yourepissingmeoff:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"yourepissingmeoff"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];}
- (IBAction)challengeme:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"challengeme"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];}
- (IBAction)forerunners:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"forerunners"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];}
and for Viewcontroller.h I have
#import <UIKit/UIKit.h>
#import "BannerManager.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#import GoogleMobileAds;
#interface ViewController: UIViewController<GADBannerViewDelegate, AVAudioPlayerDelegate> {
NSURL *soundFile;
AVAudioPlayer *sound;
}
//343 GUILTY SPARK
- (IBAction)areyoufinishedyet:(id)sender;
- (IBAction)areyoulooking:(id)sender;
- (IBAction)blueeyes:(id)sender;
- (IBAction)crosshairs:(id)sender;
- (IBAction)doyoumind:(id)sender;
- (IBAction)dontblink:(id)sender;
- (IBAction)growontrees:(id)sender;
- (IBAction)iamagenius:(id)sender;
- (IBAction)imarealboy:(id)sender;
- (IBAction)importantbusiness:(id)sender;
- (IBAction)inmyeye:(id)sender;
- (IBAction)justblinked:(id)sender;
- (IBAction)mustyoureally:(id)sender;
- (IBAction)nottheenemy:(id)sender;
- (IBAction)oddlyenough:(id)sender;
- (IBAction)staringcontest:(id)sender;
- (IBAction)youreaklutz:(id)sender;
- (IBAction)youreamoron:(id)sender;
- (IBAction)yourepissingmeoff:(id)sender;
//ARBITER
- (IBAction)challengeme:(id)sender;
- (IBAction)forerunners:(id)sender;
- (IBAction)halospurpose:(id)sender;
- (IBAction)imgoingtocutit:(id)sender;
- (IBAction)imprepared:(id)sender;
- (IBAction)readytofight:(id)sender;
- (IBAction)sacredrings:(id)sender;
- (IBAction)saveyouranger:(id)sender;
- (IBAction)youshotmefoo:(id)sender;
- (IBAction)tararusstop:(id)sender;
- (IBAction)trymypatience:(id)sender;
- (IBAction)whatyouwant:(id)sender;
- (IBAction)arbiterdo:(id)sender;
//BRUTES
- (IBAction)adaysrations:(id)sender;
- (IBAction)atinymorsel:(id)sender;
- (IBAction)bastardelite:(id)sender;
- (IBAction)boilinyourpot:(id)sender;
- (IBAction)cannedmeat:(id)sender;
- (IBAction)cannotescape:(id)sender;
this is only part of the code because the rest is pretty huge, and is basically the same line of code over and over with minor changes to the files it references. My question is how in the world do I fix retain cycles? Is it something to do with my modal transitions? Because this is what's causing crazy memory allocation in instruments and crashes on my native iOS device.
It sounds to me like you don't have a retain cycle, exactly. It sounds like you are presenting a never-ending series of view controllers on top of other view controllers.
Using a modal present or a navigation controller push has a similar effect: The previous view controller gets covered with a new one but does not get freed.
You didn't post any of the code that shows how you navigate from view controller to view controller, so it's hard to tell what you're doing exactly.
If you want to go from view controller A to view controller B to view controller C, then back to A, you need to present from A to B, then from B to C, but dismiss in order to get back, not present a new copy of view controller A.
I have used the following code to initialise an AVAudioPlayer object with NSData, which in turn has the data of an mp3 in my main bundle.
NSError *error;
NSString *filePathForBgAudio = [[NSBundle mainBundle] pathForResource:#"beebuzz10" ofType:#"mp3"];
NSData *bgAudioData = [NSData dataWithContentsOfFile:filePathForBgAudio];
bgAudioPlayer = [[AVAudioPlayer alloc] initWithData:bgAudioData error:&error];
bgAudioPlayer.delegate = self;
I have declared bgAudioPlayer in the .h file. But when I checked by setting breakpoint, I found that after this line bgAudioPlayer = [[AVAudioPlayer alloc] initWithData:bgAudioData error:&error]; the bgAudioPlayer is still shown to be nil. But the filePathForBgAudio and bgAudioData are initialised correctly and has data. just the bgAudioPlayer is nil. What is the problem in the above code? How to rectify this?
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"bubble"
ofType:#"mp3"]];
NSError *error;
_audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
if (error)
{
NSLog(#"Error in audioPlayer: %#",
[error localizedDescription]);
} else
{
_audioPlayer.delegate = self;
[_audioPlayer prepareToPlay];
}
}
- (IBAction)playsound:(id)sender
{
[_audioPlayer play];
}
NSError *error;
NSURL *filePathForBgAudio1 =[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"beebuzz10" ofType:#"mp3"]];
//NSString *filePathForBgAudio = [[NSBundle mainBundle] pathForResource:#"beebuzz10" ofType:#"mp3"];
// NSData *bgAudioData = [NSData dataWithContentsOfFile:filePathForBgAudio1];
bgAudioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:filePathForBgAudio1 error:&error];
I'm trying to play remote .mp3 file
but its giving the following error.
The operation couldn’t be completed. (OSStatus error -43.)
here is my code :
- (void)viewDidLoad
{
[super viewDidLoad];
isPlaying = NO;
/*
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"Dar-e-Nabi-Per" ofType:#"mp3"]];
*/
NSURL *url = [NSURL
URLWithString:#"http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
if (error)
{
NSLog(#"Error in audioPlayer: %#",
[error localizedDescription]);
}
else
{
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
}
}
Can anyone please guide me where i'm doing mistake
For steaming audio from a remote server, use AVPlayer instead of AVAudioPLayer.
AVPlayer Documentation
Sample Code:
- (void)playSampleSong:(NSString *)iSongName {
NSString *aSongURL = [NSString stringWithFormat:#"http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"];
// NSLog(#"Song URL : %#", aSongURL);
AVPlayerItem *aPlayerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:aSongURL]];
AVPlayer *anAudioStreamer = [[AVPlayer alloc] initWithPlayerItem:aPlayerItem];
[anAudioStreamer play];
// Access Current Time
NSTimeInterval aCurrentTime = CMTimeGetSeconds(anAudioStreamer.currentTime);
// Access Duration
NSTimeInterval aDuration = CMTimeGetSeconds(anAudioStreamer.currentItem.asset.duration);
}
Use this to play song from URL
NSData *songData=[NSData dataWithContentsOfURL:url];
self.player = [[AVAudioPlayer alloc] initWithData:songData error:nil];
self.player.numberOfLoops=0;
_player.delegate=self;
[_player prepareToPlay];
[_player play]