Change audio player
This commit is contained in:
@ -3,12 +3,13 @@
|
||||
import 'dart:math';
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:audioplayers/audioplayers.dart';
|
||||
import 'package:chewie_audio/chewie_audio.dart';
|
||||
import 'package:fluent_ui/fluent_ui.dart' as fluent;
|
||||
import 'package:fluentui_system_icons/fluentui_system_icons.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:music_web_player/api.dart';
|
||||
import 'package:music_web_player/ui/cover_image.dart';
|
||||
import 'package:video_player/video_player.dart';
|
||||
|
||||
class MusicPlayer extends StatefulWidget {
|
||||
final MusicsList musicsList;
|
||||
@ -22,8 +23,10 @@ class MusicPlayer extends StatefulWidget {
|
||||
class _MusicPlayerState extends State<MusicPlayer> {
|
||||
final rng = Random();
|
||||
|
||||
final audioPlayer = AudioPlayer(playerId: "player");
|
||||
var _playerState = PlayerState.STOPPED;
|
||||
VideoPlayerController? _videoPlayerController;
|
||||
ChewieAudioController? _chewieAudioController;
|
||||
|
||||
bool get _isPlaying => _chewieAudioController?.isPlaying ?? false;
|
||||
|
||||
final List<MusicEntry> stack = [];
|
||||
int currMusicPos = 0;
|
||||
@ -40,37 +43,33 @@ class _MusicPlayerState extends State<MusicPlayer> {
|
||||
return stack[currMusicPos];
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
audioPlayer.onPlayerError.listen((event) {
|
||||
print("Player error!");
|
||||
print(event);
|
||||
_playNext();
|
||||
});
|
||||
|
||||
audioPlayer.onPlayerStateChanged.listen((s) => setState(() {
|
||||
_playerState = s;
|
||||
|
||||
if (_playerState == PlayerState.COMPLETED) _playNext();
|
||||
}));
|
||||
}
|
||||
|
||||
Future<void> _play() async {
|
||||
if (_playerState == PlayerState.PAUSED) {
|
||||
await audioPlayer.resume();
|
||||
if (_chewieAudioController != null) {
|
||||
await _chewieAudioController!.play();
|
||||
} else {
|
||||
await audioPlayer.play(currMusic.musicURL);
|
||||
_videoPlayerController =
|
||||
VideoPlayerController.network(currMusic.musicURL);
|
||||
await _videoPlayerController!.initialize();
|
||||
_chewieAudioController = ChewieAudioController(
|
||||
videoPlayerController: _videoPlayerController!,
|
||||
autoPlay: true,
|
||||
showControls: false);
|
||||
}
|
||||
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
Future<void> _stop() async {
|
||||
await audioPlayer.stop();
|
||||
_chewieAudioController?.dispose();
|
||||
_videoPlayerController?.dispose();
|
||||
|
||||
_chewieAudioController = null;
|
||||
_videoPlayerController = null;
|
||||
}
|
||||
|
||||
void _pause() async {
|
||||
await audioPlayer.pause();
|
||||
await _chewieAudioController?.pause();
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
void _playPrevious() async {
|
||||
@ -157,11 +156,10 @@ class _MusicPlayerState extends State<MusicPlayer> {
|
||||
),
|
||||
const Spacer(),
|
||||
IconButton(
|
||||
icon: PlayerIcon(_playerState == PlayerState.PLAYING
|
||||
icon: PlayerIcon(_isPlaying
|
||||
? fluent.FluentIcons.pause
|
||||
: fluent.FluentIcons.play),
|
||||
onPressed:
|
||||
_playerState == PlayerState.PLAYING ? _pause : _play,
|
||||
onPressed: _isPlaying ? _pause : _play,
|
||||
),
|
||||
const Spacer(),
|
||||
IconButton(
|
||||
|
Reference in New Issue
Block a user