Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.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
iOS:拍照时启用快门声音_Ios_Audio - Fatal编程技术网

iOS:拍照时启用快门声音

iOS:拍照时启用快门声音,ios,audio,Ios,Audio,我正在使用以下代码: 现在我需要启用拍照时播放的声音 有人能帮我吗?有两种方法: 1) 使用AVAudioPlayer 在您的myPic.m中添加: //at the top #import <AudioToolbox/AudioToolbox.h> #import <MediaPlayer/MediaPlayer.h> @implementation myPic { AVAudioPlayer *mySound; } //then use this f

我正在使用以下代码:

现在我需要启用拍照时播放的声音


有人能帮我吗?

有两种方法:

1) 使用
AVAudioPlayer

在您的
myPic.m
中添加:

//at the top
#import <AudioToolbox/AudioToolbox.h>
#import <MediaPlayer/MediaPlayer.h>

@implementation myPic {

    AVAudioPlayer *mySound;

 }

//then use this function where ever you want to play sound

    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: @"mySound" ofType: @"mp3"]; //wav, aiff, ogg, ext
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
    loopUno = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
    loopUno.numberOfLoops = 0; // here loop setup infinite is -1
    [mySound play];
超控扬声器:

- (void)viewDidLoad {

    [super viewDidLoad];

    AVAudioSession* session = [AVAudioSession sharedInstance];
    BOOL success;
    NSError* error;
    success = [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
    success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
    success = [session setActive:YES error:&error];
}
或者,如果您在应用程序中使用了更多音频,请在应用程序中添加
AppDelegate.m
下的
application did完成启动时使用选项:
并在需要时导入您的代理


希望这对你有帮助;)

将此代码用于快门声音

AudioServicesPlaySystemSound(1108)


注意

要在点击按钮时播放声音吗?如果查看代码:图像是通过编程方式拍摄的。。不需要点击任何按钮:)好的,这不是问题,你想在播放过程中播放声音吗?好的,给我一分钟;)约翰,非常感谢你的回答。但苹果的意思是,默认情况下,代码中的声音被禁用。所以我需要先启用它。。我不知道怎么做。在这里发布您收到的确切反馈尝试使用超控扬声器,我在回答中为您添加了“允许录制第三方的应用程序必须提供合理的方式来指示录制活动。要解决此问题,请确保拍照时快门声音不会自动禁用。”
- (void)viewDidLoad {

    [super viewDidLoad];

    AVAudioSession* session = [AVAudioSession sharedInstance];
    BOOL success;
    NSError* error;
    success = [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
    success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
    success = [session setActive:YES error:&error];
}