How to set audio handler with carousel slider in flutter? - flutter

Currently i am working on music app and according to my ui i have to manage audio player with carousel slider.The without carousel slider it is working fine but with carousel slider when click on the shuffle or when onPageChanged called, i used the skipToQueueItem for play particular song with audio player, that time suddenly audioHandler.mediaItem listen continuously called and in every 1 second changed song and slider images because of skipToQueueItem line.I used just audio and audio service package.For privacy i am not sharing the code.But for update carousel slider with audio i used below code.
audioHandler.queue.listen((playlist) async {
if (playlist.isEmpty) {
mediaItemsListNotifier.value = [];
} else {
///Update media items notifier list value
mediaItemsListNotifier.value = playlist;
await audioHandler.updateQueue(playlist);
}
});
audioHandler.mediaItem.listen((mediaItem) async {
int index = audioHandler.queue.value.indexOf(mediaItem!);
currentSliderIndex.value = index >= 0 ? index : 0;
///Main issue using this line
await audioHandle.skipToQueueItem(currentSliderIndex.value);
carouselController.jumpToPage(currentSliderIndex.value);
}

Related

How to know if the video reached the last position when using video_player in flutter?

I am using video_player for displaying a video. I want to know the if the video reached the last position to display a replay icon and do some logic. How can we approach this in flutter.
_videoController.addListener(() { //custom Listner
setState(() {
if (!_videoController.value.isPlaying &&_videoController.value.initialized &&
(_videoController.value.duration ==_videoController.value.position)) { //checking the duration and position every time
//Video Completed//
// code here to show reply button//
}
});
})

Flutter video_player Duration?

I am using the Flutter video_player package here: https://pub.dev/packages/video_player
How do I get the duration of the video? I can see there is a position property, so I would need that to get the current value in time.
But how do I get the total duration of the video? The video is from a URL.
I am making a custom player so I need these two values.
For getting the Total duration you can use the video controller
VideoPlayerController _controller = VideoPlayerController.network('https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4')
..initialize().then((_) {
// Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
});
Duration durationOfVideo = _controller.value.duration;
You can also directly store the integer instead of duration as below image
You can create a function to calculate the video Dutarion and format it.
getVideoPosition() {
var duration = Duration(milliseconds: videoController.value.position.inMilliseconds.round());
return [duration.inMinutes, duration.inSeconds].map((seg) => seg.remainder(60).toString().padLeft(2, '0')).join(':');
}
After craeate this function, you just need to call it to show the duration.
Ex.:
Text(getVideoPosition())

How to get audio duration with audio_service?

I am using audio_service to play mp3 file. I am following example_playlist as AudioPlayerHander. My code is exactly same as example. But I use dependency injection (get_It) rather than a global variable. And I replace with broadcast media item changes in _init() method to get duration.
Rx.combineLatest5<int?, List<MediaItem>, bool, List<int>?, Duration?,
MediaItem?>(
_player.currentIndexStream,
queue,
_player.shuffleModeEnabledStream,
_player.shuffleIndicesStream,
_player.durationStream, // <- add listening to durationStream here
(index, queue, shuffleModeEnabled, shuffleIndices, duration) {
if (kDebugMode) {
print("${mediaItem.value?.title} - $duration");
}
final queueIndex =
getQueueIndex(index, shuffleModeEnabled, shuffleIndices);
return (queueIndex != null && queueIndex < queue.length)
? queue[queueIndex].copyWith(
duration: duration) // <- sink mediaItem provided with duration
: null;
}).whereType<MediaItem>().distinct().listen(mediaItem.add);
But I did not get the duration correctly.
For playlist 1, it is upload in _init() method and first track's duration is showing correctly but if you click second track, duration is still showing first track's duration.
I have added Load Play List 2 button and load playlist 2. All track's duration is always zero.
Note: audio is playing and start progress from 0 if duration is zero.
If I added playlist 2 in audio_service's example_playlist, it is showing correctly.
So the only different is using global variable and dependency injection (getIt).
I have added sample Github project audio_service_playlist_test
May I know which path is missing?

Flutter - Stop audio playback from recorded video in the background

Essentially the app is like snapchat. I take pics and reset back to camera mode, the issue comes when I record video and reset, it goes back to camera mode but the audio form the video keeps playing in the background. The functions are somwhat exactly like the camera doc, with a few addition to reset the camera.
I added this:
_reset() {
if (mounted)
setState(() {
if (this._didCapture) {
this._didCapture = false;
this._isRecording = false;
this._isPosting = false;
this._file = File('');
this._fileType = null;
this._captions.clear();
this._textEditingControllers.clear();
this._videoController = null;
this._videoPlayerListener = null;
}
});
}
It works just fine but the audio in the background is still on. Also wondering if the video/picture is saved on the phone, which I don't want to...
i had been looking for a similar answer, but i didn´t find it. You could try to stop it adding this to your function:
this._controller.setVolume(0.0);
that´s what i did in my app

Can't stop audio playback on iOS with Codename One

My Codename One app features audio playback in the background when the user taps the screen. The audio I use is an mp3. Here is how I use the Media playback :
public static void playSound(boolean stop) {
sound.reset(); // The input stream needs to go back to the beginning
Media myClip = MediaManager.createMedia(sound, "audio/mp3", () -> {
// If there is no order to stop playback, we keep playing when it has completed (looping)
playSound(false);
});
if (!stop) {
myClip.play();
} else {
myClip.cleanup();
}
}
So hen the user taps the screen components change and I pass true to playSound method. On Android the current playback stops not on iOS with an iPhone 4.
Please note that when the app gets minimized (center button pressed) the playback stops (even if I don't call cleanup() on the Media which I do on Android to stop the playback when the app is minimized).
How can I stop the playback on iPhone ?
Any help appreciated,
#Shai pointed me to the right direction so here is the code finally used :
Media myClip = null;
public static void playSound(boolean stop) {
sound.reset(); // The input stream needs to go back to the beginning
/**
* If the media is playing we don't create it
* otherwise we would have several media in the wild
* that could not be stopped
*/
if (myClip == null || !myClip.isPlaying()) {
myClip = MediaManager.createMedia(sound, "audio/mp3", () -> {
// If there is no order to stop playback, we keep playing when it has completed (looping)
playSound(false);
});
}
if (!stop) {
myClip.play();
} else {
myClip.cleanup();
}
}