Skip to content
This repository was archived by the owner on Oct 6, 2022. It is now read-only.

Android - Fatal Exception: java.lang.RuntimeException: Unable to create service com.tanguyantoine.react.MusicControlNotification$NotificationService: java.util.ConcurrentModificationException #247

Open
1 of 3 tasks
The0racle opened this issue Apr 6, 2019 · 24 comments · Fixed by #254
Assignees

Comments

@The0racle
Copy link

Description

Getting this crash after updating to v0.99. It seems related to #221 which #220 supposedly fixed it. Here is the Stack trace:
image

Looking at actions user took before the crash, makes me think the controls were not actually removed when app last terminated(or device locked), since app crashes right away when trying to play a new audio. Also, it happens only on OS 8.1.0+ and on Samsung devices.

I'm calling MusicControl.stopControl() when the specific component handling it is closed, but when app is terminated componentWillUnmount is not called. Not sure if that's related.

  1. Platform ?

    • iOS
    • [x ] Android
  2. Device

  • Simulator
  • Real device
@tanguyantoine
Copy link
Owner

Can you share some code?

@The0racle
Copy link
Author

@tanguyantoine here is the component that handles the controls:

export default class LockScreenControls extends Component<Props> {
  static defaultProps = {
    currentFactTitle: null,
    currentItemTitle: '',
    imagePath: '',
    track: null,
    playing: false,
    currentTime: 0,
    duration: 0,
    audioLoading: false,
  }

  componentDidMount() {
    const controls = this._remoteControls(this.props.track !== null);
    controls.forEach(element => MusicControl.enableControl(element, true));
    this._registerForRemoteControlEvents();

    MusicControl.setNowPlaying({
      title: this.props.currentItemTitle,
      artwork: this.props.imagePath,
      album: this.props.track,
      artist: this.props.currentFactTitle,
      duration: this.props.duration,
    });
  }

  componentWillUnmount() {
    MusicControl.stopControl();
  }

  componentDidUpdate(prevProps) {
    if (prevProps.playing !== this.props.playing) {
      const state = this.props.playing ? MusicControl.STATE_PLAYING : MusicControl.STATE_PAUSED;
      MusicControl.updatePlayback({
        state,
        elapsedTime: this.props.currentTime,
      });
    }

    if ((prevProps.currentItemTitle !== this.props.currentItemTitle) || (prevProps.currentFactTitle !== this.props.currentFactTitle)) {
      MusicControl.setNowPlaying({
        title: this.props.currentItemTitle,
        artwork: this.props.imagePath,
        artist: this.props.currentFactTitle,
      });
    }
  }

  shouldComponentUpdate(nextProps) {
    return (nextProps.playing !== this.props.playing) || (nextProps.currentItemTitle !== this.props.currentItemTitle) || (nextProps.currentFactTitle !== this.props.currentFactTitle);
  }

  _remoteControls = (hasTrack) => {
    const controls = ['play', 'pause', 'skipForward', 'skipBackward'];
    if (hasTrack) {
      return [
        ...controls,
        'nextTrack',
        'previousTrack',
      ];
    }

    return controls;
  }

  _registerForRemoteControlEvents() {
    MusicControl.enableBackgroundMode(true);
    MusicControl.on('play', this.props.onPressPlay);
    MusicControl.on('pause', this.props.onPressPause);
    MusicControl.on('skipForward', () => this.props.onPressSkipForward(skipTime));
    MusicControl.on('skipBackward', () => this.props.onPressSkipBackward(skipTime));
    MusicControl.on('nextTrack', this.props.onPressNextTrack);
    MusicControl.on('previousTrack', this.props.onPressPreviousTrack);
  }

  render() {
    return null;
  }
}

@The0racle
Copy link
Author

Just to give more context, our app supports background audio.

@tanguyantoine
Copy link
Owner

if componentWillUnmount is not called then it's an issue with React not this library.

can you trye to render something else than null ?

@The0racle
Copy link
Author

The0racle commented Apr 9, 2019

@tanguyantoine componentWillUnmount is never called when the app is terminated but that is just a shortcoming on React's side, not a bug. However, the native side does know when the application is terminated and I believe it's the responsibility of the native library to stop any running tasks related to it; otherwise, killing the app would always cause bugs with react-native-music-control.

Also, I could try to render something else but returning null is acceptable per RN docs.

@jayeshanandani
Copy link

I am getting same crash in reports in play store.

@cinder92
Copy link
Contributor

im getting same issue but only with Android 9

@tanguyantoine
Copy link
Owner

sorry I have absolutely no knowledge on Android 🤷‍♂️

@roffelund
Copy link
Contributor

roffelund commented Apr 26, 2019

@The0racle What do you think about this. No android expert so last time i tried to fix this bug i thought onTaskRemoved was triggered more often then it seems to be. I think however onDestroy() might be the key. #254

@alexkander
Copy link

Hi, guys.

I had the same issue and I resolved it adding the permission <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> in the AndroidManifest.xml.

Regards.

@roffelund
Copy link
Contributor

@arondn2 Should be added to readme.md #263

@alexkander
Copy link

@roffelund I'm on it

@The0racle
Copy link
Author

@arondn2 I have that permission already. App still crashes.

@alexkander
Copy link

@The0racle Do you have the logs?

@ouabing
Copy link

ouabing commented May 31, 2019

Still facing this issue after adding permission to AndroidManifest.xml.

Logs below:

java.util.ConcurrentModificationException: null
    at java.util.ArrayList$Itr.next(ArrayList.java:860)
    at android.support.v4.app.NotificationCompatBuilder.<init>
    at android.support.v4.app.NotificationCompat$Builder.build
    at com.tanguyantoine.react.MusicControlNotification.prepareNotification
    at com.tanguyantoine.react.MusicControlNotification$NotificationService.onCreate
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3535)
    at android.app.ActivityThread.-wrap4
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1817)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6758)
    at java.lang.reflect.Method.invoke(Method.java)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)
java.lang.RuntimeException: Unable to create service com.tanguyantoine.react.MusicControlNotification$NotificationService: java.util.ConcurrentModificationException
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3545)
    at android.app.ActivityThread.-wrap4
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1817)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6758)
    at java.lang.reflect.Method.invoke(Method.java)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)

@alexkander
Copy link

alexkander commented May 31, 2019

@ouabing I am using android 8.1. Maybe you can try cleaning the android project and rebuilding. You can try with react-native link or specific react-native link react-native-music-control to be sure that everything is ok. Sorry for being of little help

@ouabing
Copy link

ouabing commented May 31, 2019

Thanks, @arondn2 . Our app has already released several build versions, the release script will clean the build cache before assembleRelease so I think cleaning & rebuilding won't fix this issue (at least for me).

Correct my previous comment, I checked our error tracker again, Android 8.0, 8.1, 9.0 devices have this issue.

@The0racle
Copy link
Author

@ouabing @tanguyantoine @arondn2 could it be that the target SDK version has to be set to 28? Our project is currently set to 26 in build.gradle

image

@garfiaslopez
Copy link

+1 @The0racle how did you solve this?

@alexkander
Copy link

@The0racle We are using targetSdkVersion 28

@The0racle
Copy link
Author

@garfiaslopez unfortunately I haven't found a way to fix it yet. Changing to targetSdkVersion 28 didn't fix it either.

@mordonez-me
Copy link

Just for the record, this happens when I enable handleAudioInterruptions on Android.

@bradfloodx
Copy link
Collaborator

Reopening. I will investigate.

@bradfloodx bradfloodx reopened this May 28, 2020
@bradfloodx bradfloodx self-assigned this May 28, 2020
@krunalbad
Copy link
Contributor

Screenshot 2021-06-03 at 12 30 13 PM
Does this make sense to anyone having advance JAVA knowledge? I am not sure if this might causing you an issue. You may look into this. It might help.

Path: /node_modules/react-native/ReactAndroid/src/main/java/com/facebook/react/MemoryPressureRouter.java
RN: 0.63.2

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.