IOS:AVAudioPlayer管理内存

IOS:AVAudioPlayer管理内存,ios,xcode,avaudioplayer,Ios,Xcode,Avaudioplayer,我有这个方法来播放mp3: - (void) playSound:(NSString*)sound{ NSString *path = [NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], sound]; NSURL *filePath = [NSURL fileURLWithPath:path

我有这个方法来播放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”时,我都必须分配该字符串;我想在停止或完成时释放它……可能吗?

看看AVAudioPlayerDelegate


它具有audioPlayerDidFinishPlaying:successfully:和audioplayerdecodeerrorrdoccur:error:方法,您可以使用这些方法执行所需操作。

请尝试以下说明

yourViewController.h

 @interface yourViewController : UIViewController <AVAudioPlayerDelegate>
在下面的代码中声明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];
谢谢

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