i'm using this code to play sound whenever I click on a widget.
Future<void> playMySound(String who) async{
AudioPlayer advancedPlayer = new AudioPlayer();
audioCache = new AudioCache(fixedPlayer: advancedPlayer);
audioCache.clearCache();
String localFilePath;
audioCache.play('roomSounds/$who', mode: PlayerMode.LOW_LATENCY);
}
Problem:
Whenever I tap for example 15 times on a widget that would play the sound, it would stop playing and throw the following stack:
E/MediaPlayer( 5793): Error (-38,0)
I/flutter ( 5793): hello 0.0
E/MediaPlayerNative( 5793): error (1, -19)
E/MediaPlayer( 5793): Error (1,-19)
E/MediaPlayerNative( 5793): pause called in state 0, mPlayer(0x77ceaa80)
E/MediaPlayerNative( 5793): error (-38, 0)
E/MediaPlayerNative( 5793): Attempt to perform seekTo in wrong state: mPlayer=0x77ceaa80, mCurrentState=0
E/MediaPlayerNative( 5793): error (-38, 0)
E/MediaPlayer( 5793): Error (-38,0)
E/MediaPlayer( 5793): Error (-38,0)
I've tried to use audioCache.clearCache() but with no effect. Only after reloading the application it is possible to play any sound at all.
Related
I'd like to create a list of List<Future<void>> that represent executions of sound effects.
I therefore iterate through a list of event types/formats (ie. "sword") and map each to a sound effect play execution.
I wrap these Futures in a Future.delayed for each with a random number of milliseconds from 30 to 100 just to have a little random occurance of when the sound effects are played in order (to add a pause between two sound effects).
The list of those sound effect Futures is then called by Future.wait where I expected the list to be resolved and the Future.delay might take effect.
This is my code
_playActionEventSounds() async {
if (widget.log.isNotEmpty) {
final lastRound = widget.log.map((e) => e.round).reduce(max);
final lastActionEvents = widget.log.where((actionEvent) => actionEvent.round == lastRound);
final List<Future<void>> eventSounds = lastActionEvents.map((actionEvent) async {
if (actionEvent.format == "sword") {
return Future.delayed(Duration(milliseconds: next(30, 100)), () => _soundPlayer.punch());
}
if (actionEvent.format == "damage") {
return Future.delayed(Duration(milliseconds: next(30, 100)), () => _soundPlayer.pain());
}
return Future.value(null);
}).toList();
await Future.wait(eventSounds);
}
}
SoundPlayer class:
import 'package:audioplayers/audioplayers.dart';
class SoundPlayer {
Future<void> punch() async {
final player = AudioPlayer();
player.setReleaseMode(ReleaseMode.stop);
return await player.play(AssetSource("sounds/punch.wav"));
}
Future<void> pain() async {
final player = AudioPlayer();
player.setReleaseMode(ReleaseMode.stop);
return await player.play(AssetSource("sounds/pain.wav"));
}
}
I now can run the process multiple times. I do hear the sounds appear as expected. But after the 4th execution the app crashes.
This is the error:
I/flutter (28346): Unexpected platform error: MediaPlayer error with what:MEDIA_ERROR_UNKNOWN {what:1} extra:MEDIA_ERROR_UNKNOWN {extra:-19}
E/MediaPlayerNative(28346): pause called in state 0, mPlayer(0xb400007a92646810)
E/MediaPlayerNative(28346): error (-38, 0)
E/MediaPlayerNative(28346): Attempt to call getDuration in wrong state: mPlayer=0xb400007a92646810, mCurrentState=0
E/MediaPlayerNative(28346): error (-38, 0)
E/MediaPlayerNative(28346): stop called in state 0, mPlayer(0xb400007a92646810)
E/MediaPlayerNative(28346): error (-38, 0)
E/MediaPlayerNative(28346): prepareAsync called in state 0, mPlayer(0xb400007a92646810)
D/AndroidRuntime(28346): Shutting down VM
E/AndroidRuntime(28346): FATAL EXCEPTION: main
E/AndroidRuntime(28346): Process: com.example.app, PID: 28346
E/AndroidRuntime(28346): java.lang.IllegalStateException
E/AndroidRuntime(28346): at android.media.MediaPlayer._prepare(Native Method)
E/AndroidRuntime(28346): at android.media.MediaPlayer.prepare(MediaPlayer.java:1313)
E/AndroidRuntime(28346): at xyz.luan.audioplayers.player.MediaPlayerPlayer.prepare(MediaPlayerPlayer.kt:89)
E/AndroidRuntime(28346): at xyz.luan.audioplayers.player.WrappedPlayer.stop(WrappedPlayer.kt:185)
E/AndroidRuntime(28346): at xyz.luan.audioplayers.player.WrappedPlayer.onCompletion(WrappedPlayer.kt:248)
E/AndroidRuntime(28346): at xyz.luan.audioplayers.player.MediaPlayerPlayer.createMediaPlayer$lambda-5$lambda-1(MediaPlayerPlayer.kt:17)
E/AndroidRuntime(28346): at xyz.luan.audioplayers.player.MediaPlayerPlayer.$r8$lambda$3fK1i48Yert5dbg2Q8ZiB5tiKHg(Unknown Source:0)
E/AndroidRuntime(28346): at xyz.luan.audioplayers.player.MediaPlayerPlayer$$ExternalSyntheticLambda1.onCompletion(Unknown Source:2)
E/AndroidRuntime(28346): at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:3559)
E/AndroidRuntime(28346): at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime(28346): at android.os.Looper.loopOnce(Looper.java:201)
E/AndroidRuntime(28346): at android.os.Looper.loop(Looper.java:288)
E/AndroidRuntime(28346): at android.app.ActivityThread.main(ActivityThread.java:7842)
E/AndroidRuntime(28346): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(28346): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
E/AndroidRuntime(28346): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
I/Process (28346): Sending signal. PID: 28346 SIG: 9
Lost connection to device.
I guess I somewhat messed up the resource management due the repeated execution. Therefore I guess the error lies somewhat in the API usage of audioplayers.dart?
I had to change the ReleaseMode to release. What I did was using stop that kept the resources in memory I guess and therefore cluttered the memory?
I am working on an app in flutter that uses the camera.
I have followed the basic flutter tutorial
https://flutter.dev/docs/cookbook/plugins/picture-using-camera
However, when I am on a screen using the CameraPreview widget, and I navigate to another app that uses the camera, like snapchat, I see the following error when opening snapchat (the error comes from my app), has anyone else seen this before?
Returning to my app shows the camera in a frozen/paused state.
I/CameraManagerGlobal(30470): Camera 0 facing CAMERA_FACING_BACK state now CAMERA_STATE_IDLE for client com.my.cliet API Level 2
I/CameraManagerGlobal(30470): Camera 0 facing CAMERA_FACING_BACK state now CAMERA_STATE_CLOSED for client com.my.cliet API Level 2
E/CameraCaptureSession(30470): Session 0: Exception while stopping repeating:
E/CameraCaptureSession(30470): android.hardware.camera2.CameraAccessException: CAMERA_ERROR (3): cancelRequest:547: Camera 0: Error clearing streaming request: Function not implemented (-38)
E/CameraCaptureSession(30470): at android.hardware.camera2.CameraManager.throwAsPublicException(CameraManager.java:1340)
E/CameraCaptureSession(30470): at android.hardware.camera2.impl.ICameraDeviceUserWrapper.cancelRequest(ICameraDeviceUserWrapper.java:99)
E/CameraCaptureSession(30470): at android.hardware.camera2.impl.CameraDeviceImpl.stopRepeating(CameraDeviceImpl.java:1259)
E/CameraCaptureSession(30470): at android.hardware.camera2.impl.CameraCaptureSessionImpl.close(CameraCaptureSessionImpl.java:578)
E/CameraCaptureSession(30470): at android.hardware.camera2.impl.CameraCaptureSessionImpl$2.onDisconnected(CameraCaptureSessionImpl.java:789)
E/CameraCaptureSession(30470): at android.hardware.camera2.impl.CameraDeviceImpl$7.run(CameraDeviceImpl.java:245)
E/CameraCaptureSession(30470): at android.os.Handler.handleCallback(Handler.java:938)
E/CameraCaptureSession(30470): at android.os.Handler.dispatchMessage(Handler.java:99)
E/CameraCaptureSession(30470): at android.os.Looper.loop(Looper.java:246)
E/CameraCaptureSession(30470): at android.app.ActivityThread.main(ActivityThread.java:8506)
E/CameraCaptureSession(30470): at java.lang.reflect.Method.invoke(Native Method)
E/CameraCaptureSession(30470): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
E/CameraCaptureSession(30470): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
E/CameraCaptureSession(30470): Caused by: android.os.ServiceSpecificException: cancelRequest:547: Camera 0: Error clearing streaming request: Function not implemented (-38) (code 10)
E/CameraCaptureSession(30470): at android.os.Parcel.createExceptionOrNull(Parcel.java:2399)
E/CameraCaptureSession(30470): at android.os.Parcel.createException(Parcel.java:2369)
E/CameraCaptureSession(30470): at android.os.Parcel.readException(Parcel.java:2352)
E/CameraCaptureSession(30470): at android.os.Parcel.readException(Parcel.java:2294)
E/CameraCaptureSession(30470): at android.hardware.camera2.ICameraDeviceUser$Stub$Proxy.cancelRequest(ICameraDeviceUser.java:750)
E/CameraCaptureSession(30470): at android.hardware.camera2.impl.ICameraDeviceUserWrapper.cancelRequest(ICameraDeviceUserWrapper.java:97)
E/CameraCaptureSession(30470): ... 11 more
So I have resolved the issue by adding the following which aren't included in the link that I posted.
in the initState() method I had to manually add the observer
WidgetsBinding.instance!.addObserver(this);
I had to define/modify the didChangeAppLifecycleState(...) function as follows
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
final CameraController? cameraController = _controller;
// App state changed before we got the chance to initialize.
if (cameraController == null || !cameraController.value.isInitialized) {
return;
}
if (state == AppLifecycleState.inactive) {
cameraController.dispose();
} else if (state == AppLifecycleState.resumed) {
this._controller = CameraController(
widget.camera,
ResolutionPreset.medium,
);
if (mounted) {
setState(() {
this._initializeControllerFuture = this._controller.initialize();
});
}
}
}
I am trying to use the audio_service package to allow background controls for my podcast playing app. The service does not start. When I call AudioService.start(), nothing is ever returned (I'm awaiting this function and trying to print a statement after this returns, but it never returns. Also, when I first attempt to play an item, it will correctly evaluate AudioService.running to false and then try to start the AudioService (hanging on the start() function). When I hit play a second time, it will evaluate AudioService.running as true (even though the start function never returned) and will then throw this error:
E/flutter ( 8726): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: PlatformException(Attempt to invoke virtual method 'void io.flutter.plugin.common.MethodChannel.invokeMethod(java.lang.String, java.lang.Object, io.flutter.plugin.common.MethodChannel$Result)' on a null object reference, null, null)
E/flutter ( 8726): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:572:7)
E/flutter ( 8726): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:161:18)
E/flutter ( 8726): <asynchronous suspension>
E/flutter ( 8726): #2 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:334:12)
E/flutter ( 8726): #3 AudioService.play (package:audio_service/audio_service.dart:895:20)
Here is my code which calls the start function:
if(AudioService.running) {
print('playing audio service: ${AudioService.running}');
AudioService.play();
} else {
MediaItem mediaItem = MediaItem(
id: newPostPod.audioUrl,
title: newPostPod.titleTextString(),
artist: newPostPod.subtitleTextString(),
);
print('starting audio service');
bool started = await AudioService.start(
backgroundTaskEntrypoint: _backgroundTaskEntrypoint,
params: {'mediaItem': mediaItem.toJson()},
);
print('audio service started');
if(AudioService.running) {
print('starting to play via media service');
AudioService.playMediaItem(mediaItem);
} else {
print('Audio Service not yet started: $started');
}
}
The code for the background task entrypoint (which is outside of any other class, as it wasn't working inside a class I had read in another issue that it had to be top-level):
_backgroundTaskEntrypoint() {
AudioServiceBackground.run(() => PlayerTask());
}
Lastly, it doesn't appear any of the code within my onStart in my BackgroundAudioTask is running (as print statements at the top of my onStart are not being run). So I'm not sure what's happening here. It isn't give me a clear error or really any hint of where to start debugging audio_service.
Calling AudioService.connect(); before starting the service will solve the error.
For me AudioService.start() would freeze until I added the WAKE_LOCK and FOREGROUND_SERVICE permissions to the AndroidManifest.xml file per the audio_service README file.
I'm currently using the flutter camera package to record and save videos, but I've noticed that the camera preview runs into an exception when the device's camera is opened on another app or when face unlock is used on the lock screen.
I've tried using didChangeAppLifecycleState to possibly fetch or reinitialize the camera, but there's no success yet.
await model.fetchCameras();
if (model.cameras.isNotEmpty) {
await model.onNewCameraSelected(model.cameras[model.cameraIndex], true);
}
}
This issue is currently opened here and here, but haven't been resolved.
I get this exception when the device's camera is opened in another app. I tried disposing the camera and reinitializing it in the sample code above, but it doesn't work.
E/CameraCaptureSession(31468): android.hardware.camera2.CameraAccessException: CAMERA_DISCONNECTED (2): cancelRequest:458: Camera device no longer alive
E/CameraCaptureSession(31468): at android.hardware.camera2.CameraManager.throwAsPublicException(CameraManager.java:814)
E/CameraCaptureSession(31468): at android.hardware.camera2.impl.ICameraDeviceUserWrapper.cancelRequest(ICameraDeviceUserWrapper.java:95)
E/CameraCaptureSession(31468): at android.hardware.camera2.impl.CameraDeviceImpl.stopRepeating(CameraDeviceImpl.java:1134)
E/CameraCaptureSession(31468): at android.hardware.camera2.impl.CameraCaptureSessionImpl.close(CameraCaptureSessionImpl.java:526)
E/CameraCaptureSession(31468): at android.hardware.camera2.impl.CameraCaptureSessionImpl$2.onDisconnected(CameraCaptureSessionImpl.java:737)
E/CameraCaptureSession(31468): at android.hardware.camera2.impl.CameraDeviceImpl$7.run(CameraDeviceImpl.java:242)
E/CameraCaptureSession(31468): at android.os.Handler.handleCallback(Handler.java:873)
E/CameraCaptureSession(31468): at android.os.Handler.dispatchMessage(Handler.java:99)
E/CameraCaptureSession(31468): at android.os.Looper.loop(Looper.java:214)
E/CameraCaptureSession(31468): at android.app.ActivityThread.main(ActivityThread.java:6981)
E/CameraCaptureSession(31468): at java.lang.reflect.Method.invoke(Native Method)
E/CameraCaptureSession(31468): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
E/CameraCaptureSession(31468): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
E/CameraCaptureSession(31468): Caused by: android.os.ServiceSpecificException: cancelRequest:458: Camera device no longer alive (code 4)
E/CameraCaptureSession(31468): at android.os.Parcel.createException(Parcel.java:1980)
E/CameraCaptureSession(31468): at android.os.Parcel.readException(Parcel.java:1934)
E/CameraCaptureSession(31468): at android.os.Parcel.readException(Parcel.java:1884)
E/CameraCaptureSession(31468): at android.hardware.camera2.ICameraDeviceUser$Stub$Proxy.cancelRequest(ICameraDeviceUser.java:402)
E/CameraCaptureSession(31468): at android.hardware.camera2.impl.ICameraDeviceUserWrapper.cancelRe
Any Ideas, on how to force the camera package to reinitialize?
On resume re initialise Camera
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
_controller != null
? _initializeControllerFuture = _controller.initialize()
: null; //on pause camera is disposed, so we need to call again "issue is only for android"
}
}
checkout https://medium.com/#navinkumar0118/take-a-picture-using-flutter-camera-a9c11d282632
How to catch error on flutter video_player plugin?
the error is Source error. when this error occurred I want to display error message.
I/ExoPlayerImpl(31473): Init 98a4284 [ExoPlayerLib/2.9.6] [kenzo, Redmi Note 3, Xiaomi, 23]
E/ExoPlayerImplInternal(31473): Source error.
E/ExoPlayerImplInternal(31473): com.google.android.exoplayer2.source.UnrecognizedInputFormatException: None of the available extractors (MatroskaExtractor, FragmentedMp4Extractor, Mp4Extractor, Mp3Extractor, AdtsExtractor, Ac3Extractor, TsExtractor, FlvExtractor, OggExtractor, PsExtractor, WavExtractor, AmrExtractor) could read the stream.
E/ExoPlayerImplInternal(31473): at com.google.android.exoplayer2.source.ExtractorMediaPeriod$ExtractorHolder.selectExtractor(ExtractorMediaPeriod.java:973)
E/ExoPlayerImplInternal(31473): at com.google.android.exoplayer2.source.ExtractorMediaPeriod$ExtractingLoadable.load(ExtractorMediaPeriod.java:891)
E/ExoPlayerImplInternal(31473): at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:381)
E/ExoPlayerImplInternal(31473): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
E/ExoPlayerImplInternal(31473): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
E/ExoPlayerImplInternal(31473): at java.lang.Thread.run(Thread.java:818)
I/flutter (31473): Another exception was thrown: A ChewieController was used after being disposed.
I/ExoPlayerImpl(31473): Release 98a4284 [ExoPlayerLib/2.9.6] [kenzo, Redmi Note 3, Xiaomi, 23] [goog.exo.core]
my code
animeVidBloc.vidCtrl = VideoPlayerController.network(streamurl.src)
..initialize().then((_) {
animeVidBloc.vidCtrl.pause();
animeVidBloc.cheCtrl = ChewieController(
videoPlayerController: animeVidBloc.vidCtrl,
aspectRatio: animeVidBloc.vidCtrl.value.aspectRatio,
autoPlay: false,
looping: false,
allowedScreenSleep: false,
);
animeVidBloc.isLoadingData = false;
animeVidBloc.isLoadingVideo = false;
animeVidBloc.cheCtrl.pause();
animeVidBloc.rebuildWidgets(ids: ['body', 'download']);
});
using .catch after initialize().then doesn't catch the error,
example video https://str09.vidoza.net/x4lfoiywwbzpvjumxbaeidisvq6cmoufczvmicnesasm4zqpyunkofpekfla/v.mp4
You can attach listener to the VidePlayerController object to catch states of the controller.
VideoPlayerController _controller = VideoPlayerController.network(
"https://str09.vidoza.net/x4lfoiywwbzpvjumxbaeidisvq6cmoufczvmicnesasm4zqpyunkofpekfla/v.mp4")
..initialize();
_controller.addListener(() {
if (_controller.value.hasError) {
print(_controller.value.errorDescription);
}
if (_controller.value.initialized) {}
if (_controller.value.isBuffering) {}
});