2013-03-12 10 views
4

두 가지 다른 버튼 누름 동작이 있습니다. 하나의 버튼을 누르면 하나의 사운드가 재생되고, 다른 버튼을 누르면 첫 번째 사운드는 멈추고 다른 하나는 재생해야합니다. 어떻게해야합니까?AudioToolbox 사운드를 어떻게 멈출 수 있습니까?

당신은 "AudioServicesPlaySystemSound"를 통해 재생중인 사운드를 중지 할 수 없습니다

-(void) onButtonPressAlbina { 
    [soundID2 stop];  
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"albina" ofType:@"m4a"]; 
    SystemSoundID soundID; 
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID); 
    AudioServicesPlaySystemSound (soundID); 
} 

-(void) onButtonPressBalena { 
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"balena" ofType:@"m4a"]; 
    SystemSoundID soundID; 
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID); 
    AudioServicesPlaySystemSound (soundID);  
} 

답변

3

감사합니다,하지만 당신이 할 수있는 대신 "AVAudioPlayer"를 사용합니다.

개체에 "AVAudioPlayer"이라는 참조를 유지하면 중단해야 할 때 "stop"으로 전화 할 수 있습니다.

재생

#import <AudioToolbox/AudioToolbox.h> 
#import <AVFoundation/AVAudioPlayer.h> 
AVAudioPlayer *player; 

// ... 

NSString *path; 
NSError *error; 
path = [[NSBundle mainBundle] pathForResource:@"albina" ofType:@"m4a"]; 
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) 
{  
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error]; 
    player.volume = 0.5f; 
    [player prepareToPlay]; 
    [player setNumberOfLoops:0]; 
    [player play];  
} 

정지

if (player != nil) 
{ 
    if (player.isPlaying == YES) 
    [player stop]; 
} 
+0

덕분에 많이 일했다 – Mihai

관련 문제