2014-11-14 8 views
1

나는 이것에 잠시 붙어있다. 그래서 내 애플 리케이션에서 나는 소리를 재생 버튼을해야합니다. 사용자가 버튼 (button1.png)을 클릭하면 이미지를 (button2.png)로 변경하고 사운드 재생이 끝나면 그림을 원래 이미지로 변경하려고합니다. 콜백이 이것을 설정하는 것이 가장 좋지만 문제가있는 것 같습니다. 도움은 감사 될 것입니다.콜백 함수를 설정하는 방법은 무엇입니까?

여기

#import "ViewController.h" 
#import <AudioToolbox/AudioToolbox.h> 

@interface ViewController() 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
[scrollView setScrollEnabled:YES]; 
// change setContentSize When making scroll view Bigger and adding more items 
[scrollView setContentSize:CGSizeMake(320, 1000)]; 

} 
- (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

#pragma mark - CallBackMethods 









#pragma mark - SystemSoundIDs 
SystemSoundID sound1; 







#pragma mark - Sound Methods 
-(void)playSound1 
{ 
NSString* path = [[NSBundle mainBundle] 
        pathForResource:@"Sound1" ofType:@"wav"]; 
NSURL* url = [NSURL fileURLWithPath:path]; 
AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &sound1); 


static void (^callBAck)(SystemSoundID ssID, void *something); 

callBAck = ^(SystemSoundID ssID, void *something){ 
    [button1 setImage:@"WhiteButton.png" forState:UIControlStateNormal]; 
}; 

AudioServicesAddSystemSoundCompletion(sound1, 
             NULL, 
             NULL, 
             callback, 
             NULL); 

AudioServicesPlaySystemSound(sound1); 
} 
- (IBAction)button:(id)sender { 
NSLog(@"Hello"); 
[button1 setImage:[UIImage imageNamed:@"ButtonPressed.png"] forState:UIControlStateNormal]; 
[self playSound1];  
} 
@end 

답변

0

AudioToolboxC 프레임 워크합니다 (C 스타일의 함수 호출을주의) 내 코드입니다. 콜백은 C function pointer이어야합니다. 당신이 다시 호출 AudioServicesAddSystemSoundCompletion의 4 번째 인수로 전달하는 데 필요한 AudioServicesSystemSoundCompletionProc 유형을 보면

:

typedef void (*AudioServicesSystemSoundCompletionProc) (SystemSoundID ssID, void *clientData);

그것은 당신이 받아들이는 C 기능를 선언 할 필요가 있음을 알려줍니다 두 개의 매개 변수를 사용하고 void를 콜백 핸들러로 반환하고 AudioServicesAddSystemSoundCompletion에 전달합니다.

// Declare this anywhere in the source file. 
// I would put this before @implement of the class. 
void audioCompletionHandler(SystemSoundID ssID, void *clientData) { 
    NSLog(@"Complete"); 
} 

... 

- (void)playSound { 
    ... 
    // To pass the function pointer, add & before the function name. 
    AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, &audioCompletionHandler, NULL); 
    AudioServicesPlaySystemSound(sound); 
} 
관련 문제