iPhone初學者在這裏,我正在創建一個名爲Piano Master的簡單音樂iPhone應用程序,當單擊按鈕時會產生聲音。Obj-C iPhone:內存管理
這裏是我的代碼:
MusicViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import "PianoMasterAppDelegate.h"
@interface MusicViewController : UIViewController
<AVAudioPlayerDelegate> {}
- (IBAction)buttonClick:(id)sender;
@end
MusicViewController.m
#import "MusicViewController.h"
@implementation MusicViewController
- (IBAction)buttonClick:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"Piano1" ofType:@"wav"];
AVAudioPlayer *theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path error:NULL];
theAudio.delegate = self;
[theAudio play];
}
單擊該按鈕時,播放聲音,但每次點擊按鈕時,一新的AVAudioPlayer被創建,關於如何有效地管理這個內存問題,你最好的想法是什麼?我曾經想過爲MusicViewController製作一個AVAudioPlayer的實例並使用它,但是如果我每次都繼續分配一個新的AVAudioPlayer,那麼仍然會出現內存問題... 任何幫助都很有用,謝謝。
當我按下按鈕時沒有聲音播放...... – user544359 2011-03-06 00:39:28