Im making a mobile flutter project, this is a opening page of my project, it contains a short videop opening and audio. but the audio won’t playing. only the video. and at my terminal says Error playing audio: PlatformException(WebAudioError, Player has not yet been created or has already been disposed., null, null) here my code
import 'package:flutter/material.dart';
import 'package:abel/screens/home_screen.dart';
import 'package:flutter/services.dart';
import 'package:video_player/video_player.dart';
import 'package:audioplayers/audioplayers.dart';
class SplashScreen extends StatefulWidget {
const SplashScreen({Key? key}) : super(key: key);
@override
_SplashScreenState createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
late VideoPlayerController _videoController;
late AudioPlayer _audioPlayer;
@override
void initState() {
super.initState();
// Initialize the video player
_videoController = VideoPlayerController.asset('assets/images/splashscreen.mp4')
..initialize().then((_) {
setState(() {});
_videoController.play();
});
// Initialize the audio player
_audioPlayer = AudioPlayer();
_playAudio();
// Navigate to HomeScreen after a delay
Future.delayed(const Duration(milliseconds: 5000), () {
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => const HomeScreen()),
);
});
// System UI settings
SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: [
SystemUiOverlay.bottom,
]);
SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
statusBarIconBrightness: Brightness.light,
));
// Lock orientation to landscape
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
}
Future<void> _playAudio() async {
try {
await _audioPlayer.play(AssetSource('assets/audio/splashsound.mp3'));
} catch (error) {
print('Error playing audio: $error');
}
}
@override
void dispose() {
_videoController.dispose();
_audioPlayer.dispose();
// Reset orientation
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
extendBody: true,
extendBodyBehindAppBar: true,
appBar: PreferredSize(
preferredSize: const Size.fromHeight(0),
child: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
),
),
backgroundColor: const Color.fromRGBO(255, 255, 255, 1),
body: LayoutBuilder(
builder: (context, constraints) {
return Stack(
children: [
FractionallySizedBox(
widthFactor: 1.0,
heightFactor: 1.0,
child: AspectRatio(
aspectRatio: 16 / 9,
child: VideoPlayer(_videoController),
),
),
],
);
},
),
);
}
}
New contributor
Omega is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.