在这里,我曾经使用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];
答案 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
被回收(因为它未被保留)。