【问题标题】:avplayer not playing audio in video which is captured through device camera in IOS objective-Cavplayer 不播放通过 IOS Objective-C 中的设备摄像头捕获的视频中的音频
【发布时间】:2016-05-17 09:47:39
【问题描述】:

我正在从设备相机捕获视频,然后将该视频存储到设备本地内存中,然后在下一个屏幕中,我正在使用 AVPlayer 播放该视频,但我没有从视频中获得声音.这是视频播放的代码

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive:YES error:nil];

    _avPlayer=[[AVPlayer alloc]initWithURL:url]; //URL of my recorded video
    _avPlayerLayer =[AVPlayerLayer playerLayerWithPlayer:_avPlayer];
    [_avPlayerLayer setFrame:CGRectMake(0, 0, self.videoview.frame.size.width, self.videoview.frame.size.height)];
    [_avPlayerLayer setBackgroundColor:[[UIColor blackColor]CGColor]];
    _avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.pictureview.layer addSublayer:_avPlayerLayer];
    [_avPlayer seekToTime:kCMTimeZero];
    _avPlayer = [self rotateVideoPlayer:_avPlayer withDegrees:0.0];
    _avPlayer.volume = 1.0;
    [_avPlayer play];

注意:-视频有声音,因为预览后,我将视频上传到服务器上,这样我可以听到录制视频的声音。

我正在使用 AVCaptureSession 录制视频,并已添加 AVMediaTypeVideoAVMediaTypeAudio 作为 AVCaptureDevice。 所以在播放的时候会不会有什么问题。在播放之前,我要从 AVCaptureSession 中删除 AVCaptureDevice 设备,然后播放捕获的视频。

【问题讨论】:

  • 所以问题是视频没有被捕获,或者没有播放?
  • 问题是在播放中没有在视频中播放音频。

标签: ios objective-c audio avfoundation avplayer


【解决方案1】:

我前段时间遇到了同样的问题,将此代码添加到 AppDelegate 方法对我有帮助:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
        NSError *err;
        AVAudioSession *session = [AVAudioSession sharedInstance];
        bool success = [session setCategory:AVAudioSessionCategoryPlayback
                                      error:&err];
        if (!success) {
            DLog(@"error changing audio mode %@", err);
            [session setCategory:AVAudioSessionCategoryPlayback
                           error:&err];
            if (err) {
                DLog(@"error changing audio mode %@", err);
            }
        }
}

另外,如果没有帮助,请尝试关闭设备上的“静音”按钮。 如果确实存在,还要检查错误描述。

更新: 您是否像这样初始化音频输入?

        AVCaptureDevice *audioDevice = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio] firstObject];
        AVCaptureDeviceInput *audioDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&error];

        if (error)
        {
            NSLog(@"%@", error);
        }

        if ([session canAddInput:audioDeviceInput])
        {
            [session addInput:audioDeviceInput];
        }

【讨论】:

  • 感谢您的回答。但它只工作了 2 次之后,我遇到了同样的没有声音的问题。
  • 尝试将视频存储到相机胶卷,然后尝试从原生ios应用播放,声音会存在吗?
  • 存在声音,但是当我尝试在我的应用中播放预览时,它没有播放声音。
  • 我正在使用 AVCaptureSession 录制视频,并添加了 AVMediaTypeVideo 和 AVMediaTypeAudio 作为 AVCaptureDevice。所以它会在播放时产生任何问题。在播放之前,我从 AVCaptureSession 中删除 AVCaptureDevice 设备,然后播放捕获的视频。
  • AVCaptureMovieFileOutput 怎么样?你如何初始化它?
【解决方案2】:

您可以使用MPMoviePlayerController 轻松播放视频。在我正在玩的一个项目中,

self.videoController = [[MPMoviePlayerController alloc] init];

[self.videoController setContentURL:videourl];
[self.videoController.view setFrame:CGRectMake (0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:self.videoController.view];

UIButton *closeButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width-60, 20, 60, 30)];

[closeButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];

[closeButton setTitle:@"Cancel" forState:UIControlStateNormal];

[self.videoController.view addSubview:closeButton];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(videoPlayBackDidFinish:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:self.videoController];

[self.videoController play];

我正在添加关闭按钮和观察者,如果不需要,您不需要这样做。

别忘了导入<MediaPlayer/MediaPlayer.h>

更新:

 audioSession = [AVAudioSession sharedInstance];


    NSError *err;
    NSError *error;


    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &err];

    if (err) {
        NSLog(@"ERROR occured %@",[err localizedDescription]);
    }

    [audioSession setActive:YES error: &error];

    [audioSession setActive:NO error:nil];
    // [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
    [audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker
                                    error:nil];

    [audioSession setActive:YES error:nil];



    audioPlayer = nil;
    //NSError *error;


    audioPlayer = [[AVAudioPlayer alloc]
                   initWithContentsOfURL:self.audioRecorder1.url
                   error:&error];
    // audioPlayer.volume = 1.0;
    audioPlayer.delegate = self;
    [audioPlayer prepareToPlay];
      [audioPlayer play];

您使用 avplayer 而不是音频播放器!主要关注的是会话!

希望这会有所帮助:)

【讨论】:

  • 我也使用了你的代码,但仍然没有得到录制视频的声音
  • 你如何录制视频?
  • 我正在使用 AVCaptureSession 录制视频,并添加了 AVMediaTypeVideo 和 AVMediaTypeAudio 作为 AVCaptureDevice。所以它会在播放时产生任何问题。在播放之前,我从 AVCaptureSession 中删除 AVCaptureDevice 设备,然后播放捕获的视频。
  • 我认为您在录制时没有收到音频!为什么不使用imagepickercontroller 录制视频?
  • 我正在录制音频,因为预览后我将该视频上传到我们的服务器。当我在服务器上播放该视频时,它有一个音频。
猜你喜欢
  • 1970-01-01
  • 2023-03-06
  • 2012-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多