2011-03-02 3 views
0

iPhone 초보자 여기서는 피아노 마스터라는 간단한 음악 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]; 
} 

버튼을 클릭 할 때 소리가 재생하지만, 매번 버튼을 클릭하면, A : 여기

내 코드입니다 새로운 AVAudioPlayer가 생성되면,이 메모리 문제를 효율적으로 관리하는 방법에 대한 가장 좋은 아이디어는 무엇입니까? MusicViewController에 대한 AVAudioPlayer의 인스턴스를 만들고이를 사용하려고 생각했지만 매번 새로운 AVAudioPlayer를 계속 할당하면 여전히 메모리 문제가 발생합니다 ... 도움이됩니다. 감사합니다.

답변

1

오디오 플레이어를 초기화 한 후 다른 컨트롤러 (MVC) 객체에 보관하십시오. 그런 다음 뷰 컨트롤러에서 오디오 컨트롤러 개체를 호출하여 기존 컨트롤러를 다시 사용하기 만하면됩니다.

0

hotpaw2 님의 답변은 제가하고 싶은 것입니다.

하지만 가장 중요한 것은 메모리 관리입니다. 다음을 추가해야합니다.

[theAudio play]; 
[theAudio release]; 

메모리에 할당하는 항목을 해제해야합니다. 매번 AVAudioPlayer를 만들지 만. 사용 된 메모리가 해제됩니다. 그래서 당신은 누출을 얻지 않습니다.

0

MusicViewController.h 파일

#import <UIKit/UIKit.h> 
#import <AVFoundation/AVFoundation.h> 
#import "PianoMasterAppDelegate.h" 

@interface MusicViewController : UIViewController <AVAudioPlayerDelegate> { 
    AVAudioPlayer* _theAudio; 
} 

- (IBAction)buttonClick: (id)sender; 

@end 

MusicViewController.m 파일

#import "MusicViewController.h" 

@implementation MusicViewController 

- (void)dealloc { 
    [_theAudio release]; 
    [super dealloc]; 
} 

- (AVAudioPlayer*)theAudio { 
    if (_theAudio == nil) { 
     NSString* path = [[NSBundle mainBundle] pathForResource: @"Piano1" ofType: @"wav"]; 
     _theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path error: nil]; 
     [_theAudio setDelegate: self]; 
    } 
    return _theAudio; 
} 

- (IBAction)buttonClick: (id)sender { 
    [self.theAudio play]; 
} 

당신은 또한 당신이 전무 포인터를 설정해야합니다, 메모리를 확보하기 위해 viewDidUnload 방법에 theAudio을 해제 할 수 있습니다 다음과 같은 단어 :

- (void)viewDidUnload { 
    [_theAudio release]; 
    _theAudio = nil; 
} 
0

당신이 공동 uple 다른 오디오 파일, AVAudioPlayers 사전 잘 작동합니다. 예를 들어 사운드 파일이 처음 요청되면 AVAudioPlayer 객체를 만들어 사운드 사전에 넣은 다음 각 요청에 대해 사전에서 기존 AVAudioPlayer 객체를 가져옵니다. AVAudioPlayer 후, "오토 릴리즈"는 '소리'사전에 추가됩니다

@interface MusicViewController : UIViewController <AVAudioPlayerDelegate> { 
    NSMutableDictionary *sounds; 
} 
- (IBAction)buttonClick:(id)sender; 
- (AVAudioPlayer *)playerForSoundFile:(NSString *)fileName; 
@end 


@implementation MusicViewController 

- (id)init 
{ 
    if (! (self = [super init])) 
     return nil; 

    sounds = [[NSMutableDictionary alloc] init]; 

    return self; 
} 

- (void)dealloc 
{ 
    [sounds release]; 
    [super dealloc]; 
} 

- (AVAudioPlayer *)playerForSoundFile:(NSString *)fileName 
{ 

    AVAudioPlayer *player = [sounds objectForKey:fileName]; 

    if (! player) { 
     NSString *path = [[NSBundle mainBundle] pathForResource:fileName 
                 ofType:@"wav"]; 
     NSURL *url = [NSURL fileURLWithPath:path]; 
     player = [[[AVAudioPlayer alloc] initWithContentsOfURL:url] autorelease]; 
     player.delegate = self; 

     [sounds setObject:player forKey:fileName]; 
    } 

    return player; 
} 

- (IBAction)buttonClick:(id)sender 
{ 
    AVAudioPlayer *theAudio = [self playerForSoundFile:@"Piano1"]; 
    [theAudio play]; 
} 

@end 

참고 :

여기에 간단한 구현입니다. 이것은 사전이 파기되었을 때 모든 AVAudioPlayers도 마찬가지라는 것을 의미합니다. 이것이 명확하지 않은 경우 Objective-C의 메모리 관리를 읽어야합니다. http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/MemoryMgmt/MemoryMgmt.html

+0

버튼을 눌렀을 때 소리가 나지 않습니다. – user544359