I am new to flutter and the just_audio package, and am simply trying to reproduce the code on their website.
My Goal - I would like to use this simple AudioPlayer() constructor to get some audio playing on my app with a few lines of code.
Minimal Code Example - After adding the following code to the flutter create standard app...
in main.dart
import 'package:just_audio/just_audio.dart';
final player = AudioPlayer();
final duration = await player.setUrl(
'https://example.com/bar.mp3');
player.play();
and in pubspec.yaml
just_audio: ^0.9.28
I get the following error
Error: Can't use 'player' because it is declared more than once.
The app runs when I take these lines out, but I'm not sure why they don't work because they are copy-pasted from the docs.
I searched their github issues and didn't find anything about this, and also didn't find anything already on Stack Overflow.
To Reproduce
- Terminal: flutter create audio_app
- Terminal: flutter pub add just_audio
- Copy paste code from their website into main.dart -- my code above.
My Environment
- Mac OS Big Sur
- Flutter 3.0.5 • channel stable • https://github.com/flutter/flutter.git
- Framework • revision f1875d570e (6 weeks ago) • 2022-07-13 11:24:16 -0700
- Engine • revision e85ea0e79c
- Tools • Dart 2.17.6 • DevTools 2.12.2
- just_audio: ^0.9.28
- Testing simulation on Simulator v13.2
Any help understanding what is going on and how to fix it is appreciated. Let me know if you need me to attach more code.
Full main.dart file as requested
import 'package:flutter/material.dart';
import 'package:just_audio/just_audio.dart';
void main() {
runApp(const MyApp());
}
final player = AudioPlayer(); // Create a player
final duration = await player.setUrl( // Load a URL
'http://music.thomasmeli.com/wp-content/uploads/2022/05/Thomas-Meli-Binary-Star-Sample.mp3'); // Schemes: (https: | file: | asset: )
player.play();
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}