my pubspec
dependencies:
flutter:
sdk: flutter
audioplayers: ^5.2.1
cupertino_icons: ^1.0.6
sourc code
``` import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';
void main() {runApp(AudioPlayerApp());}
class AudioPlayerApp extends StatelessWidget {@overrideWidget build(BuildContext context) {return MaterialApp(home: AudioPlayerWidget(),);}}
class AudioPlayerWidget extends StatefulWidget {@override_AudioPlayerWidgetState createState() => _AudioPlayerWidgetState();}
class _AudioPlayerWidgetState extends State<AudioPlayerWidget> {late AudioPlayer _audioPlayer;late AudioSource _audioSource;
@overridevoid initState() {super.initState();_audioPlayer = AudioPlayer();_audioSource = UrlSource('http://myradio.com/test');}
@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text('Audio Player'),),body: Center(child: IconButton(icon: Icon(Icons.play_circle_filled),onPressed: () async {await _audioPlayer.play(_audioSource);},),),);}
@overridevoid dispose() {_audioPlayer.dispose();super.dispose();}} ```
I keep getting these logs lib/main.dart:24:8: Error: Type 'AudioSource' not found. late AudioSource _audioSource; ^^^^^^^^^^^ lib/main.dart:24:8: Error: 'AudioSource' isn't a type. late AudioSource _audioSource; ^^^^^^^^^^^ Target kernel_snapshot failed: Exception
Change late AudioSource _audioSource;
to late Source _audioSource;
Because Source is the parent class of UrlSource.
void main() {
runApp(AudioPlayerApp());
}
class AudioPlayerApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: AudioPlayerWidget());
}
}
class AudioPlayerWidget extends StatefulWidget {
@override
_AudioPlayerWidgetState createState() => _AudioPlayerWidgetState();
}
class _AudioPlayerWidgetState extends State<AudioPlayerWidget> {
late AudioPlayer _audioPlayer;
late Source _audioSource;
@override
void initState() {
super.initState();
_audioPlayer = AudioPlayer();
_audioSource = UrlSource('http://myradio.com/test');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Audio Player'),
),
body: Center(
child: IconButton(
icon: Icon(Icons.play_circle_filled),
onPressed: () async {
await _audioPlayer.play(_audioSource);
},
),
),
);
}
@override
void dispose() {
_audioPlayer.dispose();
super.dispose();
}
}