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
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'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.
I've changed some code and compiled the latest parse SDK for .net plugin for unity. Now I want to configure unity client app to receive a push notification (especially android). I don't know how to configure it, I've just found some code to receive or show the notification not how to configure it like all permission or where to put cloud message key. If someone could help me? thanks
yes, of course. I've found something strange, i have two messages receiver firebase and parse, firebase receiver work when app is in foreground `
//Events subscription
private void Awake()
{
instance = this;
Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
ParsePush.ParsePushNotificationReceived += OnMessageReceiveByParse;
Firebase.Messaging.FirebaseMessaging.MessageReceived +=
OnMessageReceiveByFirebase;
}
// method call when firebase message received
void OnMessageReceiveByFirebase(object
sender,Firebase.Messaging.MessageReceivedEventArgs args)
{
Debug.Log("Message from firebase : " + args.Message);
}
// method call when parse message received
void OnMessageReceiveByParse(object sender, ParsePushNotificationEventArgs args)
{
Debug.Log("Message from Parse Server : " + args.Payload);
}
// parse guide receiver implementation
void cool(object sender, ParsePushNotificationEventArgs args)
{
#if UNITY_ANDROID
AndroidJavaClass parseUnityHelper = new AndroidJavaClass("com.parse.ParsePushUnityHelper");
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
// Call default behavior.
parseUnityHelper.CallStatic("handleParsePushNotificationReceived", currentActivity, args.StringPayload);
#endif
}
`
I use android studio to debug when app is launched, an error is always shown, i use classes parse sdk not compiled dll: this classe doesn't exist in my sdk:
(Filename: ./Runtime/Export/Debug.bindings.h Line: 45)
2019-07-12 09:12:33.968 14620-14663/? E/Unity: AndroidJavaException: java.lang.ClassNotFoundException: com.parse.ParsePushUnityHelper
java.lang.ClassNotFoundException: com.parse.ParsePushUnityHelper
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:453)
at java.lang.Class.forName(Class.java:378)
at com.unity3d.player.UnityPlayer.nativePause(Native Method)
at com.unity3d.player.UnityPlayer.l(Unknown Source:0)
at com.unity3d.player.UnityPlayer$22.run(Unknown Source:2)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at com.unity3d.player.UnityPlayer$e.run(Unknown Source:32)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.parse.ParsePushUnityHelper" on path: DexPathList[[zip file "/data/app/com.wmg.mascothunt-bUi8a5Uz_-d12IhMoYJuOQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.wmg.mascothunt-bUi8a5Uz_-d12IhMoYJuOQ==/lib/arm, /data/app/com.wmg.mascothunt-bUi8a5Uz_-d12IhMoYJuOQ==/base.a
This is my code set zoom of camera:
// 10 Set zoom value to camera
if (p.isZoomSupported() && p.isSmoothZoomSupported()) {
// most phones
mCamera.startSmoothZoom(zoomvalue);
} else if (p.isZoomSupported() && !p.isSmoothZoomSupported()) {
p.setZoom(zoomvalue);
mCamera.setParameters(p);
mCamera.setParameters(p);
mCamera.startPreview();
}
But it occur exception on Canvas Spark (Q380) android 5.0:
java.lang.RuntimeException: start smooth zoom failed
at android.hardware.Camera.startSmoothZoom(Native Method)
jat android.app.Activity.dispatchTouchEvent(Activity.java:2775)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2326)
at android.view.View.dispatchPointerEvent(View.java:8687)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4485)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4343)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3884)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3937)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3903)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4013)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3911)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4070)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3884)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3937)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3903)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3911)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3884)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6300)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6256)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6209)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6462)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6421)
at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6493)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:795)
at android.view.Choreographer.doCallbacks(Choreographer.java:598)
at android.view.Choreographer.doFrame(Choreographer.java:565)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:781)
at android.os.Handler.handleCallback(Handler.java:810)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:189)
at android.app.ActivityThread.main(ActivityThread.java:5529)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
Why android 5.0 occur error java.lang.RuntimeException: start smooth zoom failed?