CameraController not starting video recording instantly Flutter - flutter

I am trying to play a beep sound with vibration before starting video recording. However, there is a delay for _cameraController.startVideoRecording()
to start video recording and this delay is not constant. Each time I try recording the delay is different.
How can I do this in a way that recording starts instantly after the beep sound finishes.
my code:
Future _handleRecording() async {
try {
await _audioCache.play(_soundPath);
Vibration.vibrate(duration: 350);
await _cameraController.startVideoRecording();
} catch (e) {
throw (e);
}
}
I am using camera: ^0.8.1+7 dependency the CameraController class.

The problem here is you are calling the startVideoRecording after initiating the play function
You have two options since your audio duration is fixed :
First one is to surround you startVideoRecording with delay like that:
Future.delay(Duration(seconds:"You audio time"),(){
await _cameraController.startVideoRecording();
} catch (e) {
throw (e);
}
});
and the second one is to check when play finish then call startVideoRecording

Related

Flutter: AudioPlayers how to stop multiple sounds at once?

I use AudioPlayers package.
I have Button1 that plays the sounds. (Code Below)
AudioCache playerCache = new AudioCache(); // you already initialized this
AudioPlayer player = new AudioPlayer();
void _playFile(String yol, String name) async {
player = await playerCache.play(yol);
}
But there is another button which calling "Button2" has to stop all of the sounds at once. I wrote this :
void cancelPlay() {
print("stop");
playSounds.removeRange(0, playSounds.length);
player.stop();
player.stop();
}
However , when user click Button2 , it only stops the last sound. I want that to stop all of the sounds. How to do that ?
I guess The problem is, that every time i call _playFile() (press Button 1) a new instance of AudioPlayer is assigned to the player variable, hence in cancelPlay() the player variable holds only the last instance of AudiPlayer.
How can i do to store the instances in a list.
Thank you.
make a variable
List<AudioPlayer> audioPlayers = [];
every time you tap button1, add new audioPlayer
audioPlayers.add(new AudioPlayer());
then when you tap on button2, do something like this to stop every audioPlayer
audioPlayers.forEach((audioPlayer) => audioPlayer.stop());
or even better would be function to toggle the AudioPlaybackState of audioPlayers, so if they are paused then play and vice versa. Ofcourse this example is based on presumption that every audioPlayer controller has same AudioPlaybackState as first:
void toggleAudioPlayers() {
if (audioPlayers.first.playbackState == AudioPlaybackState.playing) {
audioPlayers.forEach((audioPlayer) => audioPlayer.stop());
} else {
audioPlayers.forEach((audioPlayer) => audioPlayer.play());
}
}
Dont forget to dispose every audioPlayer controller to prevent memory leaks:
audioPlayers.forEach((audioPlayer) => audioPlayer.dispose());
AudioPlayer.players.forEach((key, value) {
value.stop();
});
when you play another audio you need to call this code before

Flutter Audioplayers delay

I'm coding a small game with the Flutter Framework.
I'm using audioplayers for the Sounds.
It works fine like this, when calling it for example 2 times a second.
But whenn I call it more than 5 times and again in the next second at some point the sound has like a delay and then after a second or so all the sounds play at once :) That sounds weired.
I also tested the audioplayers example from github on my iphone. Repeating the sounds in low frequency is ok, but when I repeat clicking the button as fast as possible at some point it gets some delay and the same thing is happening.
Is there some way to stop the previous Sound before and then playing the next one or isnt this possible?
Or is there some other Idea how to deal with the sounds?
This is how I use it:
AudioCache upgradeSound = new AudioCache();
void playUpgradeSound() {
_playUpgradeSound(upgradeSound);
}
void _playUpgradeSound(AudioCache ac) async{
await ac.play('audio/upgr.mp3');
}
Thank you very much in advance
I solve similar problem by having singleton class, and after first play I can get the state, and I can stop previous play.
class Audio {
static final playerCache = AudioCache();
static AudioPlayer audioPlayer;
static void play(String audioName) async {
audioPlayer = await playerCache.play('$audioName.mp3');
}
static void stop() {
audioPlayer.stop();
}
}
...
child: IconButton(
onPressed: () {
try {
if (Audio.audioPlayer.state ==
AudioPlayerState.PLAYING) {
Audio.stop();
} else {
Audio.play('bid');
}
} catch (e) {
Audio.play('bid');
}
},
...
There is a line of code in its own documentation.
To use the low latency API, better for gaming sounds, use:
AudioPlayer audioPlayer = AudioPlayer(mode: PlayerMode.LOW_LATENCY);
In this mode the backend won't fire any duration or position updates. Also, it is not possible to use the seek method to set the audio a specific position. This mode is also not available on web.
I hope it is useful.

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();
}
}

html5 audio tag playing audio when it is load audio

i have html5 audio player like play, pause, stop buttons.
function onUpdate()
{
var isPlaying = (!getAudioElement().paused);
document.getElementById("playerplay").style.display = (isPlaying)?"none":"block";
document.getElementById("playerpause").style.display = (isPlaying)?"block":"none";
};
function getAudioElement()
{
return document.getElementById("id_audio_element");
}
function play() {
getAudioElement().play();
onUpdate();
}
function pause() {
getAudioElement().pause();
onUpdate();
}
function stop() {
getAudioElement().pause();
getAudioElement().currentTime=0;
onUpdate();
getAudioElement().load();
}
And audio tag:
<audio id="id_audio_element" onended="onUpdate();load();"><source src="105.ogg" type="audio/ogg"></audio>
it works all on browsers perfectly, exept iPhone and iPad's Safari and Chrome. When I call load() , audio starts playing, although I don't call play().
Anybody has a solution? Thanks!
If the source is not changing, there is no need to call load(). If it is changing, try calling pause() after it loads.

iphone audio streaming

i'm developing an application which uses audio streaming. For streaming audio from internet i'm using the AudioStreamer class. The audio streamer has
four state isPlaying, isPaused ,isWaiting, and isIdle . My problem is that when the audio streamer is in the state "isWaiting" and at that time if i get a phone call Audio queue fails giving the error "Audio queue start failed." Any has solution for this? help....
An interruption - such as receiving a call - will deactivate the Audio Session. It's up to you to, in your interruption handler, re-activate the Audio Session by calling AudioSessionSetActive(true).
Look here for details of the interruption handler.
Having said that, I'll assume that you're using mattgallagher's library. The thing to do is put a breakpoint in AudioStreamer.m on line 949 (the line after "err = AudioQueueStart(audioQueue, NULL);" in -[AudioStreamer pause].
If err == kAudioSessionNotActiveError then my theory's right, and you need to call restart the audio session. Perhaps something like this (but I've only only ever glanced through this code so perhaps there's a better way of solving the problem):
else if (state == AS_PAUSED)
{
err = AudioQueueStart(audioQueue, NULL);
if (err) {
err = AudioSessionSetActive(true);
if (err) {
[self failWithErrorCode:AS_AUDIO_QUEUE_START_FAILED];
return;
} else {
err = AudioQueueStart(audioQueue, NULL);
if (err) {
[self failWithErrorCode:AS_AUDIO_QUEUE_START_FAILED];
return;
}
}
}
}