如何将视频下载到应用程序,并使其只能通过应用程序访问(类似于Youtube/Netflix)



我目前正在搜索一个将视频下载到应用程序的功能。下载的视频只能像Youtube和Netflix一样通过应用程序访问,并且将在图库中隐藏/加密。如果有人能为我构建这个功能指明正确的方向,我将不胜感激。

使用Path提供程序包。

您可以使用适用于iOS的getApplicationSupportDirectory()和适用于Android的getApplicationDocumentsDirectory()

像这样:

var dir = await (Platform.isIOS
? getApplicationSupportDirectory()
: getApplicationDocumentsDirectory());
File videoFile = File("${dir.path}/video.mp4");

我们已经将vdocipher_flutter用于此类功能。此库将有助于播放仅适用于应用程序的下载视频。

要下载视频,您需要集成VdoCipher的Android库,您可以通过Flutter中的方法通道使用它

以下是示例代码:

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:vdocipher_flutter/vdocipher_flutter.dart';
import 'package:vdocipher_flutter_example/vdoplayback_view.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'VdoCipher Sample Application',
home: MyHome(),
theme: ThemeData(
primaryColor: Colors.white,
textTheme: TextTheme(bodyText1: TextStyle(fontSize: 12.0))),
);
}
}
class MyHome extends StatefulWidget {
@override
_MyHomeState createState() => _MyHomeState();
}
class _MyHomeState extends State<MyHome> {
String? _nativeAndroidLibraryVersion = 'Unknown';
@override
void initState() {
super.initState();
getNativeAndroidLibraryVersion();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> getNativeAndroidLibraryVersion() async {
String? version;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
version = await VdocipherMethodChannel.nativeAndroidLibraryVersion;
} on PlatformException {
version = 'Failed to get native android library version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_nativeAndroidLibraryVersion = version;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('VdoCipher Sample Application'),
),
body: Center(child: Column(
children: <Widget>[
Expanded(child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: _goToVideoPlayback,
child: const Text('Online Playback',
style: TextStyle(fontSize: 20)),
),
ElevatedButton(
onPressed: null,
child: const Text('Todo: video selection',
style: TextStyle(fontSize: 20)),
)
])),
Padding(padding: EdgeInsets.all(16.0),
child: Text('Native android library version: $_nativeAndroidLibraryVersion',
style: TextStyle(color: Colors.grey, fontSize: 16.0)))
],
)))
);
}
void _goToVideoPlayback() {
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (BuildContext context) {
return VdoPlaybackView();
},
),
);
}
}

注意:它是付费的,仅适用于Android。

相关内容

最新更新