flatter:video\u缩略图包的数据源错误

jjjwad0x  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(418)

我尝试按照视频缩略图插件中的示例,为本地放置在android emulator下载文件夹中的视频生成缩略图:

import 'dart:async';
import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:video_thumbnail/video_thumbnail.dart';

class VideoSelectPageListTile extends StatefulWidget {
  final String videoName;

  const VideoSelectPageListTile({
    Key key,
    @required this.videoName,
  }) : super(key: key);

  @override
  _VideoSelectPageListTileState createState() =>
      _VideoSelectPageListTileState();
}

class _VideoSelectPageListTileState extends State<VideoSelectPageListTile> {
  Future<Image> generateThumbnail(String videoName) async {

    final uint8list = await VideoThumbnail.thumbnailData(
      video: "/storage/emulated/0/Download/my-video-file.mp4",
      imageFormat: ImageFormat.JPEG,
      maxWidth: 128, // specify the width of the thumbnail, let the height auto-scaled to keep the source aspect ratio
      quality: 25,
    );

    return Image.memory(uint8list);
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<Image>(
      future: generateThumbnail(widget.videoName),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        if (snapshot.hasData) {
          return ListTile(
            title: Text(widget.videoName),
            trailing: snapshot.data,
          );
        } else {
          return Text("No image can be generated");
        }
      },
    );
  }
}

无论出于何种原因,它继续在生成时的mediametadataretriever.java类的第77行抛出illegalargumentexceptions:

Launching lib/main.dart on Android SDK built for x86 in debug mode...
Running Gradle task 'assembleDebug'...
✓ Built build/app/outputs/flutter-apk/app-debug.apk.
Installing build/app/outputs/flutter-apk/app.apk...
Waiting for Android SDK built for x86 to report its views...
Debug service listening on ws://127.0.0.1:55082/iYajbqTSHdQ=/ws
Syncing files to device Android SDK built for x86...
D/EGL_emulation(27588): eglMakeCurrent: 0xebc126a0: ver 3 0 (tinfo 0xe2a0f850)
D/eglCodecCommon(27588): setVertexArrayObject: set vao to 0 (0) 1 0
D/ThumbnailPlugin(27588): buildThumbnailData( format:0, maxh:0, maxw:128, timeMs:0, quality:25 )
W/System.err(27588): java.lang.IllegalArgumentException
W/System.err(27588):    at android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:77)
W/System.err(27588):    at xyz.justsoft.video_thumbnail.VideoThumbnailPlugin.createVideoThumbnail(VideoThumbnailPlugin.java:201)
W/System.err(27588):    at xyz.justsoft.video_thumbnail.VideoThumbnailPlugin.buildThumbnailData(VideoThumbnailPlugin.java:116)
W/System.err(27588):    at xyz.justsoft.video_thumbnail.VideoThumbnailPlugin.access$100(VideoThumbnailPlugin.java:37)
W/System.err(27588):    at xyz.justsoft.video_thumbnail.VideoThumbnailPlugin$1.run(VideoThumbnailPlugin.java:77)
W/System.err(27588):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
W/System.err(27588):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
W/System.err(27588):    at java.lang.Thread.run(Thread.java:919)
D/EGL_emulation(27588): eglMakeCurrent: 0xe2a1a2a0: ver 3 0 (tinfo 0xe2a0fad0)

我在这里的实现有什么错误吗?我在package链接上运行了示例程序,没有问题,也没有在androidmanifest.xml文件中添加额外的权限。
你们谁知道怎么解决这个问题吗?

von4xj4u

von4xj4u1#

您可以复制粘贴运行下面的完整代码
你需要许可 READ_EXTERNAL_STORAGE 第一步:添加 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />AndroidManifest.xml 第2步:请求权限 permission_handler ```
Future generateThumbnail(String videoName) async {

Map<Permission, PermissionStatus> statuses = await [
  Permission.storage,
].request();
...
工作演示
![](https://i.stack.imgur.com/t3szV.gif)
完整代码

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:video_thumbnail/video_thumbnail.dart';

class VideoSelectPageListTile extends StatefulWidget {
final String videoName;

const VideoSelectPageListTile({
Key key,
@required this.videoName,
}) : super(key: key);

@override
_VideoSelectPageListTileState createState() =>
_VideoSelectPageListTileState();
}

class _VideoSelectPageListTileState extends State {
Future generateThumbnail(String videoName) async {
Map<Permission, PermissionStatus> statuses = await [
Permission.storage,
].request();

final uint8list = await VideoThumbnail.thumbnailData(
  video: "/storage/emulated/0/Download/my-video-file.mp4",
  imageFormat: ImageFormat.JPEG,
  maxWidth:
      128, // specify the width of the thumbnail, let the height auto-scaled to keep the source aspect ratio
  quality: 25,
);

return Image.memory(uint8list);

}

@override
Widget build(BuildContext context) {
return FutureBuilder(
future: generateThumbnail(widget.videoName),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
return ListTile(
title: Text("${widget.videoName}"),
trailing: snapshot.data,
);
} else {
return Text("No image can be generated");
}
},
);
}
}

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

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
VideoSelectPageListTile(
videoName: "test",
),
],
),
),
);
}
}

相关问题