Reputation: 21
I'm encountering an issue while trying to implement background music playback in my app using the Just Audio Background package. Whenever the music playback starts, I receive the error message "type 'Null' is not a subtype of type 'MediaItem'." This error occurs consistently each time I initiate music playback. However, when I restart the app a few times, the music starts playing without any errors.
I fetch the music data from the device's memory using the OnAudioQuery package, and I'm able to retrieve the music successfully. However, after adding the Just Audio Background package, the music doesn't start playing initially. It only begins playing after restarting the app multiple times.
Below is the code snippet I've used to play music using the Just Audio Background package. I didn't encounter any issues before adding this package.
// fetchMusic method is used to fetch music from the device's memory
Future<void> fetchMusic() async {
_audioQuery = OnAudioQuery();
_musicList = await _audioQuery.querySongs(
sortType: SongSortType.DATE_ADDED,
orderType: OrderType.DESC_OR_GREATER,
uriType: UriType.EXTERNAL,
ignoreCase: true,
);
_artistList = await _audioQuery.queryArtists();
notifyListeners();
}
MusicPlayerController class controls the music playback
class MusicPlayerController extends ChangeNotifier {
final player = AudioPlayer();
var shuffledList;
var numbersSet = <int>{};
int _playingIndex = 0;
final GetMusicProvider _musicProvider;
late PlayerStateles _playerStateles;
late BuildContext context;
MusicPlayerController(this._musicProvider, this._playerStateles) {
player.playerStateStream.listen((state) {
if (state.processingState == ProcessingState.completed) {
playOrPause(_playingIndex+1,
_musicProvider.musicList[_playingIndex+1].uri.toString());
}
});
}
Future<void> playOrPause(int index, String uri) async {
log("1");
if (index == _playingIndex) {
if (player.playing){
player.stop();
icon(index);
} else {
player.play();
icon(index);
}
} else {
MediaItem? mediaItem;
mediaItem = MediaItem(
id: _musicProvider.musicList[index].id.toString(),
title: _musicProvider.musicList[index].title,
);
player.setAudioSource(AudioSource.uri(
Uri.parse(uri),
tag: mediaItem,
));
player.play();
_playingIndex = index;
_playerStateles.updateCurrentIndex(index);
icon(index);
_musicProvider.refreshPage();
}
notifyListeners();
}
Icon icon(int index) {
if (index == _playingIndex) {
return player.playing ? Icon(Icons.pause_circle) :
Icon(Icons.play_circle_outline_outlined) ;
} else {
return Icon(Icons.play_circle_outline_outlined);
}
}
}
all error E/flutter (15426): [ ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'Null' is not a subtype of type 'MediaItem' in type cast E/flutter (15426): #0 _PlayerAudioHandler._updateQueue.
(package:just_audio_background/just_audio_background.dart:597:51)
E/flutter (15426): #1 MappedListIterable.elementAt
(dart:_internal/iterable.dart:425:31)
E/flutter (15426): #2 ListIterator.moveNext
(dart:_internal/iterable.dart:354:26)
E/flutter (15426): #3 new
_GrowableList._ofEfficientLengthIterable (dart:core-
patch/growable_array.dart:189:27)
E/flutter (15426): #4 new _GrowableList.of (dart:core-
patch/growable_array.dart:150:28)
E/flutter (15426): #5 new List.of (dart:core-
patch/array_patch.dart:39:18)
E/flutter (15426): #6 ListIterable.toList
(dart:_internal/iterable.dart:224:7)
E/flutter (15426): #7 _PlayerAudioHandler._updateQueue
(package:just_audio_background/just_audio_background.dart:597:65)
E/flutter (15426): #8 _PlayerAudioHandler.customLoad
(package:just_audio_background/just_audio_background.dart:482:5)
E/flutter (15426): #9 _JustAudioPlayer.load
(package:just_audio_background/just_audio_background.dart:237:27)
E/flutter (15426): #10 AudioPlayer._load
(package:just_audio/just_audio.dart:850:12)
Upvotes: 0
Views: 117
Reputation: 1
You just import the audio service library or Just audio background service library to solve this query,as well add media item like that.
AudioSource.uri( Uri.parse(item.music), tag: MediaItem( id: "${item.id}", album: "${musicImage}", title: "${item.name}", artUri: Uri.parse('${musicImage}'), ), ),
import 'package:just_audio_background/just_audio_background.dart'; import 'package:audio_service/audio_service.dart';
Upvotes: 0