Maverick1st
Maverick1st

Reputation: 3770

AVAudioPlayer returns nil at random

I have the following lines in my code:

NSString *soundPath = [[NSBundle mainBundle] pathForResource:[[soundString componentsSeparatedByString:@"."]objectAtIndex:0] ofType:@"wav"];
NSURL *soundURL_ = [NSURL fileURLWithPath:soundPath];
av_player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL_ error:nil];

These lines are in a class calles AnimatableObject which i use about 10 to 30 times in one ViewController.

It all works perfectly the first few times the ViewControllers get initialized and loaded. But after switching the ViewControllers some time my app crashes because the following line (which had worked all the times before) returns nil right now.

av_player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL_ error:nil];

I checked if soundPath or soundURL_ are nil. But they are ok. Exactly the same value like all the times before when it worked.

So now I ask myself how can it happen, that it works a lot of times and than returns nil? In the same app, without closing and starting it again. And all I do is switching between viewControllers. I first thought it is an memory issue, but i don't get any memory warnings.

I really have no idea why this is happening.

Any help would be appreciated. Thx in advance, Maverick.

--------Edit--------

I now found out that i get the following error:

ImageIO: CGImageRead_mapData 'open' failed '/var/mobile/Applications/F3186E1A-BBC2-47A6-8EF6-90F73C9BD566/huckla_zoo.app/Weiter.png'
error = 24 (Too many open files)

so i realize that i cannot open a soundfile when i get this error. Only thing i wonder now is: How can i close file-handles? I don't load the images via [UIImage imageNamed:] but with

[UIImage imageWithContentsOfFile:filename];

I also have implemented a MutableDictionary as image cache which gets emptied, when memory issues occur. Is there any other option for me to close my file-handles?

Upvotes: 0

Views: 2586

Answers (2)

Jonathan Grynspan
Jonathan Grynspan

Reputation: 43470

From the error, it sounds like you've opened a lot of files, and forgotten to close them. Are you releasing file objects when you're done? How many files are you working with?

Upvotes: 0

Dilip Lilaramani
Dilip Lilaramani

Reputation: 1154

TRY THIS,

NSString* URL = [YOUR_URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Upvotes: 2

Related Questions