如何从AVAudioPlayer释放NSDATA?

时间:2014-02-25 10:21:29

标签: ios objective-c memory-management avaudioplayer

在这里,我曾经使用AVAudioPlayer Framework播放歌曲。希望它正在播放,但如果我点击Songslist中的其他歌曲,我会检查条件:

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];

1 个答案:

答案 0 :(得分:2)

AVAudioPlayer#data是一个只读的非保留属性。因此,数据不会被保留,您也无需将其重新发布(默认情况下为assigned)。

但是,正如我所说的那样,它是read-only所以如果你想改变曲目你就不能设置它。您也不应该在已初始化的对象上调用init...,如代码中所示:

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 Overview