chewie_audio 1.4.0 chewie_audio: ^1.4.0 copied to clipboard
An audio player for Flutter with Cupertino and Material play controls
import 'package:chewie_audio/chewie_audio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
void main() {
runApp(
const ChewieAudioDemo(),
);
}
class ChewieAudioDemo extends StatefulWidget {
// ignore: use_key_in_widget_constructors
const ChewieAudioDemo({this.title = 'Chewie Audio Demo'});
final String title;
@override
State<StatefulWidget> createState() {
return _ChewieAudioDemoState();
}
}
class _ChewieAudioDemoState extends State<ChewieAudioDemo> {
TargetPlatform? _platform;
late VideoPlayerController _videoPlayerController1;
late VideoPlayerController _videoPlayerController2;
ChewieAudioController? _chewieAudioController;
@override
void initState() {
super.initState();
initializePlayer();
}
@override
void dispose() {
_videoPlayerController1.dispose();
_videoPlayerController2.dispose();
_chewieAudioController!.dispose();
super.dispose();
}
Future<void> initializePlayer() async {
_videoPlayerController1 = VideoPlayerController.network(
'https://www.w3schools.com/html/mov_bbb.mp4');
_videoPlayerController2 = VideoPlayerController.network(
'https://assets.mixkit.co/videos/preview/mixkit-a-girl-blowing-a-bubble-gum-at-an-amusement-park-1226-large.mp4');
await Future.wait([
_videoPlayerController1.initialize(),
_videoPlayerController2.initialize()
]);
_chewieAudioController = ChewieAudioController(
videoPlayerController: _videoPlayerController1,
autoPlay: true,
looping: true,
// Try playing around with some of these other options:
// showControls: false,
// materialProgressColors: ChewieProgressColors(
// playedColor: Colors.red,
// handleColor: Colors.blue,
// backgroundColor: Colors.grey,
// bufferedColor: Colors.lightGreen,
// ),
// placeholder: Container(
// color: Colors.grey,
// ),
// autoInitialize: true,
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: widget.title,
theme: ThemeData.light().copyWith(
platform: _platform ?? Theme.of(context).platform,
),
home: Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
children: <Widget>[
Expanded(
child: Center(
child: _chewieAudioController != null && _chewieAudioController!.videoPlayerController.value.isInitialized
? ChewieAudio(
controller: _chewieAudioController!,
)
: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
CircularProgressIndicator(),
SizedBox(height: 20),
Text('Loading'),
],
),
),
),
Row(
children: <Widget>[
Expanded(
child: TextButton(
onPressed: () {
setState(() {
if (_chewieAudioController != null) _chewieAudioController!.dispose();
_videoPlayerController1.pause();
_videoPlayerController1.seekTo(const Duration());
_chewieAudioController = ChewieAudioController(
videoPlayerController: _videoPlayerController1,
autoPlay: true,
looping: true,
);
});
},
child: const Padding(
padding: EdgeInsets.symmetric(vertical: 16.0),
child: Text("Audio 1"),
),
),
),
Expanded(
child: TextButton(
onPressed: () {
setState(() {
if (_chewieAudioController != null) _chewieAudioController!.dispose();
_videoPlayerController2.pause();
_videoPlayerController2.seekTo(const Duration());
_chewieAudioController = ChewieAudioController(
videoPlayerController: _videoPlayerController2,
autoPlay: true,
looping: true,
);
});
},
child: const Padding(
padding: EdgeInsets.symmetric(vertical: 16.0),
child: Text("Audio 2"),
),
),
)
],
),
Row(
children: <Widget>[
Expanded(
child: TextButton(
onPressed: () {
setState(() {
_platform = TargetPlatform.android;
});
},
child: const Padding(
padding: EdgeInsets.symmetric(vertical: 16.0),
child: Text("Android controls"),
),
),
),
Expanded(
child: TextButton(
onPressed: () {
setState(() {
_platform = TargetPlatform.iOS;
});
},
child: const Padding(
padding: EdgeInsets.symmetric(vertical: 16.0),
child: Text("iOS controls"),
),
),
)
],
)
],
),
),
);
}
}