我正在将以下模块转换为 Java:https ://github.com/dehy/react-native-radio-player
所以我在我的仓库中创建了一个新模块。但我无法从 ExoPlayer 接收事件。https://github.com/arbel03/react-radio-app/tree/b3d0aa332249840fdc3616ff11d9e07f2e7f18e0
安卓端:
@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);
}
反应方面:
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));
})
}
在 componentDidMount 内部调用了 initializePlayer(事实上,我之所以调用 useEffect 是因为我选择实现一个功能组件)
这是我按下播放时正在打印的内容:
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
这是 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
有一行说“发送事件 StateDidChange”,我在 sendEvent 中记录。但是“收到事件!!” 根本没有打印出来。事件没有到达目的地。