Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何停止音频工具箱声音?_Objective C_Xcode_Avfoundation_Audiotoolbox - Fatal编程技术网

Objective c 如何停止音频工具箱声音?

Objective c 如何停止音频工具箱声音?,objective-c,xcode,avfoundation,audiotoolbox,Objective C,Xcode,Avfoundation,Audiotoolbox,我有两种不同的按键声音动作。当我按下一个按钮时,它应该播放一个声音,当我按下另一个按钮时,它应该停止第一个声音并播放另一个声音,我如何才能做到这一点 谢谢 -(void) onButtonPressAlbina { [soundID2 stop]; NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"albina" ofType:@"m4a"]; SystemSoundID soundID

我有两种不同的按键声音动作。当我按下一个按钮时,它应该播放一个声音,当我按下另一个按钮时,它应该停止第一个声音并播放另一个声音,我如何才能做到这一点

谢谢

-(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);    
}

您无法停止通过“
AudioServicesPlaySystemSound
”播放的声音,但您可以改为使用“
AVAudioPlayer

在对象中保留对“
AVAudioPlayer
”的引用,然后在需要停止时调用它

#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];
}