Flutter player wont play audio from podbean - android

guys, I'm building a music app in flutter and I have a problem which I don't know how to resolve, so if someone can help me out on this.
So this is the thing I have songs that are stored on CloudFront and they work perfectly
play(song, context) async {
int result = await audioPlayer.play(song); //path to the song
setState(() {
isPlaying = true;
});
// int seek = await audioPlayer.seek(Duration(minutes: 10, seconds: 10));
if (result == 1) {
return 'succes';
}
}
but when I give another path from podbean it will not start playing "https://mcdn.podbean.com/mf/play/9i7qz8/Japji_Sahib_Pauri_3.mp3" -> this is the link you can play it in browser
Error message. Shows after some time on the initial play of the podcast
Any idea what I can do about this problem.
Thanks in advance.

Related

How to loop background audio without gap?

The app plays some background audio (wav) in an endless loop. Audio playback was flawless using just_audio package, but after switching to audioplayers, all looped audio now contain a short gap of silence before they restart at the beginning. Question now is: How to get rid of the gap?
The code to load and play the files is quite simple:
Future<void> loadAmbient() async {
_audioAmbient = AudioPlayer();
await _audioAmbient!.setSource(AssetSource('audio/ambient.wav'));
await _audioAmbient!.setReleaseMode(ReleaseMode.loop);
}
void playAmbient() async {
if (PlayerState.playing != _audioAmbient?.state) {
await _audioAmbient?.seek(Duration.zero);
_audioAmbient?.resume();
}
}
According to the audioplayers docs, this seems to be a known issue:
The Getting Started docs state:
Note: there are caveats when looping audio without gaps. Depending on the file format and platform, when audioplayers uses the native implementation of the "looping" feature, there will be gaps between plays, witch might not be noticeable for non-continuous SFX but will definitely be noticeable for looping songs. Please check out the Gapless Loop section on our Troubleshooting Guide for more details.
Following the link to the [Trouble Shooting Guide] (https://github.com/bluefireteam/audioplayers/blob/main/troubleshooting.md) reveals:
Gapless Looping
Depending on the file format and platform, when audioplayers uses the native implementation of the "looping" feature, there will be gaps between plays, witch might not be noticeable for non-continuous SFX but will definitely be noticeable for looping songs.
TODO(luan): break down alternatives here, low latency mode, audio pool, gapless_audioplayer, ocarina, etc
Interesting is the Depending on the file format and platform, ... part, which sounds as if gap-less loops could somehow be doable. Question is, how?
Any ideas how to get audio to loop flawless (i.e. without gap) are very much appreciated. Thank you.
You can pass this parameter
_audioAmbient = AudioPlayer(mode: PlayerMode.LOW_LATENCY);
if not solved u can try something new like https://pub.dev/packages/ocarina
I fixed it this way:
Created 2 players, set asset path to both of them and created the method that starts second player before first is completed
import 'package:just_audio/just_audio.dart' as audio;
audio.AudioPlayer player1 = audio.AudioPlayer();
audio.AudioPlayer player2 = audio.AudioPlayer();
bool isFirstPlayerActive = true;
StreamSubscription? subscription;
audio.AudioPlayer getActivePlayer() {
return isFirstPlayerActive ? player1 : player2;
}
void setPlayerAsset(String asset) async {
if (isFirstPlayerActive) {
await player1.setAsset("assets/$asset");
await player2.setAsset("assets/$asset");
} else {
await player2.setAsset("assets/$asset");
await player1.setAsset("assets/$asset");
}
subscription?.cancel();
_loopPlayer(getActivePlayer(), isFirstPlayerActive ? player2 : player1);
}
audio.AudioPlayer getActivePlayer() {
return isFirstPlayerActive ? player1 : player2;
}
void _loopPlayer(audio.AudioPlayer player1, audio.AudioPlayer player2) async {
Duration? duration = await player1.durationFuture;
subscription = player1.positionStream.listen((event) {
if (duration != null &&
event.inMilliseconds >= duration.inMilliseconds - 110) {
log('finished');
player2.play();
isFirstPlayerActive = !isFirstPlayerActive;
StreamSubscription? tempSubscription;
tempSubscription = player1.playerStateStream.listen((event) {
if (event.processingState == audio.ProcessingState.completed) {
log('completed');
player1.seek(const Duration(seconds: 0));
player1.pause();
tempSubscription?.cancel();
}
});
subscription?.cancel();
_loopPlayer(player2, player1);
}
});
}

How to implement audio service with audioplayers flutter

Below code shows how i am using audioplayers without audio service
playFromFile(List textList, seek) async {
for (int i = seek; i < textList.length; i++) {
setState(() {
newSeek = i;
});
itemScrollController.scrollTo(
index: i,
duration: const Duration(seconds: 2),
curve: Curves.easeInOutCubic);
String text = textList[i].join(' ');
final bytes = await getAudio(text);
await audioPlugin.playBytes(bytes);
while (audioPlugin.state == PlayerState.PLAYING && !playFresh) {
await Future.delayed(const Duration(seconds: 1));
if (audioPlugin.state == PlayerState.PLAYING) {
audioPlugin.onPlayerCompletion.listen((onDone) async {
audioPlugin.state = PlayerState.COMPLETED;
await audioPlugin.release();
});
}
if (audioPlugin.state == PlayerState.COMPLETED) {
await audioPlugin.release();
break;
}
}
if (playFresh) break;
}
}
I want to implement this code using audio service so i can play audio in background . How to implement this with audio service so it will play in background. Please help as i am new to flutter and unable to solve this for days.
Solution will be quite simple.
You implement this using different layer.
UI Layer - This will be your UI from where you going to click the play button.
Intermediate layer or Audio service Layer(Audio service class) - From your UI layer you going to called this audio service class play method.
Final layer (Actual audio player methods) - This will be final layer where your actual audio package mehtods resides. You called this layer from your intermediate layer.
Summary - UI layer play button -> Audio service Play method -> Audio player Play method

How to add song to playlist during playback by using just_audio flutter

My app downloads audio files to device and has a audio player which plays that audio.
So I am trying to add song to my playlist while audio is playing. I am using flutter package just_audio: ^0.9.7 . I query all the songs from device from a specific folder and use these two function in init state
Future<List<AudioSource>> _getList(List<SongModel> something) async {
List<AudioSource> _list = [];
something.forEach((element) {
_list.add(
AudioSource.uri(
Uri.directory(element.data.toString()),
tag: MediaItem(
id: element.id.toString(),
title: element.title.toString(),
),
),
);
print(element.data);
});
return _list;
}
Future<void> settingAudioPlayer() async {
List<SongModel> something = [];
something =
await OnAudioQuery().querySongs(sortType: SongSortType.DATA_ADDED);
something
.removeWhere((element) => !element.data.contains('StrikleDustiness'));
//print('${something[0].data}');
var items = await _getList(something);
await MainScreen.player.setAudioSource(
ConcatenatingAudioSource(useLazyPreparation: true, children: items),
);
}
by doing this I am able to play song whenever I restart the app. But I want to add song to the playlist whenever I download new song while playing the existing song . I want the new download song to be my next queue.
Sorry for the bad english and also I am new to flutter.
Thanks..
EDIT: I figured it out. I just had to create a separate list containing all the video sources.

Microphone Timer in Android Flutter

Good morning guys, I'm using lib speech_to_text to be able to use the microphone and do voice recognition ... I put a Timer to be able to leave the microphone active for longer after the person speaks, but it only works on iOS, so I saw Android already has a native Timer .... does anyone know what I can do? Thank you!
#action
onPressedMic({bool continous = false}) {
this.initSpeech();
if (this.hasSpeech) {
try {
stt.listen(
localeId: LocaleUtils.getPtBR().localeId,
onSoundLevelChange: _sttOnSoundLevelChange,
onResult: _sttResultListener,
cancelOnError: true,
);
// displays the wave indicating the audio capture
status = StatusFooter.capturing_speech;
if (Platform.isIOS) _startTimerListen();
if (_startAudioCapture != null) _startAudioCapture();
} catch (e) {
status = StatusFooter.open_speech;
print("Pressed Mic error: $e");
}
}
}
#computed
bool get canShowSuggestions => (this.suggestionChips?.length ?? 0) > 0;
#action
_sttResultListener(SpeechRecognitionResult result) async {
// restart timer, if stt stop command has not been issued
if (Platform.isIOS && !result.finalResult) _startTimerListen();
this.textCaptureAudio = result.recognizedWords;
// sends the question, as the stt stop command has been issued
if (result.finalResult && this.textCaptureAudio.trim().isNotEmpty)
this.sendQuestion(this.textCaptureAudio);
}
void _startTimerListen() {
_cancelTimerListen();
timerListen = Timer(Duration(seconds: 3), () {
if (this.textCaptureAudio.trim().isNotEmpty) {
stt.stop();
} else {
_defineOpenSpeechStatus();
}
});
}
As far as I know there is really no way for an app to extend the period of time the microphone is listening on Android, not with Flutter and not with native Android development. I tried solving this problem for a own app a few years ago but the speech recognition on Android just does not support this. I am sorry but I hope I could help clarifying things.

Play ParseFile Video with Handheld.PlayFullScreenMovie Unity

I am a newbie in Unity3d and Parse.com.
I want to play online video that I store in Parse.com. However when I get an AbsoluteUri of parse file for play, It open video and just play for 1s then it auto close. What thing I did wrong ? see the code below
public void queryBundleWithName(){
var query = ParseObject.GetQuery ("AssetBundle").
WhereEqualTo("name", "Board");
query.FirstAsync().ContinueWith(t =>{
ParseObject relatedObjects = t.Result;
string name = relatedObjects.Get<string>("name");
//isFinish = true;
urlVideo = relatedObjects.Get<ParseFile>("video").Url.AbsoluteUri;
textResult.text = urlVideo;
Screen.orientation = ScreenOrientation.LandscapeLeft;
Handheld.PlayFullScreenMovie (urlVideo, Color.black, FullScreenMovieControlMode.CancelOnInput, FullScreenMovieScalingMode.AspectFill);
});
}
Thank for your help.

Categories

Resources