react-native-track-player icon indicating copy to clipboard operation
react-native-track-player copied to clipboard

Crash on Android

Open mohity777 opened this issue 3 years ago • 22 comments

Describe the bug My android app is crashing but it is working fine on IOS. it crashes 1 time in every 3 -4 attempts not every time -

Exception in native call- java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.Handler.post(java.lang.Runnable)' on a null object reference

Image ->> https://lh3.googleusercontent.com/-xaRP4BB5EHk/YM-PoYdmWLI/AAAAAAAABvg/t0LIUg2bcsEl8KC4IfRjNqIkBY077OjAQCJEEGAsYHg/s0/2021-06-20.jpg?authuser=0

To Reproduce Steps to reproduce the behavior:

Environment (please complete the following information): Run react-native info in your project and share the content.

System: OS: Windows 10 10.0.19041 CPU: (8) x64 Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz Memory: 490.54 MB / 7.78 GB Binaries: Node: 12.18.1 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.4 - ~\AppData\Roaming\npm\yarn.CMD npm: 6.14.5 - C:\Program Files\nodejs\npm.CMD Watchman: Not Found SDKs: Android SDK: API Levels: 27, 28, 29, 30 Build Tools: 28.0.3, 29.0.2, 29.0.3, 30.0.0, 30.0.1, 30.0.2 System Images: android-22 | Intel x86 Atom_64, android-28 | Intel x86 Atom_64, android-28 | Google Play Intel x86 Atom, android-29 | Google Play Intel x86 Atom Android NDK: 22.1.7171670 Windows SDK: Not Found IDEs: Android Studio: Version 4.2.0.0 AI-202.7660.26.42.7322048 Visual Studio: Not Found Languages: Java: 1.8.0_211 Python: 3.8.3 npmPackages: @react-native-community/cli: Not Found react: 16.13.1 => 16.13.1 react-native: 0.63.4 => 0.63.4 react-native-windows: Not Found npmGlobalPackages: react-native: Not Found

What react-native-track-player version are you using? "react-native-track-player": "^1.2.7",

Are you testing on a real device or in the simulator? Which OS version are you running?

On real device

Code Please, share the code that is causing the issue

const setupPlayer = async () => {
    await TrackPlayer.setupPlayer();
    await TrackPlayer.updateOptions({
        stopWithApp: true,
        capabilities: [
            TrackPlayer.CAPABILITY_PLAY,
            TrackPlayer.CAPABILITY_PAUSE,
            TrackPlayer.CAPABILITY_SKIP_TO_NEXT,
            TrackPlayer.CAPABILITY_SKIP_TO_PREVIOUS,
            TrackPlayer.CAPABILITY_STOP,
        ],
        compactCapabilities: [
            TrackPlayer.CAPABILITY_PLAY,
            TrackPlayer.CAPABILITY_PAUSE,
        ],
        notificationCapabilities: [
            TrackPlayer.CAPABILITY_PLAY,
            TrackPlayer.CAPABILITY_PAUSE,
            TrackPlayer.CAPABILITY_SKIP_TO_NEXT,
            TrackPlayer.CAPABILITY_SKIP_TO_PREVIOUS,
            TrackPlayer.CAPABILITY_STOP,
        ],
    });
};

//***** For Generating playlist of audios

export const generatePlaylistFromTimeline = (startIndex) => {
    return async (dispatch, getState) => {
        await setupPlayer();
        const timelineState = getState().timeline.timeline;
        let newsList = [...timelineState.feed];
        const startIndexId = newsList[startIndex] ? newsList[startIndex].id : 0 ;
        console.log("!!!!",startIndexId);
        newsList = newsList.filter(item => item.type === 'news');
        newsList = newsList.map(item => item.id);

        const userState = getState().user;
        const newsInterests = userState.newsInterests || [];
        const identifier = newsInterests.join(',');

        let lastId = newsList[newsList.length - 1];
        let lastTime = getState().news.byIds[lastId].createdAt;
        console.log('Generating LastTime: ', lastTime);
        lastTime = new Date(lastTime).getTime();
        console.log('Generating LastTime: ', lastTime);
        console.log(newsList);
        const newStartIndex = newsList.indexOf(startIndexId);
        console.log(newStartIndex);
        const payload = {
            tracks: newsList,
            currentTrack: newsList[newStartIndex],
            identifier: identifier,
            lastTime: lastTime,
            paging: timelineState.paging,
        };
        dispatch(savePlaylist(payload));
        await setTracks(newsList, newStartIndex, getState);
    };
};

// *** Start playing the selected track

const setTracks = async (trackList, startIndex, getState) => {
    const newsByIds = getState().news.byIds;
    await TrackPlayer.reset();
    const tracks = trackList.map(track => {
        const newsItem = newsByIds[track];
        return {
            id: track,
            url: newsItem.audioUrl,
            title: newsItem.headline,
            artist: newsItem.categories[0],
            artwork: newsItem.images[0],
        };
    });
    await TrackPlayer.add(tracks);
    await TrackPlayer.skip(trackList[startIndex]);
    await TrackPlayer.play();
};

mohity777 avatar Jun 20 '21 18:06 mohity777

EDIT -

  1. It is occuring if player.destroy() throws some error
    Receiver not registered: com.guichaguri.trackplayer.service.MusicManager

mohity777 avatar Jun 20 '21 19:06 mohity777

You should to get capabilities from Capability not from TrackPlayer example:-

capabilities: [ Capability.Play, Capability.Pause, Capability.Stop, Capability.JumpForward, Capability.JumpBackward, ],

ebrahimmelngary avatar Aug 31 '21 16:08 ebrahimmelngary

The code i have written is for V1.2.7, The one you have written down is for the new version V2.0.0 +

mohity777 avatar Aug 31 '21 17:08 mohity777

@mohity777 is this an issue with v2? At the moment we're not maintaining v1.

dcvz avatar Oct 19 '21 12:10 dcvz

Weighing in here, the crash is occurring for me on "react-native-track-player": "2.1.1". This is a screengrab from Crashlytics. Same error message as OP.

Screenshot 2021-10-20 at 09 38 09

nickmcmillan avatar Oct 19 '21 22:10 nickmcmillan

Is this PR related? https://github.com/DoubleSymmetry/react-native-track-player/pull/1167

It might be better to ignore errors silently than to crash for real users.

nickmcmillan avatar Oct 19 '21 22:10 nickmcmillan

The "attempt to invoke virtual method ... on a null object reference" crash is by far our most common crash on Android. It's the most serious issue we have with Podverse on Android.

image

mitchdowney avatar Nov 02 '21 00:11 mitchdowney

I can confirm it's happening also in V2

mattiaefesto avatar Nov 02 '21 13:11 mattiaefesto

Thanks for the updates here, are any of you able to try our current pre release of the Android rewrite? We believe we've sorted out a whole class of crashes there, but would love some feedback.

dcvz avatar Nov 05 '21 11:11 dcvz

Hey @dcvz, we tried installing the beta on Podverse but we are getting the following Kotlin error on built time:

A problem occurred evaluating project ':react-native-track-player'.
> Plugin with id 'kotlin-android' not found.

I think there is something like an import missing in your node_module export. Not sure what it is because I've not dabbled with Kotlin too much.

cc @mitchdowney

kreonjr avatar Nov 05 '21 20:11 kreonjr

@kreonjr did you follow the instructions here: https://github.com/DoubleSymmetry/react-native-track-player/discussions/1264 ?

dcvz avatar Nov 05 '21 21:11 dcvz

Hi, we can't upgrade to apiLevel 31 because of others dependencies of libraries not updated, so we are facing this issues in version 2.1.2...

mrigo avatar Dec 10 '21 09:12 mrigo

This should be fixed in #1371

Can y'all help us test this out and see?

@mrigo @kreonjr @mattiaefesto @nickmcmillan @mohity777

brad-sf avatar Jan 15 '22 10:01 brad-sf

Hey @dcvz, we tried installing the beta on Podverse but we are getting the following Kotlin error on built time:

A problem occurred evaluating project ':react-native-track-player'.
> Plugin with id 'kotlin-android' not found.

I think there is something like an import missing in your node_module export. Not sure what it is because I've not dabbled with Kotlin too much.

cc @mitchdowney

@kreonjr this particular issue is resolved in v2.2.0-rc3 via https://github.com/doublesymmetry/react-native-track-player/pull/1446.

jspizziri avatar Apr 13 '22 13:04 jspizziri

@mrigo , all the energy is currently going into the v2.2.0 and beyond. Were you able to upgrade your project to use a newer version of the package?

jspizziri avatar Apr 13 '22 13:04 jspizziri

Hi, we can't upgrade to apiLevel 31 because of others dependencies of libraries not updated, so we are facing this issues in version 2.1.2...

@mrigo the update to the apiVersion are the result of the upgrade in RN v0.68 we're going to try and stay in sync with RN.

Because all the energy is going into v2.2.0 this issue will be left open for now but will be closed once that version is formally released (as it appears that version will solve all the issues raised here).

jspizziri avatar Apr 13 '22 14:04 jspizziri

Get crash in androind12 any quick solution?

I am using "react-native": "0.67.2" minSdkVersion = 23 compileSdkVersion = 31 targetSdkVersion = 31

Screenshot 2022-04-26 at 3 22 00 PM

await TrackPlayer.setupPlayer({});

I Do this for quick fix: https://github.com/doublesymmetry/react-native-track-player/issues/1484#issuecomment-1110812304

MaxJadav avatar Apr 26 '22 09:04 MaxJadav

I've tried building version v2.2.0-rc3 for android but it crashes immediately without any runtime errors. I'm not sure if I'm the only one encountering this?

testfilee15 avatar May 03 '22 03:05 testfilee15

@testfilee15 I've not seen that. Take a look at the example project. That builds and runs just fine and it's using main

jspizziri avatar May 03 '22 05:05 jspizziri

It looks like it was a version issue, I upgraded to the v2.2.0-rc3 and now it's all good. Thanks!

testfilee15 avatar May 04 '22 00:05 testfilee15

I was testing v2.2.0-rc3 on an Android 12 emulator, and it lost the app icon and used the generic Android app icon. I then switched back to ^2.1.3 and the icon was back to normal. I have no idea how this could be related, but it may be something to look for. The only tracked file changes were package.json, yarn.lock, and Podfile.lock.

brantleyenglish avatar Jun 08 '22 15:06 brantleyenglish

Exception in native call- java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.Handler.post(java.lang.Runnable)' on a null object reference

I was getting the same error on android (also not every time but every 3 - 4 times), using v2.1.3 of Track-Player with RN 0.67.4. Maybe I can help someone as I seem to have found a hacky fix for my case:

To start my audio player component, I was using:

// Start
TrackPlayer.setupPlayer()

To stop the player:

// Stop
TrackPlayer.reset()
TrackPlayer.destroy()

Then I changed my start method and added the destroy call before the new initialization, and now the error seems to be gone (left the stop method untouched):

// Start
TrackPlayer.destroy()
TrackPlayer.setupPlayer()

EDIT: Unfortunatly I still get the error sometimes :( but it definitely became less

c-goettert avatar Jun 24 '22 07:06 c-goettert

v3.1 is now out. If you're having issues with Android 12 please upgrade to the latest version.

jspizziri avatar Aug 30 '22 18:08 jspizziri

v3.1 is now out. If you're having issues with Android 12 please upgrade to the latest version.

this work for me with that you also have to update your gradle also.

mschouhan123 avatar Nov 22 '22 09:11 mschouhan123