How to free NSDATA from AVAudioPlayer?

Here I played with songs using AVAudioPlayer Framework. I hope he plays, but if I click on other songs from the list of songs, I will check the condition:

if(player.isPlaying == YES)
{
    [player stop];
    // And also i need to release or remove the existing NSDATA from the Player. otherwise the player won't release the existing data. So the memory pressure occurring in my project. 
}
self.audioPlayer = [self.audioPlayer initWithData:m_currentMusic.fileData error:&error];
[self.audioPlayer prepareToPlay];
[self.audioPlayer play];
+3
source share
1 answer

AVAudioPlayer#data- A non-persistent read-only property. This way, the data is not saved, and you do not need to free it (by default this assigned).

However, as I said, it is read-only, therefore, you cannot install it if you want to change the tracks. You should also not call init...for an already initialized object, as in your code:

self.audioPlayer=[self.audioPlayer initWithData:m_currentMusic.fileData error:&error];

, , , - self.audioPlayer , :

// stop player as in your original code...
[self.audioPlayer release];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithData:m_currentMusic.fileData error:&error];
self.audioPlayer = newPlayer;
[self.audioPlayer setDelegate:self];
[newPlayer release];

, release data ( ).

. Core Audio

+2

All Articles