AVCaptureMovieFileOutput记录视频和音频时,AVPlayerItem持续时间不正确

时间:2019-03-07 13:31:41

标签: c# ios xamarin xamarin.ios avcapturesession

我有一个AVCaptureSession正在运行实时预览。我将视频和音频AVCaptureDeviceInput都设置为记录为单个AVCaptureMovieFileOutput,如下所示:

private void ConfigureSession(AVCaptureSession session) {
    session.BeginConfiguration();

    // setup camera input
    var cameraDevice = AVCaptureDevice.GetDefaultDevice(AVCaptureDeviceType.BuiltInWideAngleCamera, AVMediaType.Video, AVCaptureDevicePosition.Back);
    cameraDevice = cameraDevice ?? AVCaptureDevice.GetDefaultDevice(AVMediaType.Video);

    var captureDeviceInput = AVCaptureDeviceInput.FromDevice(cameraDevice);

    if (session.CanAddInput(captureDeviceInput)) {
        session.AddInput(captureDeviceInput);
    }

    // setup mic input
    var microphoneDevice = AVCaptureDevice.GetDefaultDevice(AVMediaType.Audio);
    var microphoneInput = AVCaptureDeviceInput.FromDevice(microphoneDevice);

    if (session.CanAddInput(microphoneInput)) {
        session.AddInput(microphoneInput);
    }

    // setup single output
    var output = new AVCaptureMovieFileOutput();
    output.MaxRecordedDuration = new CMTime(5 * 600, 600);  // max of 5 seconds
    if (session.CanAddOutput(output)) {
        session.AddOutput(output);
    }

    session.CommitConfiguration();

}

目前,我的会话已经在运行。如您所见,添加了视频和音频输入以及一个输出。

输出还具有5秒的MaxRecordedDuration。录制5秒钟后,output自动停止。然后,我将生成的视频文件加载到AVPlayerItem中,并将其添加到AVPlayer中进行播放:

    private AVPlayer SetupPlayer(string videoFilePath) {
        var asset = AVAsset.FromUrl(NSUrl.FromFilename(videoFilePath));
        var playableKey = "playable";

        var playerItem = new AVPlayerItem(asset, new NSString[] { (NSString)playableKey, (NSString)"hasProtectedContent" });
        playerItem.AddObserver(this, "status", NSKeyValueObservingOptions.OldNew, IntPtr.Zero);

        var player = AVPlayer.FromPlayerItem(playerItem);

        return player;
    }

    public override void ObserveValue(NSString keyPath, NSObject ofObject, NSDictionary change, IntPtr context) {
        if (keyPath == "status") {
            PlayerItemStatusChanged(new NSObservedChange(change));
        } else {
            base.ObserveValue(keyPath, ofObject, change, context);
        }
    }

    private void PlayerItemStatusChanged(NSObservedChange change) {
        AVPlayerItemStatus status = (AVPlayerItemStatus)Convert.ToInt32(change.NewValue.ToString());

        switch (status) {
            case AVPlayerItemStatus.Unknown:
                break;
            case AVPlayerItemStatus.ReadyToPlay:
                Debug.WriteLine($"Ready to Play: Player Item Duration: {Player.CurrentItem.Duration}");
                break;
            case AVPlayerItemStatus.Failed:
                // TODO: show error
                break;
            default:
                break;
        }
    }

更改Status后,将击中我的观察者,并将其显示在控制台上Duration。但是,此处的持续时间总是输出5.6秒左右,而不是电影文件输出的5所定义的MaxRecordedDuration秒。

当我删除麦克风输入时,AVPlayerItem输出的持续时间变为5

为什么录制视频以外的音频会导致AVPlayerItem的时长发生变化?电影文件输出对象输出的总持续时间始终为5,带有或不带有音频。

0 个答案:

没有答案