2016-05-17 31 views
0

我从设备摄像头捕获视频并将该视频存储在设备本地内存中,而不是在下一个屏幕中我正在播放该视频使用AVPlayer但我没有从video.Here获得声音的视频播放代码avplayer没有播放通过iOS设备摄像头捕获的视频中的音频Objective-C

[[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设备,并播放捕获的视频。

+0

所以问题是视频没有被捕获,或没有播放? – Lefteris

+0

问题是播放过程中没有播放视频中的音频。 –

回答

0

我有同样的问题,一时间以前,将此代码添加到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]; 
     } 
+0

谢谢你的回答。但是,它只能工作2次后,我得到了没有声音的相同问题。 –

+0

尝试将视频存储到相机胶卷,然后尝试从本机ios应用播放,声音将存在? –

+0

在声音中存在,但是当我试图在我的应用程序中播放预览而不是播放声音时。 –

0

您可以使用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,而不是!主要关心的是会议!

希望这会有所帮助:)

+0

我也使用过您的代码,但仍然没有录制视频的声音 –

+0

您如何录制视频? – Lion

+0

我正在使用AVCaptureSession录制视频并添加AVMediaTypeVideo和AVMediaTypeAudio作为AVCaptureDevice。所以它会在播放时出现问题。在播放之前,我正在从AVCaptureSession中移除AVCaptureDevice设备,并播放捕获的视频。 –