我无法使用flutter的视频播放器库从服务器播放视频当我尝试播放flutter
视频时,它运行良好,但如果我播放服务器端的url
视频,则会显示错误和黑屏。因此,请帮我缩短这个问题。
代码是
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
class MyVideoPlayerPage extends StatefulWidget {
String myvideo;
MyVideoPlayerPage({this.myvideo});
@override
_MyVideoPlayerPageState createState() => _MyVideoPlayerPageState(myvideo);
}
class _MyVideoPlayerPageState extends State<MyVideoPlayerPage> {
String myvideo;
_MyVideoPlayerPageState(this.myvideo);
VideoPlayerController _controller;
Future<void> _initializeVideoPlayerFuture;
@override
void initState() {
// TODO: implement initState
super.initState();
_controller = VideoPlayerController.network(
//'http://ochapchap.com/public/propertyImage/1594746367.VID-20200713-WA0001.mp4'
// 'http://techslides.com/demos/sample-videos/small.mp4',
'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4'
);
_initializeVideoPlayerFuture = _controller.initialize();
_controller.setLooping(true);
_controller.setVolume(1);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Video"),
),
body: FutureBuilder(
future: _initializeVideoPlayerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
// not wrapped in an AspectRatio widget
return VideoPlayer(_controller);
} else {
return Center(child: CircularProgressIndicator());
}
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
print(myvideo);
setState(() {
if (_controller.value.isPlaying) {
_controller.pause();
} else {
_controller.play();
}
});
},
child: Icon(
_controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
),
),
);
}
}
但是当我把这个flutter dev视频播放得像魅力。链接为https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4
而我的服务器链接没有播放是http://ochapchap.com/public/propertyImage/1594746367.VID-20200713-WA0001.mp4
在我的链接上显示的错误是
E/ExoPlayerImplInternal: Source error
com.google.android.exoplayer2.upstream.HttpDataSource$HttpDataSourceException: Unable to connect
at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.open(DefaultHttpDataSource.java:282)
at com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:83)
at com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:956)
at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:391)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
Caused by: java.io.IOException: Cleartext HTTP traffic to ochapchap.com not permitted
at com.android.okhttp.HttpHandler$CleartextURLFilter.checkURLPermitted(HttpHandler.java:124)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:462)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:131)
at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.makeConnection(DefaultHttpDataSource.java:550)
at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.makeConnection(DefaultHttpDataSource.java:454)
at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.open(DefaultHttpDataSource.java:280)
at com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:83)
at com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:956)
at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:391)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
此地址使用http
,如果此链接支持https
,则可以直接使用https
与http
一起使用
处理Cleartext HTTP traffic to site not permitted
可以编辑androidappsrcmainAndroidManifest.xml
并添加android:usesCleartextTraffic="true"
代码段
<application
android:name="yourName"
android:label="yourLable"
android:usesCleartextTraffic="true"