6

カメラで録音しながら音声ファイルを再生したい。オーディオの再生には AVAudioPlayer を使用し、録音には AVCamCaptureManager を使用しました。

しかし、オーディオの再生が始まると、プレビュー画面がフリーズします。私は何をすべきか?

ご協力ありがとうございます。これがコードです。(私はAVCamの例に取り組んでいます。)

これはプレビュー部分です。

if ([self captureManager] == nil) {
        AVCamCaptureManager *manager = [[AVCamCaptureManager alloc] init];
        [self setCaptureManager:manager];

        [[self captureManager] setDelegate:self];

        if ([[self captureManager] setupSession]) {             
            AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:[[self captureManager] session]];
            UIView *view = [self videoPreviewView];
            CALayer *viewLayer = [view layer];
            [viewLayer setMasksToBounds:YES];

            CGRect bounds = CGRectMake(0, 0, 480, 320);
            [newCaptureVideoPreviewLayer setFrame:bounds];

            if ([newCaptureVideoPreviewLayer isOrientationSupported]) {
                [newCaptureVideoPreviewLayer setOrientation:[DeviceProperties setPreviewOrientation]];
            }

            [newCaptureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

            [viewLayer insertSublayer:newCaptureVideoPreviewLayer below:[[viewLayer sublayers] objectAtIndex:0]];

            [self setCaptureVideoPreviewLayer:newCaptureVideoPreviewLayer];
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                [[[self captureManager] session] startRunning];
            });

            [self updateButtonStates];          
        }

そして、これがオーディオ再生部分です。

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@", sound] ofType:@"wav"]];
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [audioPlayer play];

フリーズせずに通常の AudioServicesPlaySystemSound を使用すると、今回は viewDidLoad でのみ機能します。

CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileRef;
    soundFileRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) CFBridgingRetain(@"sound"), CFSTR ("wav"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
4

2 に答える 2

1

私の問題はビデオではありませんでしたが、通話中にサウンドを再生すること(voipタイプのアプリケーション)であり、この質問への答えは私にとってうまくいきました:

vibrate と AVCaptureSession を同時に使用する

于 2013-11-12T16:53:46.860 に答える
0

AVAudioSessionカテゴリをに設定AVAudioSessionCategoryPlayAndRecordすると問題が解決することを願っています。また、そのカテゴリ オプションを に設定しますAVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers。以下のコードセットを確認してください

@property (strong, nonatomic) AVAudioPlayer *audioPlayer;

NSURL *soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"VoicemailSound" ofType:@"mp3"]]; //Download and add a system sound to your project and mention its name and type here
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers error: nil];

[self.audioPlayer prepareToPlay];
[self.audioPlayer play];
于 2016-11-01T13:59:35.737 に答える