IOS:AVAudioPlayer管理内存

标签 ios xcode avaudioplayer

我有这种播放 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/

相关文章:

iphone - xcode 4 调试显示大多数对象为 'Summary Unavailable'

ios - 使用 Swift 缩放 ImageView

ios - AVAudioPlayer 缓慢降低级别

swift - AudioPlayer 和锁屏/控制中心控制 Swift

ios - Phonegap 谷歌地图和白名单拒绝

iphone - 使用 map 缓存模态视图

iphone - 如何获取 UITableview 中第一个单元格 (x, y) 的确切起始位置?

objective-c - iOS:禁用 UINavigationController

ios - Xcode 6 Beta7 NSDictionary 到 Swift

ios - 将变量从 iOS 传递到 watchOS