ve_vod 1.41.3-lite copy "ve_vod: ^1.41.3-lite" to clipboard
ve_vod: ^1.41.3-lite copied to clipboard

This Flutter plugin provides vod player sdk native APIs for you to implement video play functions in your application

example/lib/main.dart

import 'dart:io';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:ve_vod/ve_vod.dart';
import 'package:vod_player_flutter_example/global_configuration.dart';
import 'package:vod_player_flutter_example/platform_keys.dart';
import 'package:vod_player_flutter_example/short_video_play_scene.dart';
import 'package:vod_player_flutter_example/video_preload.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {
  runApp(const MyApp());
  DartPluginRegistrant.ensureInitialized();
}

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _vodPlayerFlutterPlugin = VodPlayerFlutter();

  SharedPreferences? _sp;

  @override
  void initState() {
    super.initState();
    SharedPreferences.getInstance().then((value) {
      setState(() {
        _sp = value;
      });
    });
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    // 打开日志开关
    await FlutterTTSDKManager.openAllLog();

    // 注册插件日志
    TTFLogger.onLog = (logLevel, msg) {
      print(msg);
    };

    if (Platform.isAndroid) {
      FlutterTTSDKManager.setStaticOption(90, 4);
      FlutterTTSDKManager.setStaticOption(12003, "test");
      FlutterTTSDKManager.setStaticOption(738, 2.2);
      FlutterTTSDKManager.setStaticOption(TTMediaDataLoaderKey.keyIsEnableMDL2,
          (_sp?.getBool(GlobalConfigurationKey.mdlV2EnabledKey) ?? false) ? 1 : 0);
    } else if (Platform.isIOS) {
      FlutterTTSDKManager.setStaticOption(
          TTMediaDataLoaderKey.keyIsEnableMDL2, _sp?.getBool(GlobalConfigurationKey.mdlV2EnabledKey));
    }

    TTVideoEnginePreload.clearAllCaches();

    // final licPath = await rootBundle.load("assets/VEVod.license");
    // final directory = await getApplicationDocumentsDirectory();
    // String licPath = '${directory.path}/VEVod.license';
    String licPath = 'assets/VEVod.license';
    String channel = Platform.isAndroid ? 'xiaomi' : 'App Store';
    TTSDKVodConfiguration vodConfig = TTSDKVodConfiguration();
    vodConfig.cacheMaxSize = 300 * 1024 * 1024;
    TTSDKConfiguration sdkConfig = TTSDKConfiguration.defaultConfigurationWithAppIDAndLicPath(
        appID: '229234', licenseFilePath: licPath, channel: channel);
    // sdkConfig.appRegion = TTSDKServiceVendor.TTSDKServiceVendorCN;
    sdkConfig.vodConfiguration = vodConfig;
    sdkConfig.channel = "ByteVodDemoAndroid";
    sdkConfig.appName = "bytevod_flutter_demo";
    sdkConfig.appVersion = "1.1.1";
    FlutterTTSDKManager.startWithConfiguration(sdkConfig);
    // deviceID
    FlutterTTSDKManager.setCurrentUserUniqueID('test_1234');
    String? uniqueID = await FlutterTTSDKManager.getCurrentUserUniqueID();
    String? deviceID = await FlutterTTSDKManager.getDeviceID();
    print("TTF -- uniqueId:$uniqueID deviceId:$deviceID");

    _enableEngineStrategy(false);

    if (!mounted) return;
    setState(() {});
  }

  void _enableEngineStrategy(bool enable) {
    if (enable) {
      TTVideoEngineStrategy.init();
      TTVideoEngineStrategy.onCreatePreRenderEngine = ((source) async {
        VodPlayerFlutter player = VodPlayerFlutter();
        String coreHashHex = await player.createPlayer(vid: source.getVid, preCreated: true);
        player.setUrlSource(source as TTVideoEngineUrlSource);
        player.setHLSMultiBitrateConfig(
          selectHlsVideoStream: () {
            return 800 * 1024;
          },
          selectHlsRendition: (variantIndex) {
            return 0;
          },
        );
        player.setHardwareDecode(GlobalConfiguration.isHardwareDecode);
        player.setTrackVolumeEnabled(GlobalConfiguration.isTrackVolume);
        player.setScalingMode(TTVideoEngineScalingMode.TTVideoEngineScalingModeAspectFit);
        player.openTextureRender(!GlobalConfiguration.closeTextureRender);
        player.setLooping(true);
        player.setCustomHeader('x-tt-flutter-test-demo', '1');
        print(
            'TTVideoEngineStrategy onCreatePreRenderEngine, hashCode:${player.hashCode} engine:$coreHashHex vid:${source.getVid}');
        return player;
      });
      TTVideoEngineStrategy.enableEngineStrategy(
          strategyType: TTVideoEngineStrategyType.preloadAndPreRender, scene: TTVideoEngineStrategyScene.smallVideo);
      print('TTF -- TTVideoEngineStrategy - init end');
    } else {
      TTVideoEngineStrategy.clearAllEngineStrategy();
      print('TTF -- TTVideoEngineStrategy - clear');
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Builder(
        builder: (context) => Scaffold(
          appBar: AppBar(
            title: const Text('Plugin example app'),
          ),
          body: Center(
            child: Container(
                child: SingleChildScrollView(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  Padding(
                      padding: const EdgeInsets.all(16.0),
                      child: TextField(
                        controller: TextEditingController(
                          text: ShortVideoScene.sTestUrl,
                        ),
                        onSubmitted: _didInputUrl,
                        onChanged: _didInputUrl,
                        decoration: const InputDecoration(
                          labelText: '输入测试地址',
                        ),
                        keyboardType: TextInputType.url,
                        textInputAction: TextInputAction.done,
                      )),
                  Container(
                    padding: const EdgeInsets.all(16.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        const Text('iOS开启HDR'),
                        Switch(
                            value: GlobalConfiguration.isHDRSource,
                            onChanged: (value) {
                              setState(() {
                                GlobalConfiguration.isHDRSource = value;
                              });
                            })
                      ],
                    ),
                  ),
                  Container(
                    padding: const EdgeInsets.all(16.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        const Text('硬解播放'),
                        Switch(
                            value: GlobalConfiguration.isHardwareDecode,
                            onChanged: (value) {
                              setState(() {
                                GlobalConfiguration.isHardwareDecode = value;
                              });
                            })
                      ],
                    ),
                  ),
                  Container(
                    padding: const EdgeInsets.all(16.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        const Text('打开:调节播放音量,关闭:调节系统音量'),
                        Switch(
                            value: GlobalConfiguration.isTrackVolume,
                            onChanged: (value) {
                              setState(() {
                                GlobalConfiguration.isTrackVolume = value;
                              });
                            })
                      ],
                    ),
                  ),
                  Container(
                    padding: const EdgeInsets.all(16.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        const Text('SurfaceView(仅Android)'),
                        Switch(
                            value: GlobalConfiguration.useSurfaceView,
                            onChanged: (value) {
                              setState(() {
                                GlobalConfiguration.useSurfaceView = !GlobalConfiguration.useSurfaceView;
                              });
                            })
                      ],
                    ),
                  ),
                  Container(
                    padding: const EdgeInsets.all(16.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        const Text('关闭TextureRender(仅Android)'),
                        Switch(
                            value: GlobalConfiguration.closeTextureRender,
                            onChanged: (value) {
                              setState(() {
                                GlobalConfiguration.closeTextureRender = !GlobalConfiguration.closeTextureRender;
                              });
                            })
                      ],
                    ),
                  ),
                  Container(
                    padding: const EdgeInsets.all(16.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        const Text('开启预加载+预渲染策略'),
                        Switch(
                            value: _sp?.getBool(GlobalConfigurationKey.engineStrategyEnabledKey) ?? false,
                            onChanged: (value) {
                              setState(() {
                                _enableEngineStrategy(value);
                                _sp?.setBool(GlobalConfigurationKey.engineStrategyEnabledKey, value);
                              });
                            })
                      ],
                    ),
                  ),
                  Container(
                    padding: const EdgeInsets.all(16.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        const Text('MDL 2.0 (重启生效)'),
                        Switch(
                            value: _sp?.getBool(GlobalConfigurationKey.mdlV2EnabledKey) ?? false,
                            onChanged: (value) {
                              setState(() {
                                _sp?.setBool(GlobalConfigurationKey.mdlV2EnabledKey, value);
                              });
                            })
                      ],
                    ),
                  ),
                  Container(
                    padding: const EdgeInsets.all(16.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        const Text('清理缓存'),
                        ElevatedButton(
                            onPressed: () => {TTVideoEnginePreload.clearAllCaches()}, child: const Text("点击清理缓存"))
                      ],
                    ),
                  ),
                  ElevatedButton(
                      onPressed: () {
                        String url = ShortVideoScene.sTestUrl;
                        assert(url.isNotEmpty, '请传入有效的测试地址');
                        Navigator.of(context).push(
                          MaterialPageRoute(
                            builder: (context) => const ShortVideoScene(),
                          ),
                        );
                      },
                      child: const Text("小视频")),
                  ElevatedButton(
                      onPressed: () {
                        Navigator.of(context).push(
                          MaterialPageRoute(
                            builder: (context) => const VideoPreload(),
                          ),
                        );
                      },
                      child: const Text("预加载")),
                ],
              ),
            )),
          ),
        ),
      ),
    );
  }

  void _didInputUrl(String url) {
    if (ShortVideoScene.sTestUrl != url) {
      ShortVideoScene.sTestUrl = url;
    }
  }
}
3
likes
135
pub points
63%
popularity

Publisher

verified publishervolcengine.com

This Flutter plugin provides vod player sdk native APIs for you to implement video play functions in your application

Homepage

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on ve_vod