I am building an music player application that takes the URL from a php local host server and play it The package I am using is audio_player and it can play audio files either from assets in application or using a URL when I tried to play assets audio file it worked perfectly but a URL doesn't work
import 'dart:ui';
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:tahween/api/linkAPI.dart';
class MusicPlayer extends StatefulWidget{
const MusicPlayer({super.key});
@override
State<StatefulWidget> createState() =>MusicPlayerState();
}
class MusicPlayerState extends State<MusicPlayer> {
final audioPlayer = AudioPlayer();
Duration duration = Duration.zero;
Duration position = Duration.zero;
bool isPlaying = false;
IconData playIcon = Icons.play_arrow;
@override
void initState() {
super.initState();
audioPlayer.onPlayerStateChanged.listen((state) {
setState(() {
isPlaying = state ==PlayerState.playing;
playIcon = playIcon==Icons.play_arrow?Icons.pause:Icons.play_arrow;
});
});
audioPlayer.onDurationChanged.listen((newDuration) {
setState(() {
duration=newDuration;
});
});
audioPlayer.onPositionChanged.listen((newPosition) {
setState(() {
position=newPosition;
});
});
print("ready");
}
@override
Widget build(BuildContext context) {
double screenHeight = MediaQuery.of(context).size.height;
double screenWidth = MediaQuery.of(context).size.width;
return Scaffold(
body: Stack(
children: [
SizedBox(
height: screenHeight,
width: screenWidth,
child: Image.asset(
"lib/images/music player.jpg", // Original clear image
fit: BoxFit.fill,
),
),
Positioned.fill(
child: BackdropFilter(
filter: ImageFilter.blur(
sigmaX: 10.0,
sigmaY: 10.0), // Adjust blur intensity as needed
child: Container(
color:
Colors.black.withOpacity(0.5), // Adjust opacity as needed
),
),
),
Positioned(
top: screenHeight / 10,
left: screenWidth / 10,
right: screenWidth / 10,
child: Column(
children: [
SizedBox(
height: screenHeight / 2,
child: ClipRRect(
borderRadius: BorderRadius.circular(
30.0),
child: Image.asset("lib/images/music player.jpg",
fit: BoxFit.fill),
),
),
SizedBox(
height: screenHeight / 20,
),
SliderTheme(
data: SliderTheme.of(context).copyWith(
activeTrackColor: Colors.white,
thumbColor: Colors.white,
inactiveTrackColor: Colors.black
),
child: Slider(
value: position.inSeconds.toDouble(),
min: 0,
max: duration.inSeconds.toDouble(),
onChanged: (value) async{
final position = Duration(seconds: value.toInt());
await audioPlayer.seek(position);
await audioPlayer.resume();
},),
),
SizedBox(
height: screenHeight / 20,
),
Row(
//control buttons
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
onPressed: () {},
icon: Icon(CupertinoIcons.shuffle,color: Colors.grey[500],)),
ElevatedButton(
onPressed: () {},
child: const Icon(
CupertinoIcons.backward_end_alt_fill,
color: Colors.black,
)),
InkWell(
onTap: (){
if(isPlaying){
print("pause");
audioPlayer.pause();
} else{
// https://youtu.be/TvUvq2SnTtA
Source src= UrlSource(musicURL);
audioPlayer.play(src);
print("play");
}
},
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(100),
color: Colors.black,
),
height: screenWidth / 7,
width: screenWidth / 7,
child: Icon(
playIcon,
color: Colors.white,
),
),
),
ElevatedButton(
onPressed: () {},
child: const Icon(
CupertinoIcons.forward_end_alt_fill,
color: Colors.black,
)),
IconButton(onPressed: () {}, icon: Icon(Icons.repeat, color: Colors.grey[500],))
],
),
SizedBox(
height: screenHeight / 20,
),
const Text(
"Song name",
style: TextStyle(
color: Colors.white,
fontSize: 30,
fontWeight: FontWeight.bold),
),
const Text(
"Don't forget to close your eyes!",
style: TextStyle(
color: Colors.white,
fontSize: 17,
fontWeight: FontWeight.bold),
),
const Text(
"Duration",
style: TextStyle(fontSize: 17, fontWeight: FontWeight.bold),
),
],
),
)
],
),
);
}
}
It seems that you're trying to play a youtube as an audiosource, by the way I look at your comments
Unfortunately audioplayers does not work that way! You should include the right url that is an actual sound file url (wav, mp3,... if you know what i mean)
If by any case the actual sound file URL works when opening with the browser and not the simulator (example: using Chrome is fine but ios simulator fails), check in the type of the sounds.