arbel03
arbel03

Reputation: 1247

React Native java module NativeEventEmitter is not emitting events

I am converting the following module to Java: https://github.com/dehy/react-native-radio-player

So I have created a new module inside my repo. But I am failing at receiving events from ExoPlayer. https://github.com/arbel03/react-radio-app/tree/b3d0aa332249840fdc3616ff11d9e07f2e7f18e0

Android Side:

  @Override
  public void onPlaybackStateChanged(int playbackState) {
    Player.Listener.super.onPlaybackStateChanged(playbackState);

    Logger.getLogger(getName()).info(String.format("onPlaybackStateChanged: %d", playbackState));

    AudioSessionState newState = null;

    switch (playbackState) {
      case Player.STATE_IDLE:
      case Player.STATE_ENDED:
        newState = AudioSessionState.Stopped;
        break;
      case Player.STATE_BUFFERING:
        newState = AudioSessionState.Buffering;
        break;
      case Player.STATE_READY:
        newState = AudioSessionState.Playing;
        break;
    }

    if ((newState == null) || (this.playerState == newState)) {
      return;
    }

    this.playerState = newState;

    WritableNativeMap stateMap = new WritableNativeMap();
    stateMap.putString("state", this.playerState.name());
    sendEvent("StateDidChange", stateMap);
  }

React Side:

function setListeners(dispatch) {
    return new Promise((resolve, reject) => {
        console.log("Set listeners1");
        AudioSessionEvents.addListener('StateDidChange', (eventObject) => {
            console.log("Received event!!");
            dispatch(playerReducer.actions.setPlayerState(eventObject.state as AudioSessionState));
        });
        
        console.log("Set listeners2");
        // TODO: return the uninitializer from useEffect
        AudioSessionEvents.addListener('MetadataDidChange', (metadata) => {
            console.log("Received event!!");
            dispatch(playerReducer.actions.setPlayerMetadata(metadata));
        });

        resolve(null);
    })
}

export function uninitializePlayer() {
    AudioSessionEvents.removeAllListeners();
}

export function initializePlayer(dispatch) {
    console.log("initializePlayer");
    setListeners(dispatch)
    .then(() => AudioSession.setUrl(Config.StreamUrl))
    .then(() => AudioSession.getPlayerState()) // This triggers a @ReactNative java function and it copmletes the request successfuly. Sending asynchronous events doesn't work.
    .then((playerState) => {
        console.log(playerState);
        dispatch(playerReducer.actions.setPlayerState(playerState.state as AudioSessionState));
    })
}

initializePlayer is being called inside componentDidMount (as a matter of fact I am calling useEffect because I chose to implement a functional component)

This is what's being printed when I press play:

 BUNDLE  ./index.js

 LOG  Running "radio_app" with {"rootTag":1}
 LOG  initializePlayer
 LOG  Set listeners1
 LOG  Set listeners2
 LOG  {"state": "Initializing"}
 LOG  setPlayerState {"payload": "Initializing", "type": "player/setPlayerState"}
 LOG  Play

And this is the output from LogCat:

2022-02-20 19:04:33.784 5054-5113/com.radio_app I/AudioSession: setUrl: http://igor.torontocast.com:1025/;
2022-02-20 19:04:33.805 5054-5113/com.radio_app I/AudioSession: getPlayerState: Initializing
2022-02-20 19:04:35.176 5054-5113/com.radio_app I/AudioSession: play
2022-02-20 19:04:35.178 5054-5054/com.radio_app I/AudioSession: onPlaybackStateChanged: 2
2022-02-20 19:04:35.186 5054-5054/com.radio_app I/AudioSession: Sending event StateDidChange
2022-02-20 19:04:35.884 5054-5054/com.radio_app I/AudioSession: onMediaMetadataChanged: title=null
2022-02-20 19:04:35.886 5054-5054/com.radio_app I/AudioSession: Sending event MetadataDidChange
2022-02-20 19:04:35.973 5054-5054/com.radio_app I/AudioSession: onMediaMetadataChanged: title=Faith - I believe
2022-02-20 19:04:35.974 5054-5054/com.radio_app I/AudioSession: Sending event MetadataDidChange
2022-02-20 19:04:35.978 5054-5054/com.radio_app I/AudioSession: onPlaybackStateChanged: 3
2022-02-20 19:04:35.979 5054-5054/com.radio_app I/AudioSession: Sending event StateDidChange
2022-02-20 19:05:01.086 5054-5054/com.radio_app I/AudioSession: onMediaMetadataChanged: title=Arashi - Take Off!!!!!
2022-02-20 19:05:01.087 5054-5054/com.radio_app I/AudioSession: Sending event MetadataDidChange

There is a line saying "Sending event StateDidChange" which I log inside sendEvent. But "Received event!!" is not being printed at all. The events don't reach their destination.

Upvotes: 0

Views: 1242

Answers (1)

arbel03
arbel03

Reputation: 1247

I solved it by adding the module as a class in my root project, instead of a pluggable react module. I think what did the trick was adding the module inside getPackages()

Upvotes: 0

Related Questions