我有这种播放 mp3 的方法:
- (void) playSound:(NSString*)sound{
NSString *path = [NSString stringWithFormat:@"%@/%@",
[[NSBundle mainBundle] resourcePath],
sound];
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
[player prepareToPlay];
[player play];
}
每次我调用这个方法时,我都会得到一个新的字符串“sound”,然后每次我都必须分配这个 AVAudioplayer“player”;我想在停止或完成时释放它...这可能吗?
最佳答案
试试下面的指令
你的ViewController.h
@interface yourViewController : UIViewController <AVAudioPlayerDelegate>
添加功能的你的ViewController.m文件
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
//play finished
}
在代码下面声明 AVAudioPlayer 对象
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL
fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"audio" ofType:@"mp3"]] error:NULL];
audioPlayer.delegate = self;
audioPlayer.currentTime=0.0f;
[audioPlayer prepareToPlay];
[audioPlayer play];
谢谢...!
关于IOS:AVAudioPlayer管理内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10242678/