Can't play, but no error occured.
Flutter Version
My version : 2.11.0-0.0.pre.501
Lib Version
My version : 3.0.4
Platform (Android / iOS / web) + version
Platform : Android
Describe the bug
I created this class:
import 'package:assets_audio_player/assets_audio_player.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:mobx/mobx.dart';
import 'package:netease_cloudmusic_flutter/api/song_url.dart';
import 'stores/song.dart';
import 'stores/player_store.dart';
final Player player = Player();
class Player {
AssetsAudioPlayer _player = AssetsAudioPlayer();
void updatePlaylist(ObservableList<Song> songs) =>
storePlayer.updatePlaylist(songs);
Future<void> play(int index) async {
String _songUrl = await songUrl(storePlayer.playlist[index].id);
if (_songUrl != '') {
_player.open(
Playlist(audios: [
Audio.network(_songUrl,
metas: Metas(
title: storePlayer.playlist[index].name,
artist: storePlayer.playlist[index].combinedArtistsName(),
image: MetasImage.network(
storePlayer.playlist[index].album.coverUrl),
album: storePlayer.playlist[index].album.name),
cached: true)
]),
loopMode: LoopMode.single,
showNotification: true,
notificationSettings: NotificationSettings(
customPrevAction: (player) => play((index != 0) ? index - 1 : index),
customNextAction: (player) =>
play((index != storePlayer.playlist.length) ? index + 1 : index),
),
);
print(_player.playlist?.audios);
} else {
Fluttertoast.showToast(msg: "be banned from NetEase");
}
}
}
Then I ran player.play(index) in other places, I could see print(_player.playlist?.audios) was pringting [Audio{.......}], but I changed the index, the printed [Audio{.......}] didn't changed (I was sure there wasn't any problem with storePlayer.playlist). _player also printed something, like /data/user/0/com.example.netease_cloudmusic_flutter/cache/https___m10.music.126.net_20220312211651_76680c50e4474620e03775cd42531994_ymusic_0358_070c_0008_428833216f0f1386ff2c7b31933182e7.mp3, but it only printed once.
During all my trying the audio wasn't playing.
Try something like below.
- Please add open method inside try catch and don't forget to add await before opening player as it is a future.
Try something like below.
* Please add open method inside try catch and don't forget to add await before opening player as it is a future.
I tried, still no new reports.And await didn't work.
However, this was useful:
_player.playlist?.audios[0] = Audio.network(_songUrl,
metas: Metas(
title: storePlayer.playlist[index].name,
artist: storePlayer.playlist[index].combinedArtistsName(),
image: MetasImage.network(
storePlayer.playlist[index].album.coverUrl),
album: storePlayer.playlist[index].album.name),
cached: true);
_player.playlistPlayAtIndex(0);
But I thought it was not a good solution.
This partially solved my problem (order of await _player.open and _player.playlist?.audios[0] = Audio.network didn't matter) :
import 'package:assets_audio_player/assets_audio_player.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:mobx/mobx.dart';
import 'package:netease_cloudmusic_flutter/api/song_url.dart';
import 'stores/song.dart';
import 'stores/player_store.dart';
final Player player = Player();
class Player {
final AssetsAudioPlayer _player = AssetsAudioPlayer();
late int index;
void updatePlaylist(ObservableList<Song> songs) =>
storePlayer.updatePlaylist(songs);
Future<void> play(int index) async {
this.index = index;
String _songUrl = await songUrl(storePlayer.playlist[this.index].id);
if (_songUrl != '') {
_player.playlist?.audios[0] = Audio.network(_songUrl,
metas: Metas(
title: storePlayer.playlist[this.index].name,
artist: storePlayer.playlist[this.index].combinedArtistsName(),
image: MetasImage.network(
storePlayer.playlist[this.index].album.coverUrl),
album: storePlayer.playlist[this.index].album.name),
cached: false);
await _player.open(
Playlist(audios: [
Audio.network(_songUrl,
metas: Metas(
title: storePlayer.playlist[this.index].name,
artist:
storePlayer.playlist[this.index].combinedArtistsName(),
image: MetasImage.network(
storePlayer.playlist[this.index].album.coverUrl),
album: storePlayer.playlist[this.index].album.name),
cached: true)
]),
loopMode: LoopMode.single,
showNotification: true,
notificationSettings: NotificationSettings(
customPrevAction: (player) async =>
await play((this.index != 0) ? this.index - 1 : this.index),
customNextAction: (player) async => await play(
(this.index != storePlayer.playlist.length)
? this.index + 1
: this.index),
),
);
await _player.playlistPlayAtIndex(0);
} else {
Fluttertoast.showToast(msg: "be banned from NetEase");
}
}
}
But I wanted the first cached to be true (that truly enabled cache). If it was, I had to run player.play(index) twice to play (It seemed that the first time it started to cache, the second time it started to play).