Is there a way to eliminate the error when clicking the button fast - flutter

I have this code below which plays a sound every time I hit a button.
AudioPlayer player = AudioPlayer();
Future<void> playSound({required String soundStr}) async {
player.setAsset(soundStr);
player.play();
}
But whenever I hit the button fast, sometimes it will show this error. How can I eliminate this error? I tried adding player.stop(); but it still the same.
E/flutter ( 4312): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(abort, Loading interrupted, null, null)
E/flutter ( 4312): #0 AudioPlayer._setPlatformActive.checkInterruption (package:just_audio/just_audio.dart:1236:7)
E/flutter ( 4312): #1 AudioPlayer._setPlatformActive.setPlatform (package:just_audio/just_audio.dart:1347:11)
E/flutter ( 4312): <asynchronous suspension>
E/flutter ( 4312):
E/flutter ( 4312): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(abort, Loading interrupted, null, null)
E/flutter ( 4312):
Edit: I tried adding await.
Future<void> playSound({required String soundStr}) async {
await player.setAsset(soundStr);
await player.play();
}
If I hit the buttons fast, it will still have error. Though it is now different.
E/flutter (21654): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Connection aborted
E/flutter (21654): #0 AudioPlayer._load (package:just_audio/just_audio.dart:843:11)
E/flutter (21654): <asynchronous suspension>
E/flutter (21654): #1 AudioPlayer.load (package:just_audio/just_audio.dart:770:14)
E/flutter (21654): <asynchronous suspension>
E/flutter (21654): #2 AudioPlayer.setAudioSource (package:just_audio/just_audio.dart:745:18)
E/flutter (21654): <asynchronous suspension>
E/flutter (21654): #3 playSound (package:super_pinoy_quiz/common/generic_methods.dart:396:3)
E/flutter (21654): <asynchronous suspension>
E/flutter (21654):
I'm using this package: just_audio: ^0.9.29
Though I tried using different package but it also encounters the same error when hitting the button fast: audioplayers: ^1.1.1

Both setAsset and play are future method, you can await for setting assets
Future<void> playSound({required String soundStr}) async {
await player.setAsset(soundStr);
await player.play();
}

Related

Flutter Just_audio, Error No implementation found for method disposeAllPlayers

I am using just_audio package to play my base64 string which is a wav file that I received from my server. The app is being played from an android device API 30-ish. This is the error once the base64 is loaded:
E/flutter (31770): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: MissingPluginException(No implementation found for method disposeAllPlayers on channel com.ryanheise.just_audio.methods)
E/flutter (31770): #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:165:7)
E/flutter (31770): <asynchronous suspension>
E/flutter (31770): #1 MethodChannelJustAudio.disposeAllPlayers (package:just_audio_platform_interface/method_channel_just_audio.dart:29:10)
E/flutter (31770): <asynchronous suspension>
E/flutter (31770):
E/flutter (31770): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: MissingPluginException(No implementation found for method init on channel com.ryanheise.just_audio.methods)
E/flutter (31770): #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:165:7)
E/flutter (31770): <asynchronous suspension>
E/flutter (31770): #1 MethodChannelJustAudio.init (package:just_audio_platform_interface/method_channel_just_audio.dart:13:5)
E/flutter (31770): <asynchronous suspension>
E/flutter (31770): #2 AudioPlayer._setPlatformActive.setPlatform (package:just_audio/just_audio.dart:1330:13)
E/flutter (31770): <asynchronous suspension>
E/flutter (31770):
My custom base64 player which I follow from the instruction of the page:
class MyAudioPlayer extends StreamAudioSource {
MyAudioPlayer({this.bytes});
List<int> bytes;
#override
Future<StreamAudioResponse> request([int start, int end]) async {
final start = 0;
final end = bytes.length;
return StreamAudioResponse(
sourceLength: bytes.length,
contentLength: end - start,
offset: start,
stream: Stream.value(bytes.sublist(start, end)),
contentType: "audio/x-wav",
);
}
}
The file in which I played:
void playTTS(String content) async{
Dialogs.showLoadingDialog(context: context);
final List<int> base64 = await ApiService.textToSpeech(content);
final player = AudioPlayer();
await player.setAudioSource(MyAudioPlayer(bytes: base64));
await player.play();
Navigator.of(context, rootNavigator: true).pop();
listString.value = List.from(listString.value..add(ValueNotifier(content)));
}
I have set already set this to my androidmanifest.xml to true already.
android:usesCleartextTraffic="true"
Any help is appreciated.
Seems like I don't have to convert the base64 Wav file with a custom class like the tutorial did. According to the online converter, all I have to do was put data:audio/wav;base64,$base64 before the actual base64 string and uses the normal setUrl and it worked:
final String code = "data:audio/wav;base64," + json.decode(response.body)["base64"];
final player = AudioPlayer();
player.setUrl(code);
await player.play();

ERROR:flutter/lib/ui/ui_dart_state.cc(209) Unhandled Exception: PlatformException

I have my flutter App integrated with firebase, everything was fine but after i integrate firebase i got error whenever I try to run my app.
My main method looks like this:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(child: MyApp());
}
Error:
E/flutter ( 7922): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception:
PlatformException(firebase_core, FirebaseApp name [DEFAULT] already exists!, null, null)
E/flutter ( 7922): #0 StandardMethodCodec.decodeEnvelope
(package:flutter/src/services/message_codecs.dart:607:7)
E/flutter ( 7922): #1 MethodChannel._invokeMethod
(package:flutter/src/services/platform_channel.dart:177:18)
E/flutter ( 7922): <asynchronous suspension>
E/flutter ( 7922): #2 MethodChannel.invokeMapMethod
(package:flutter/src/services/platform_channel.dart:377:43)
E/flutter ( 7922): <asynchronous suspension>
E/flutter ( 7922): #3 MethodChannelFirebase.initializeApp (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:100:40)
E/flutter ( 7922): <asynchronous suspension>
E/flutter ( 7922): #4 Firebase.initializeApp
(package:firebase_core/src/firebase.dart:40:31)
E/flutter ( 7922): <asynchronous suspension>
E/flutter ( 7922): #5 main (package:zunada/main.dart:11:3)
E/flutter ( 7922): <asynchronous suspension>
E/flutter ( 7922):
I had similar issue when create firebase app by flavors using flutterfire CLI. I realized that flutterfire create apps with same name in different projects. Then I tried 2 ways and it works.
1st: manually change app name in each firebase project.
2nd: add param name when initialize such as in main_dev.dart
Example:
await Firebase.initializeApp(
name: "dev-project",
options: DefaultFirebaseOptions.currentPlatform);
I used the same name used in the firebase console as my project ID for consistency, keep in mind that you cannot add spaces to the name (only dashes -).

How do i resolve this issue with polyline not showing up on the map?

hoping someone is able to help me with this error i keep getting. I am using async - await method to call the function "drawPolylinefrompickuptodestination", but i keep getting this error..
[ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: 0
E/flutter ( 7629): #0 List.[] (dart:core-patch/growable_array.dart:264:36)
E/flutter ( 7629): #1 AssistantMethods.pickupToDestinationDirections (package:parcel_you_driver_app/assistant/assistant_methods.dart:52:70)
E/flutter ( 7629): <asynchronous suspension>
E/flutter ( 7629): #2 _NewTripScreenState.drawPolylineFromPickupToDestination (package:parcel_you_driver_app/mainScreens/new_trip_screen.dart:395:33)
E/flutter ( 7629): <asynchronous suspension>
E/flutter ( 7629): #3 _NewTripScreenState.build.<anonymous closure> (package:parcel_you_driver_app/mainScreens/new_trip_screen.dart:252:29)
E/flutter ( 7629): <asynchronous suspension>

Unhandled Exception: [firebase_functions/not-found] NOT_FOUND -Flutter

I am trying to implement the twilio_voice: ^0.0.9 plug in and register my firebase app at the same time but, when I run my registration function I get the error:
E/flutter (27321): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: [firebase_functions/not-found] NOT_FOUND
E/flutter (27321):
E/flutter (27321): #0 StandardMethodCodec.decodeEnvelope
package:flutter/…/services/message_codecs.dart:607
E/flutter (27321): #1 MethodChannel._invokeMethod
package:flutter/…/services/platform_channel.dart:156
E/flutter (27321): <asynchronous suspension>
E/flutter (27321): #2 MethodChannelHttpsCallable.call
package:cloud_functions_platform_interface/…/method_channel/method_channel_https_callable.dart:23
E/flutter (27321): <asynchronous suspension>
E/flutter (27321): #3 HttpsCallable.call
package:cloud_functions/src/https_callable.dart:35
E/flutter (27321): <asynchronous suspension>
E/flutter (27321): #4 _TextScreenState.register
package:buddiesDrivers/CallScreen/TextScreen.dart:72
E/flutter (27321): <asynchronous suspension>
E/flutter (27321):
E/flutter (27321): #0 MethodChannelHttpsCallable.call
package:cloud_functions_platform_interface/…/method_channel/method_channel_https_callable.dart:39
E/flutter (27321): <asynchronous suspension>
E/flutter (27321): #1 HttpsCallable.call
package:cloud_functions/src/https_callable.dart:35
E/flutter (27321): <asynchronous suspension>
I am have firebase implemented and running and I am getting my token when logging in. The app seems to fail when calling the httpsCallable("voice-accessToken") in the app. Here is the function:
register() async {
print("voip-registtering with token ");
print("voip-calling voice-accessToken");
final function =
FirebaseFunctions.instance.httpsCallable("voice-accessToken");
final data = {
"platform": Platform.isIOS ? "iOS" : "Android",
};
final result = await function.call(data);
print("voip-result");
print(result.data);
String androidToken;
if (Platform.isAndroid) {
androidToken = await FirebaseMessaging.instance.getToken();
print("androidToken is " + androidToken);
}
TwilioVoice.instance
.setTokens(accessToken: result.data, deviceToken: androidToken);
}
Any help in the right direction would be appreciated

OpenCV in Flutter

I am working on a project using OpenCV. I already created a python "prototype". Now I'd like to move it to Flutter. This is where I encountered a few problems.
I imported the https://pub.dev/packages/opencv flutter package.
import "package:opencv/opencv.dart";
import 'package:opencv/core/core.dart';
import 'dart:io';
import 'dart:async';
Future<void> main() async {
var url = "/sdcard/Download/test5.png";
var myFile = await File(url).readAsBytes();
print(myFile);
print(await ImgProc.medianBlur(myFile, 5));
}
After I run this code I get this error:
E/flutter (10044): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value
E/flutter (10044): #0 MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:142:86)
E/flutter (10044): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:148:36)
E/flutter (10044): #2 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:331:12)
E/flutter (10044): #3 ImgProc.medianBlur (package:opencv/core/imgproc.dart:539:43)
E/flutter (10044): #4 main (package:projectgama/opencvTest.dart:19:23)
E/flutter (10044): <asynchronous suspension>
E/flutter (10044):
Does anybody know what I'm doing wrong? Did anybody manage to work with OpenCV in Flutter?