That is my play yotube screen . Please help me solve the problem. When I press the video button to go to this screen, the application closes without any intervention .
For your information, everything works normally while running on the emulator in debug mode the problem just in release mode .
Code
late YoutubePlayerController _controller;
late String url = widget.url;
late YoutubePlayer youtubePlayer;
late String id;
late bool isPlaying, isMute;
late String author;
@override
void initState() {
super.initState();
isMute = false;
id = YoutubePlayer.convertUrlToId(url)!;
_controller = YoutubePlayerController(
initialVideoId: id,
flags: const YoutubePlayerFlags(autoPlay: false),
);
youtubePlayer = YoutubePlayer(
controller: _controller,
);
isPlaying = _controller.value.isPlaying;
author = _controller.metadata.author;
}
Widget setAuthor() {
// ignore: unnecessary_null_comparison
if (_controller.metadata.author != null) {
setState(() {
author = _controller.metadata.author;
});
return Text(
author,
style: const TextStyle(
color: Colors.pink,
fontSize: 25,
fontWeight: FontWeight.bold,
),
);
} else {
return setAuthor();
}
}
SafeArea(
child: Scaffold(
resizeToAvoidBottomInset: true,
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Stack(
children: [
_backAction(),
Positioned(
top: 130,
left: 3,
right: 3,
child: SizedBox(
height: MediaQuery.of(context).size.height * 2 / 3.0,
child: _Player(),
),
),
],
),
),
),
),
);
Widget _Player() {
return YoutubePlayerBuilder(
player: YoutubePlayer(
controller: _controller,
progressIndicatorColor: dustyRose,
),
builder: (BuildContext context, Widget player) {
return Scaffold(
body: Column(
children: [
const SizedBox(
height: 20,
),
player,
const SizedBox(
height: 10,
),
Text(
widget.title,
style: const TextStyle(
color: textColorBlue,
fontWeight: FontWeight.bold,
fontSize: 30),
),
const SizedBox(
height: 15,
),
Text(
widget.artist,
style: const TextStyle(color: textColorBlack, fontSize: 20),
),
const SizedBox(
height: 10,
),
Text(
widget.duration,
style: const TextStyle(color: textColorBlack),
),
const SizedBox(
height: 10,
),
// ignore: unnecessary_null_comparison
author == null
? setAuthor()
: Text(
_controller.metadata.author,
style: const TextStyle(
color: Colors.pink,
fontWeight: FontWeight.bold,
fontSize: 25),
),
const SizedBox(
height: 15,
),
buttonRowBuilder(),
],
),
);
},
);
}
buttonRowBuilder() {
double size = 35;
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
onPressed: () {
if (_controller.value.position <= const Duration(seconds: 30)) {
_controller.seekTo(const Duration(seconds: 0));
} else {
var p = _controller.value.position - const Duration(seconds: 30);
_controller.seekTo(p);
}
},
icon: const Icon(Icons.replay_30, color: textColorBlue),
iconSize: size,
),
IconButton(
onPressed: () {
if (_controller.value.isPlaying) {
_controller.pause();
} else {
_controller.play();
}
setState(() {
isPlaying = !isPlaying;
});
},
icon: isPlaying
? const Icon(Icons.pause, color: textColorBlue)
: const Icon(Icons.play_arrow, color: textColorBlue),
iconSize: size,
),
IconButton(
onPressed: () {
var newPostion =
_controller.value.position + const Duration(seconds: 30);
_controller.seekTo(newPostion);
},
icon: const Icon(Icons.forward_30, color: textColorBlue),
iconSize: size,
),
IconButton(
onPressed: () {
if (isMute) {
_controller.unMute();
} else {
_controller.mute();
}
setState(() {
isMute = !isMute;
});
},
icon: isMute
? const Icon(Icons.volume_off, color: textColorBlue)
: const Icon(Icons.volume_up, color: textColorBlue),
iconSize: size,
),
],
);
}
builde.gradle
defaultConfig {
applicationId "com.youtube_player"
minSdkVersion 21
targetSdkVersion 34
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
multiDexEnabled true
}
buildTypes {
release {
// signingConfig signingConfigs.debug
signingConfig signingConfigs.release
minifyEnabled false
shrinkResources false
}
}
I add these permissions to my AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
I am using flutter version 3.0.1
2
Answers
Thank you for your answer i solve the problem by add this line in AndroidManifest.xml
and i run test on release mode by Real device and everything works normally
The issue might be related to the ProGuard rules configuration in your Android project when building in release mode.
When you build your Flutter app in release mode, ProGuard is enabled by default to optimize and obfuscate the code. Sometimes, ProGuard may obfuscate classes or methods that are used by external libraries, causing runtime issues such as crashing or unexpected behavior.
To fix this issue, you need to add specific ProGuard rules to ensure that the classes and methods used by the
flutter_youtube_player
plugin are not obfuscated by ProGuard.Open the android/app/proguard-rules.pro file in your Flutter project
Add the following ProGuard rules to keep the classes and methods used by the flutter_youtube_player plugin:
#Keep the classes and methods used by flutter_youtube_player
-keep class com.pierfrancescosoffritti.androidyoutubeplayer.** { ; }
-keep interface com.pierfrancescosoffritti.androidyoutubeplayer.* { *; }