Skip to content

[Camera] Audio is only recorded with first recording on iOS #30689

@hillelcoren

Description

@hillelcoren

If the camera plugin is used to capture more than one video only the first video will include audio. The problem can be worked around by calling camera.initialize(); again however it causes the camera preview to flash and is obviously inefficient.

Here's a sample app demonstrating the problem. Click record, then stop then play and the audio/video will both work. Click record, stop and then play again and there will only be video.

import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:video_player/video_player.dart';
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';

List<CameraDescription> cameras;
Future<void> main() async {
  cameras = await availableCameras();
  runApp(TestApp());
}

class TestApp extends StatefulWidget {
  @override
  TestAppState createState() => TestAppState();
}

class TestAppState extends State<TestApp> {
  VideoPlayerController videoPlayer;
  CameraController camera;
  String path;

  @override
  void initState() {
    super.initState();
    camera = CameraController(
        cameras.firstWhere(
            (camera) => camera.lensDirection == CameraLensDirection.front),
        ResolutionPreset.high)
      ..addListener(() {
        if (mounted) setState(() {});
      })
      ..initialize();
  }

  void record() async {
    final Directory directory = await getApplicationDocumentsDirectory();
    final String folder = '${directory.path}/videos';
    await Directory(folder).create(recursive: true);
    path = '$folder/${DateTime.now().millisecondsSinceEpoch}.mp4';
    await camera.startVideoRecording(path);
  }

  void stopRecording([bool cancel]) async {
    await camera.stopVideoRecording();
    final player = VideoPlayerController.file(File(path));
    await player.initialize();
    setState(() {
      videoPlayer = player;
    });

    // Uncommenting this line 'fixes' the problem
    // await camera.initialize();
  }

  void play() {
    if (!videoPlayer.value.initialized) return;
    videoPlayer
      ..seekTo(Duration())
      ..play();
  }

  void clear() {
    setState(() {
      videoPlayer = null;
    });
  }

  @override
  Widget build(BuildContext context) {
    final value = camera.value;
    if (!value.isInitialized) return SizedBox();
    return MaterialApp(
      home: SafeArea(
        child: Scaffold(
            body: Column(children: [
          Expanded(
              child: AspectRatio(
                  aspectRatio: value.aspectRatio,
                  child: CameraPreview(camera))),
          Material(
            color: Colors.black26,
            elevation: 8,
            child: Row(children: [
              FlatButton(
                child: Text('Record'),
                onPressed: record,
              ),
              FlatButton(
                child: Text('Stop'),
                onPressed: stopRecording,
              ),
              FlatButton(
                child: Text('Play'),
                onPressed: play,
              ),
              FlatButton(
                child: Text('Clear'),
                onPressed: clear,
              ),
            ]),
          ),
          Expanded(
            child: videoPlayer == null
                ? SizedBox()
                : AspectRatio(
                    aspectRatio: videoPlayer.value.aspectRatio,
                    child: VideoPlayer(videoPlayer)),
          )
        ])),
      ),
    );
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: videoVideo playbackfound in release: 1.22Found to occur in 1.22has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: cameraThe camera pluginpackageflutter/packages repository. See also p: labels.platform-iosiOS applications specificallyr: fixedIssue is closed as already fixed in a newer version

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions