I'm trying to build an app that can play audio files and wanted to import the just_audio audio player since its far superior to the native one and better set to my needs.
I'm using chatgpt to help b/c my coding can be alot better.
The following code is as such. its just a basic audio display. In addition I've named the widget JustAudioPlayerWidget and uploaded the dependency package just_audio:^0.9.13:
// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/widgets/index.dart'; // Imports other custom widgets
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom widget code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
// Set your widget name, define your parameter, and then add the
// boilerplate code using the green button on the right!
import 'package:just_audio/just_audio.dart';
class JustAudioPlayerWidget extends StatefulWidget {
final String audioUrl;
final double width;
final double height;
const JustAudioPlayerWidget({
Key? key,
required this.audioUrl,
this.width = 200.0,
this.height = 50.0,
}) : super(key: key);
@override
_JustAudioPlayerWidgetState createState() => _JustAudioPlayerWidgetState();
}
class _JustAudioPlayerWidgetState extends State<JustAudioPlayerWidget> {
late AudioPlayer _audioPlayer;
bool _isPlaying = false;
@override
void initState() {
super.initState();
_audioPlayer = AudioPlayer();
_initAudio();
}
Future<void> _initAudio() async {
try {
await _audioPlayer.setUrl(widget.audioUrl);
} catch (e) {
print('Error initializing audio: $e');
}
}
@override
void dispose() {
_audioPlayer.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
width: widget.width,
height: widget.height,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: Icon(_isPlaying ? Icons.pause : Icons.play_arrow),
onPressed: () {
if (_isPlaying) {
_audioPlayer.pause();
} else {
_audioPlayer.play();
}
setState(() {
_isPlaying = !_isPlaying;
});
},
),
],
),
);
}
}