iT邦幫忙

0

Flutter 抓到裝置的FPS只有30(已解決)

  • 分享至 

  • xImage

我想要透過Flutter與ios溝通去抓到裝置的最大FPS,錄影的時候去使用最大的FPS錄影

但我現在抓到的FPS都只有30,但我使用的設備是iphone14 pro max,有支援到60FPS
不知道為什麼抓不到60FPS

以下是Flutter的main.dart

import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(

        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  //相機幀數//
  static const cameraFpsChannel = MethodChannel('samples.flutter.dev/camera_fps');
  String _cameraMaxFps = 'none';

  Future<void> _getCameraFps() async {
    try {
      final double maxFps = await cameraFpsChannel.invokeMethod('getMaxCameraFps');
      setState(() {
        _cameraMaxFps = maxFps.toStringAsFixed(2); // 轉換成字串以顯示
      });
    } on PlatformException catch (e) {
      setState(() {
        _cameraMaxFps = "Failed to get camera max fps: '${e.message}'.";
      });
    }
  }

  Future<void> _startRecording() async {
    final camera = await availableCameras();
    final cameraController = CameraController(camera[0], ResolutionPreset.high);

    try {
      await cameraController.initialize();
      final maxFps = await cameraFpsChannel.invokeMethod('getMaxCameraFps');

      // Do something while recording...

      await cameraController.stopVideoRecording();
    } on PlatformException catch (e) {
      // Handle error
    } finally {
      cameraController.dispose();
    }
  }




  @override
  Widget build(BuildContext context) {
    return Material(
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            ElevatedButton(
              onPressed: _getCameraFps,
              child: const Text('Get Max FPS'),
            ),
            ElevatedButton(
              onPressed: _startRecording,
              child: const Text('Start Recording with Max FPS'),
            ),
            Text(_cameraMaxFps),
          ],
        ),
      ),
    );
  }

}

以下為 ios AppDelegate

import UIKit
import Flutter
import AVFoundation

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    let controller: FlutterViewController = window?.rootViewController as! FlutterViewController
    
    // Create a MethodChannel to communicate with Flutter
    let cameraFpsChannel = FlutterMethodChannel(name: "samples.flutter.dev/camera_fps",
                                                binaryMessenger: controller.binaryMessenger)
    
    // Set the method call handler for the camera fps channel
    cameraFpsChannel.setMethodCallHandler({
      (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
      // This method is invoked on the UI thread.
      if call.method == "getMaxCameraFps" {
        self.getMaxCameraFps(result: result)
      } else {
        result(FlutterMethodNotImplemented)
      }
    })

    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

  // Define a function to get max camera fps
  private func getMaxCameraFps(result: FlutterResult) {
    // Get the current device
    let device = AVCaptureDevice.default(for: AVMediaType.video)
    // Check if the device supports frame rate ranges
    guard let range = device?.activeFormat.videoSupportedFrameRateRanges.first else {
      result(FlutterError(code: "UNAVAILABLE",
                          message: "No supported frame rate ranges available.",
                          details: nil))
      return
    }
    // Iterate through all supported frame rate ranges to find the max value
    var maxFps: Double = 0.0
    for frameRateRange in device!.activeFormat.videoSupportedFrameRateRanges {
      if frameRateRange.maxFrameRate > maxFps {
        maxFps = frameRateRange.maxFrameRate
      }
    }
    result(maxFps)
  }
}

手機App上是顯示的30FPS,不是60FPS
不知道有沒有人知道問題出在哪?

麻煩高手指點了!謝謝!

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答