Continu my music after a pause in just_audio plugin - flutter

I use the plugin just_audio
How can I continu my music after to do a pause? please.
void _play() {
audioPlayer.setAsset("assets/applause.mp3");
audioPlayer.play();
}
void _pause() async {
await audioPlayer.pause();
}
void _rePlay() async {
?????????????????????????
}

In general, after calling pause, call play to "resume".
In your case, you should NOT set the asset file again. This resets the process.
Below is your code, pasted from your question:
void _play() { // BAD EXAMPLE
audioPlayer.setAsset("assets/applause.mp3"); // <-- PROBLEM
audioPlayer.play();
}
You should move setAsset call elsewhere, like in init state.

You can just call audioPlayer.play(). This works exactly like the play button in a music app, while pause() works exactly like the pause button in a music app.
It is designed this way so that you can simply wire up your play and pause buttons directly to these player methods.
For example, here are the play/pause buttons from the just_audio example app:
// Play button:
IconButton(
icon: Icon(Icons.play_arrow),
onPressed: audioPlayer.play,
)
// Pause button:
IconButton(
icon: Icon(Icons.pause),
onPressed: audioPlayer.pause,
)
You can just alternate calls to play()/pause()/play()/pause(). Note the left side of this state diagram from the README:

Related

How can I get the details of currently playing song in miniplayer?

I am working on a music player app in flutter.
I fetch the songs from devices using on_audio_query package and display them in listview. When I click on a song, i set the plylist of all songs and play the one thats clicked using just_audio package like this.
await player.setAudioSource(
ConcatenatingAudioSource(
useLazyPreparation: true,
shuffleOrder: DefaultShuffleOrder(),
children: allSongsList! // allSongsList is the list of
songs(on_audio_query)
.map((songModel) =>
AudioSource.uri(Uri.parse(songModel.data)))
.toList(),
),
initialIndex: index, // Listview Index
initialPosition: Duration.zero);
await item.player.play();
I want to show miniplayer at the bottom only when there is a song playing(paused),refresh the song descriptions,how do I get the song description(Artist/song)?
I will let you the implementation of the mini player widget for you, however, I will consider that I have a shouldMiniPlayerBeVisible, in the Scaffold of your page:
First, declare and initialize a shouldMiniPlayerBeVisible variable that will manage the show/hide of the mini player:
bool shouldMiniPlayerBeVisible = false;
and a currentAudio which will take the song and pass it in the mini player.
YourAudioModel currentAudio = YourAudioModel();
Here, I supposed that I have a simple YourAudioModel which will hold information about audio such as title, description, length, URL (or local) path ...
I recommend not setting it to null initially , you can initialize it with a placeholder YourAudioModel audio model that has some dummy data, or loading data information for example...
now in your Scaffold:
Scaffold(
floatingActionButton: Visibility(
visible: shouldMiniPlayerBeVisible,
child: shouldMiniPlayerBeVisible(songToPlay: currentAudio),
),
),
I will consider that you are using a StatefulWidget, but the steps are the same as you need just to get the idea of it to implement it in other state management solutions.
in the initState, you need to listen to the playerStateStream stream like this:
player.playerStateStream.listen((state) {
if (state.playing) {
shouldMiniPlayerBeVisible = true;
} else {
shouldMiniPlayerBeVisible = false;
}
setState(() {});
});
this basically will track if any audio is playing or not, then show the mini player based on it.
now before your run the await item.player.play();, set your currentAudio variable to the new audio which is running, you can do it with a function like this:
Future<void> playCurrentAudioANdShowItInMiniPlayer(YourAudioModel audio) async {
currentAudio = YourAudioModel;
await item.player.play();
}
now from your UI, for each item in ListView, you can just call the playCurrentAudioANdShowItInMiniPlayer method with its YourAudioModeland expect it to work fine.
from here, you should implement the actual mini player widget, and you will need also a custom Navigator widget so that mini player will be on the screen even if you navigated to other screens, well I faced this issue before and the miniplayer worked just really fine and good as I expected, I recommend you to use it.

i am actually making flappy bird as my first project in flutter. how can i add an asset audio in gesturedetector?

[here is the write and gesture detector code](https://i.stack.imgur.com/RmUfF.png)
I tried adding this
but it didnt work . i just want when i click on the music button it should play the background audio in assets and if click on no music it should stop the background music. but that state should be maintained after applying.
void initState(){
super.initState();
setAudio();
audioPlayer.onPlayerStateChanged.listen((state) {
setState(() {
isPlaying = state == PlayerState.PLAYING;
});
});

Flutter: How to call function of Home button (home button default of Android)?

I have a button on the View and how to call function of Home button in onClick function of button?
Code example:
InkWell(
onTap: () {
//function of home button
},)
You could use this package:
https://pub.dev/packages/move_to_background
Meaning your code could be something like this:
InkWell(
onTap: () {
MoveToBackground.moveTaskToBack();
},
)
For what you are trying to achieve you need a background notifier because the home button sends app to background. There is a package called flutter_fgbg which can help you - click here pub.dev. Then you can proceed to stream background events, eg:
import 'package:flutter_fgbg/flutter_fgbg.dart';
StreamSubscription<FGBGType> subscription;
...
// in initState
subscription = FGBGEvents.stream.listen((event) {
if (event == FGBGType.foreground){
//if its come back to foreground, then restart your app
} else {
//if its gone to background
}
});
// in dispose
subscription.cancel();
For more assistance, you can check this link, for visual aid in solving problems Mufungo Geeks YouTube

How to identify if the controls has gone or not in flutter chewie video player

I am working on a flutter app and using chewie videoplayer plugin.So when we started playing a video it shows controls at bottom and after few seconds it vanishes and when we move the mouse again it shows again. so is there any method to find when the controls are shown on screen and when its not shown.
actually i am giving close button on the video player. but it doesnt vanishes with those video player controls. it still stay on screen so to hide that close button along with video controls i need to get which process hides the control.
Please help me...
This process is inside the source code of chewie player. So if you need to customize and synchronise your own custom controls and options, either you have to explicitly fetch the source code and edit in that or you need to make your own controllers from scratch using video_player package as the core.
You need to customize the source code of the chewie player.
You can find MaterialControl class inside the source code of this package there is a function called _buildHitArea() just change notifier.hideStuff boolean according to your preference.
Widget _buildHitArea() {
final bool isFinished = _latestValue.position >= _latestValue.duration;
final bool showPlayButton = widget.showPlayButton && !_dragging && !notifier.hideStuff;
return GestureDetector(
onTap: () {
if (_latestValue.isPlaying) {
if (_displayTapped) {
setState(() {
notifier.hideStuff = false;
});
} else {
_cancelAndRestartTimer();
}
} else {
_playPause();
setState(() {
notifier.hideStuff = false;
});
}
},
child: CenterPlayButton(
backgroundColor: Colors.black54,
iconColor: Colors.white,
isFinished: isFinished,
isPlaying: controller.value.isPlaying,
show: showPlayButton,
onPressed: _playPause,
),
);
}

Playing videos with flutter web and firebase storage

I am trying to make a website in which the user can add videos and also see these videos. All of them are stored in firebase storage. This is how I store it
fb.StorageReference storageRef = fb.storage().ref('videos/$chapter)');
fb.UploadTaskSnapshot uploadTaskSnapshot = await storageRef.put(videoFile, fb.UploadMetadata(contentType: 'video/mp4')).future;
await uploadTaskSnapshot.ref.getDownloadURL().then((value){
videoUrl = value.toString();
});
snapshot.reference.collection("videos").add({
"chapter":chapter,
"class":grade,
"description":description,
"notes":notes,
"subject":subject,
"video":videoUrl,
"timestamp":DateTime.now().millisecondsSinceEpoch.toString()
});
And I play them using the normal video player plugin. Now the problem is when I store it, I get a url like
https://firebasestorage.googleapis.com/v0/b/studyme-me.appspot.com/o/videos%2Ff)?alt=media&token=ec4fea39-032b-438f-8122-f8e042c1c9c7
But the video player in flutter web requires a .mp4 or .wav or something like that file. What can I do that can either allow the video player to play these (I don't think its possible) or I can get the .mp4 file for this. Maybe I can use firebase storage to open this url and get it but I dont know how
Any suggestions would be appreciated
You can use the VideoPlayerController plugin to do that
VideoPlayerController controller = VideoPlayerController.network('your-video-url',);
//Play/Pause Video:
FloatingActionButton(
onPressed: () {
// Wrap the play or pause in a call to `setState`. This ensures the
// correct icon is shown
setState(() {
// If the video is playing, pause it.
if (_controller.value.isPlaying) {
_controller.pause();
} else {
// If the video is paused, play it.
_controller.play();
}
});
},
// Display the correct icon depending on the state of the player.
child: Icon(
_controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
),
)
'''
more examples in the link above