释放声音和内存管理

时间:2010-10-07 13:38:38

标签: iphone objective-c

你好我的应用程序我使用下面的代码处理和播放声音,我想知道我是否需要发布任何东西或什么都不做。

代码:

 -(void) playNote: (NSString*) note type: (NSString*) type
{

CFURLRef        soundFileURLRef;
SystemSoundID   soundFileObject;
CFBundleRef mainBundle;
mainBundle = CFBundleGetMainBundle ();
    // Get the URL to the sound file to play
    soundFileURLRef  =  CFBundleCopyResourceURL (mainBundle,
            (CFStringRef)note,
            (CFStringRef)type,
            NULL);

    // Create a system sound object representing the sound file
    AudioServicesCreateSystemSoundID (soundFileURLRef, &soundFileObject);

    AudioServicesPlaySystemSound (soundFileObject);
}

由于 ,

1 个答案:

答案 0 :(得分:2)

您需要致电

AudioServicesDisposeSystemSoundID

完成声音后进行清理。

在标题中

@interface xyz
{
  SystemSoundID soundFileObject;
};
在你的.m文件中,创建一个init方法(如果你没有):

-(id)init
{
   if ( (self = [super init]) )
   {
     AudioServicesCreateSystemSoundID (soundFileURLRef, &soundFileObject);
   }
   return self;
}
你的dealloc

中的

-(void)dealloc
{
   AudioServiceDisposeSystemSoundID(soundFileObject);
   [super dealloc];
}

你应该将soundFileURLRef声明为ivar