Flutter视频流播放插件flutter_gstreamer_player的使用

发布于 1周前 作者 yuanlaile 来自 Flutter

Flutter视频流播放插件flutter_gstreamer_player的使用

GStreamer Player插件

一个基于GStreamer的替代video_player插件,支持Linux、iOS和Android。

截图

Buy Me A Coffee

安装

Linux

按照GStreamer的Linux安装指南进行操作。

iOS

按照GStreamer的iOS开发安装指南进行操作,步骤包括PrerequisitesDownload and install GStreamer binaries

将GStreamer框架链接到ios目录,以便flutter的ios构建系统可以访问。

ln -s /Users/<Your_User_Name>/Library/Developer/GStreamer/iPhone.sdk/GStreamer.framework ios/GStreamer.framework

Android

按照GStreamer的Android开发安装指南进行操作,步骤包括PrerequisitesDownload and install GStreamer binaries

在Android中,你需要在主活动中初始化GStreamer。在你的flutter应用中,编辑MainActivity.ktMainActivity.java文件(取决于你的flutter配置)。通常该文件位于android/app/src/main/<PATH_DEPENDENT_ON_YOUR_FLUTTER_APPLICATION>/MainActivity.kt

导入GStreamer模块并覆盖onCreate方法以初始化GStreamer,最终结果应如下所示:

import org.freedesktop.gstreamer.GStreamer;

class MainActivity: FlutterActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState);

    GStreamer.init(this);
  }
}

此源代码已在GStreamer 1.20.1版本下测试通过。

开始使用

要开始使用插件,复制以下代码或查看示例项目中的example目录。

import 'package:flutter/material.dart';

import 'package:flutter/services.dart';
import 'package:flutter_gstreamer_player/flutter_gstreamer_player.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: GstPlayer(
          pipeline:
            '''rtspsrc location=
                rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4 !
              rtph264depay !
              h264parse !
              decodebin !
              videoconvert !
              video/x-raw,format=RGBA !
              appsink name=sink''',
        ),
      ),
    );
  }
}

示例代码

以下是示例代码:

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';

import 'package:flutter_gstreamer_player/flutter_gstreamer_player.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  Widget initMultipleGstPlayer(List<String> pipelines) {
    // 尝试这个: https://stackoverflow.com/a/66421214
    List<Widget> videoListWidget = [];
    for (var i = 0; i < pipelines.length - 1; i = i + 2){
      videoListWidget.add(
        Expanded(
          child: Row(
            children: <Widget> [
              Expanded(
                child: GstPlayer(
                  pipeline: pipelines[i],
                ),
              ),
              Expanded(
                child: GstPlayer(
                  pipeline: pipelines[i+1],
                ),
              ),
            ],
          ),
        ),
      );
    }

    if (pipelines.length.isOdd) {
      videoListWidget.add(
        Expanded(
          child: Row(
            children: <Widget> [
              Expanded(
                child: GstPlayer(
                  pipeline: pipelines.last,
                ),
              ),
            ],
          ),
        ),
      );
    }

    Widget multipleGstPlayer = Column(
          children: videoListWidget,
        );

    return multipleGstPlayer;
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    List<String> pipelines = [];

    switch (defaultTargetPlatform) {
      case (TargetPlatform.linux):
      case (TargetPlatform.android):
        pipelines.addAll([
          '''rtspsrc location=
              rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4 !
            decodebin !
            videoconvert !
            video/x-raw,format=RGBA !
            appsink name=sink''',
          '''rtspsrc location=
              rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4 !
            decodebin !
            videoconvert !
            video/x-raw,format=RGBA !
            appsink name=sink''',
          '''rtspsrc location=
              rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4 !
            decodebin !
            videoconvert !
            video/x-raw,format=RGBA !
            appsink name=sink''',
          '''rtspsrc location=
              rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4 !
            decodebin !
            videoconvert !
            video/x-raw,format=RGBA !
            appsink name=sink''',
        ]);
        break;
      case (TargetPlatform.iOS):
        pipelines.addAll([
          '''rtspsrc location=
              rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4 !
            decodebin !
            glimagesink''',
          '''rtspsrc location=
              rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4 !
            decodebin !
            glimagesink''',
          '''rtspsrc location=
              rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4 !
            decodebin !
            glimagesink''',
          '''rtspsrc location=
              rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4 !
            decodebin !
            glimagesink''',
        ]);
        break;
      default:
        break;
    }

    Widget multipleGstPlayer = initMultipleGstPlayer(pipelines);

    return MaterialApp(
      home: Scaffold(
        body: multipleGstPlayer,
      ),
    );
  }
}

更多关于Flutter视频流播放插件flutter_gstreamer_player的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter视频流播放插件flutter_gstreamer_player的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中集成和使用flutter_gstreamer_player插件来播放视频流的示例代码。请注意,这个插件依赖于GStreamer,因此在开始之前,请确保您的开发环境中已经正确安装了GStreamer,并且您的Flutter项目已经设置好了原生插件的支持。

1. 添加依赖

首先,在您的pubspec.yaml文件中添加flutter_gstreamer_player依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_gstreamer_player: ^最新版本号  # 请替换为实际发布的最新版本号

然后运行flutter pub get来安装依赖。

2. 配置原生项目

由于flutter_gstreamer_player是一个包含原生代码的插件,您可能需要在iOS和Android项目中进行一些配置。通常,这些配置在插件的README文件中会有详细说明。这里假设您已经按照README完成了必要的配置。

3. 使用插件

接下来,在您的Dart代码中导入插件并使用它来播放视频流。以下是一个简单的示例,展示如何在Flutter应用中播放视频流:

import 'package:flutter/material.dart';
import 'package:flutter_gstreamer_player/flutter_gstreamer_player.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: VideoPlayerScreen(),
    );
  }
}

class VideoPlayerScreen extends StatefulWidget {
  @override
  _VideoPlayerScreenState createState() => _VideoPlayerScreenState();
}

class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
  late GStreamerPlayerController _controller;

  @override
  void initState() {
    super.initState();
    // 初始化GStreamerPlayerController
    _controller = GStreamerPlayerController(
      uri: '您的视频流URI',  // 替换为您的视频流URI
      autoPlay: true,
    );

    // 监听播放器的状态变化
    _controller.addListener(() {
      if (_controller.value.isPlaying) {
        print('视频正在播放');
      } else if (_controller.value.isPaused) {
        print('视频已暂停');
      } else if (_controller.value.isEnded) {
        print('视频播放结束');
      }
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter GStreamer Player 示例'),
      ),
      body: Center(
        child: GStreamerPlayer(
          controller: _controller,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          // 切换播放/暂停
          if (_controller.value.isPlaying) {
            _controller.pause();
          } else {
            _controller.play();
          }
        },
        tooltip: '播放/暂停',
        child: Icon(
          _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
        ),
      ),
    );
  }
}

注意事项

  1. GStreamer安装:确保您的开发环境中已经安装了GStreamer,并且版本与flutter_gstreamer_player插件兼容。
  2. 权限:如果您的视频流来自网络,确保在Android和iOS项目中配置了必要的网络权限。
  3. URI格式:确保您提供的视频流URI是正确的,并且可以被GStreamer解析和播放。

通过上述步骤,您应该能够在Flutter应用中成功集成并使用flutter_gstreamer_player插件来播放视频流。如果遇到任何问题,请参考插件的官方文档或在其GitHub仓库中查找相关信息。

回到顶部