国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

flutter開發(fā)實戰(zhàn)-video_player視頻播放功能及視頻緩存

這篇具有很好參考價值的文章主要介紹了flutter開發(fā)實戰(zhàn)-video_player視頻播放功能及視頻緩存。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

flutter開發(fā)實戰(zhàn)-video_player視頻播放功能及視頻緩存

最近開發(fā)過程中video_player播放視頻,
flutter開發(fā)實戰(zhàn)-video_player視頻播放功能及視頻緩存,移動開發(fā),flutter開發(fā)實戰(zhàn),flutter,flutter,音視頻,緩存,video_player,cache,AVPlayer,ExoPlayer
flutter開發(fā)實戰(zhàn)-video_player視頻播放功能及視頻緩存,移動開發(fā),flutter開發(fā)實戰(zhàn),flutter,flutter,音視頻,緩存,video_player,cache,AVPlayer,ExoPlayer

一、引入video_player

在pubspec.yaml引入video_player

  video_player: ^2.7.0

在iOS上,video_player使用的是AVPlayer進行播放。
在Android上,video_player使用的是ExoPlayer。

二、使用前設置

2.1 在iOS中的設置

在iOS工程中info.plist添加一下設置,以便支持Https,HTTP的視頻地址

<key>NSAppTransportSecurity</key>
<dict>
	<key>NSAllowsArbitraryLoads</key>
	<true/>
</dict>

2.2 在Android中的設置

需要在/android/app/src/main/AndroidManifest.xml文件中添加網(wǎng)絡權限

<uses-permission android:name="android.permission.INTERNET"/>

三、使用前設置video_player

video_player 使用VideoPlayerController來控制播放與暫停

VideoPlayerController的初始化

_controller = VideoPlayerController.networkUrl(Uri.parse(
        'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4'))
      ..initialize().then((_) {
        // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
        setState(() {});
      });

顯示視頻Widget

Center(
          child: _controller.value.isInitialized
              ? AspectRatio(
                  aspectRatio: _controller.value.aspectRatio,
                  child: VideoPlayer(_controller),
                )
              : Container(),
        ),

控制播放與暫停

// 播放
_controller.play();
// 暫停
_controller.pause();

完整實例代碼

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

void main() => runApp(const VideoApp());

/// Stateful widget to fetch and then display video content.
class VideoApp extends StatefulWidget {
  const VideoApp({super.key});

  
  _VideoAppState createState() => _VideoAppState();
}

class _VideoAppState extends State<VideoApp> {
  late VideoPlayerController _controller;

  
  void initState() {
    super.initState();
    _controller = VideoPlayerController.networkUrl(Uri.parse(
        'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4'))
      ..initialize().then((_) {
        // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
        setState(() {});
      });
  }

  
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Video Demo',
      home: Scaffold(
        body: Center(
          child: _controller.value.isInitialized
              ? AspectRatio(
                  aspectRatio: _controller.value.aspectRatio,
                  child: VideoPlayer(_controller),
                )
              : Container(),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            setState(() {
              _controller.value.isPlaying
                  ? _controller.pause()
                  : _controller.play();
            });
          },
          child: Icon(
            _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
          ),
        ),
      ),
    );
  }

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

注意:video_player暫時不支持緩存,如果需要可以使用flutter_cache_manager

四 緩存flutter_cache_manager下載文件

使用flutter_cache_manager代碼如下

import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'dart:async';
import 'dart:typed_data';
import 'package:file/file.dart';
import 'package:flutter_suprisebox/utils/string_utils.dart';

class CustomCacheManager {
  static const key = 'customCacheKey';
  static CacheManager instance = CacheManager(
    Config(
      key,
      stalePeriod: const Duration(days: 7),
      maxNrOfCacheObjects: 20,
      repo: JsonCacheInfoRepository(databaseName: key),
      fileService: HttpFileService(),
    ),
  );

  Future<File> getSingleFile(
    String url, {
    String? key,
    Map<String, String>? headers,
  }) async {
    return await instance.getSingleFile(url, key: key, headers: headers);
  }

  Future<FileInfo?> getFileFromCache(String url,
      {bool ignoreMemCache = false}) async {
    String? key = StringUtils.toMD5(url);
    if (key != null && key.isNotEmpty) {

      return await instance.getFileFromCache(key, ignoreMemCache: ignoreMemCache);
    }

    return null;
  }

  Future<FileInfo> downloadFile(String url,
      {String? key,
      Map<String, String>? authHeaders,
      bool force = false}) async {
    return await instance.downloadFile(url, key: key, authHeaders: authHeaders, force: force);
  }

  Stream<FileResponse> getFileStream(String url,
      {String? key, Map<String, String>? headers, bool withProgress = false}) {
    return instance.getFileStream(url,
        key: key, headers: headers, withProgress: withProgress);
  }

  Future<void> removeFile(String key) async {
    return instance.removeFile(key);
  }

  /// Removes all files from the cache
  Future<void> emptyCache() {
    return instance.emptyCache();
  }
}

添加flutter_cache_manager后,flutter_cache_manager會先判斷文件是否存在,如果不存在則下載視頻文件。

使用CustomCacheManager后的視頻初始化代碼如下

Future<void> stuVideoPlay() async {
    _controller?.dispose();

    if (Platform.isIOS) {
      _controller = VideoPlayerController.network(widget.videoUrl);
    } else {
      FileInfo? fileInfo =
      await CustomCacheManager().getFileFromCache(widget.videoUrl);
      if (fileInfo == null) {
        fileInfo = await CustomCacheManager().downloadFile(widget.videoUrl);
        // if (fileInfo != null) {
        _controller = VideoPlayerController.file(fileInfo.file);
        // } else {
        //   _controller = VideoPlayerController.network(widget.videoUrl);
        // }
      } else {
        var file = await CustomCacheManager().getSingleFile(widget.videoUrl);
        _controller = VideoPlayerController.file(file);
      }
    }

    await _controller?.initialize().then((_) {
      // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
      setState(() {});
    });

    await _controller!.setLooping(true);
    if (widget.autoPlay) {
      await _controller?.play();
    } else {
      await _controller?.pause();
    }
  }

特別注意:我使用的時候,flutter_cache_manager好像暫時不支持iOS。這點可能還需要其他方案來做緩存處理。如果支持了請留言哦,也可能我記錯了。

四、小結(jié)

flutter開發(fā)實戰(zhàn)-video_player視頻播放功能及視頻緩存。video_player播放視頻,flutter_cache_manager處理視頻緩存。

學習記錄,每天不停進步。文章來源地址http://www.zghlxwxcb.cn/news/detail-628009.html

到了這里,關于flutter開發(fā)實戰(zhàn)-video_player視頻播放功能及視頻緩存的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • Flutter 視頻video_player與緩存flutter_cache_manager

    參考官方DefaultCacheManager代碼,這里引入Config可以指定天數(shù)與最大個數(shù). 文件名 video_cache.dart 使用 await MyDefaultCacheManager().getSingleFile(url)) 即可

    2024年02月07日
    瀏覽(19)
  • Flutter:文件讀取—— video_player、chewie、image_picker、file_picker

    Flutter:文件讀取—— video_player、chewie、image_picker、file_picker

    簡單學習一下幾個比較好用的文件讀取庫 簡介 用于視頻播放 官方文檔 https://pub-web.flutter-io.cn/packages/video_player 安裝 加載網(wǎng)絡視頻 加載本地視頻 設置倍速和進度條 video_player 雖然是官方提供的插件,但是很明顯它只適合拿來簡單的播放視頻,就比如前端的 video 標簽功能也很

    2024年02月13日
    瀏覽(87)
  • Unity視頻播放之Video Player的簡單使用

    Unity視頻播放之Video Player的簡單使用

    使用Unity自帶的VideoPlayer來播放視頻 一、準備視頻 Unity3D常用視頻格式:.mov、.mpg、.mpeg、.mp4、.avi、.asf 如果都不識別,試試轉(zhuǎn)換成ogv格式。轉(zhuǎn)換完成之后,將視頻素材文件拖入Unity Assets 文件夾內(nèi)。 二、創(chuàng)建UI及添加組件 創(chuàng)建RenderTexture資源,設置分辨率,跟視頻分辨率一致

    2024年01月18日
    瀏覽(41)
  • Unity2020Video player發(fā)布后無法播放視頻問題

    我也是小白,用Unity過程中也是遇到很多草蛋問題,我在做demo的時候第一次用到Unity的video player,聽說ogv格式能直接播放,我就把視頻轉(zhuǎn)了ogv格式,然后放到clip里,編輯器里運行完全沒問題,發(fā)布打包后就播放不了,黑屏都沒有,直接視頻沒了,百度搜的絕對路徑說是放到

    2024年02月15日
    瀏覽(26)
  • 前端實現(xiàn)視頻播放功能----vue-video-player --save

    前端實現(xiàn)視頻播放功能----vue-video-player --save

    步驟一: npm安裝插件 : npm install vue-video-player --save 如果報錯 安裝具體版本 如下所示: npm install --save vue-video-player@4.0.6 ?如果package.json文件內(nèi)顯示則為安裝成功 步驟二: 局部引入---這里因為只有一個頁面需要所有采用的局部引入 在需要的頁面引入(這里注意路徑不要寫錯

    2024年02月10日
    瀏覽(28)
  • vue-video-player,springboot實現(xiàn)視頻分段下載播放

    vue-video-player,springboot實現(xiàn)視頻分段下載播放

    事情的起因是在博主把項目部署到服務器上后,發(fā)現(xiàn)由于視頻太大,加上服務器太垃圾,導致稍微大點的視頻加載很久才能播放(指十多分鐘…),然后就上網(wǎng)查找資料,看下咋實現(xiàn)。 這里涉及到有關http請求的知識“HTTP Header里的Range和Content-Range參數(shù),Range是在請求頭里 Ra

    2023年04月13日
    瀏覽(56)
  • 【vue】 vue 實現(xiàn)視頻播放 vue-video-player

    【vue】 vue 實現(xiàn)視頻播放 vue-video-player

    注:用來測試的在線視頻url地址 1.下載依賴 vue2 推薦下載指定版本依賴,不然可能下載不下來報錯。 2.全局注冊main.js 3.新建vue文件 下班`

    2024年02月16日
    瀏覽(25)
  • Unity播放帶Alpha通道的視頻【W(wǎng)ebM+Video Player】(替代播放GIF方案)

    Unity播放帶Alpha通道的視頻【W(wǎng)ebM+Video Player】(替代播放GIF方案)

    在Unity中播放GIF或者動態(tài)效果,可以通過Video Player播放帶透明通道的WebM視頻來實現(xiàn)。 制作帶Alpha的MOV視頻 制作帶Alpha通道的MOV視頻有多重方式,如AE、PR、PS等等,具體制作過程可以自行百度。 將MOV視頻格式轉(zhuǎn)化為WebM格式 將WebM格式導入Unity,設置視頻播放 完成動態(tài)效果展示

    2024年02月13日
    瀏覽(26)
  • vue 視頻播放插件vue-video-player自定義樣式、自動播放設置、設置一開始全屏播放視頻

    vue 視頻播放插件vue-video-player自定義樣式、自動播放設置、設置一開始全屏播放視頻

    1、背景 項目中有涉及視頻播放的需求,并且UI設計了樣式,與原生的視頻video組件有差異,所以使用了vue-video-player插件,并對vue-video-player進行樣式改造,自定義播放暫停按鈕、全屏按鈕、時間進度條樣式等,自動播放設置、設置一開始全屏播放視頻、監(jiān)聽全屏事件等。 2、效

    2024年02月05日
    瀏覽(25)
  • Unity零基礎到進階 ??| 視頻播放器 Video Player組件 詳解

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包