I have little problem in my iPhone application code but can not identify. please help.
bellow is my code gives me error.
this code is in loop.
- (void)playSoundSequence{
if(songCounter >= totalSoundsInQueue || songCounter < 0){
songCounter = 0;
}
NSLog(@"%d", songCounter);
sound = [[NSString alloc] initWithFormat:@"%@", [theSoundArray objectAtIndex:songCounter]];
NSLog(@"call");
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:sound ofType:@""]] error:NULL];
sound = nil;
[sound release];
audioPlayer.delegate = self;
audioPlayer.volume = m_volSlider.value;
[audioPlayer play];
m_progressBar.progress = 0;
m_progressSlider.value = 0;
m_progressSlider.maximumValue = audioPlayer.duration;
[m_btnPlay setBackgroundImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
bgTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateDisplay) userInfo:nil repeats:YES];
songCounter++;
}
songCounter is integer and one by one increment according to loop.
In this code theSoundArray is my array that contain like bellow
"song1.mp3",
"song2.mp3",
"song3.mp3",
"song4.mp3"
The problem is whenever first time my loop was called and “songCounter” was 0 it’s run fine. Then 2nd time goes in loop and “songCounter” was 1 then there have problem and gives me error.
Below is the error.
-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance
I hope you have not retained the array , and so when you are accessing that after some time, it crashes.
So, do as following while initializing the
theSoundArray. See theretainAnd try now.
EDIT
And the order is not correct in the following statements.
You release it and then set it to nil.