I am working on a React Native application where we add downloads functionality. The code was built as a native modules in the Java enviroment. However we found a potential issue related with trying to delete a video when a error event occured (noOptionsAvailable
or downlaodFailed
) . We manage this by when the event is triggered, we send a payload to update the state of the video downloaded in our DB on our React Native app. On top of this, we delete the video from our database when is failed but also it triggeres the action to bitmovin to do the same internally (offlineContentManager.clearAll()
).
The thing is when I try to download the same video avoiding to produce an error, the video doesn’t download, when debugging it seems that bitmovin didn’t clear the videoConfig
from the downlaod folder.
So I was wonder how would be the best practice here to remove efficiently any trace from a video when this has been failed.
Here some snippets:
Delete react function:
@ReactMethod
public void deleteVideo(String videoManifestUrl) {
if (videoManifestUrl != null) {
OfflineContentManager offlineContentManager;
if (_stateHashMap.containsKey(videoManifestUrl)) {
offlineContentManager = _stateHashMap.get(videoManifestUrl).getOfflineContentManager();
_stateHashMap.get(videoManifestUrl).setAction(DELETE);
} else {
SourceConfig sourceConfig = new SourceConfig(videoManifestUrl, SourceType.Dash);
offlineContentManager = OfflineContentManager.getOfflineContentManager(sourceConfig,
_file.getPath(),
videoManifestUrl,
this,
_context);
OfflineManagerAction offlineManagerAction = new OfflineManagerAction(offlineContentManager, DELETE);
_stateHashMap.put(sourceConfig.getUrl(), offlineManagerAction);
}
offlineContentManager.deleteAll();
}
}
And the onError
event handler:
@Override
public void onError(SourceConfig sourceConfig, ErrorEvent errorEvent) {
if (errorEvent instanceof OfflineErrorEvent && _stateHashMap.containsKey(sourceConfig.getUrl())) {
OfflineManagerAction offlineManagerAction = _stateHashMap.get(sourceConfig.getUrl());
if(OfflineErrorCode.InsufficientStorage.equals(errorEvent.getCode())) {
WritableMap payload = Arguments.createMap();
payload.putString("errorMessage", errorEvent.getMessage());
payload.putBoolean("insufficientStorage",true);
sendEvent("insufficientStorageEvent", payload);
}
if(OfflineErrorCode.DownloadFailed.equals(errorEvent.getCode()) || OfflineErrorCode.NoOptionsAvailable.equals(errorEvent.getCode())) {
WritableMap payload = Arguments.createMap();
payload.putString("errorMessage",errorEvent.getMessage());
payload.putString("completeURL", sourceConfig.getUrl());
payload.putBoolean("downloadFailed",true);
sendEvent("downloadFailedEvent",payload);
}
}
}
One observation is that we call offlineManagerAction.getOfflineContentManager().release()
when onComplete
event happens and the setAction
is set to Delete
.
if (offlineManagerAction != null && offlineManagerAction.getAction().equals(DELETE)) {
WritableMap payload = Arguments.createMap();
payload.putString("completeURL", sourceConfig.getUrl());
sendEvent("onDeleteEvent", payload);
_stateHashMap.remove(sourceConfig.getUrl());
offlineManagerAction.getOfflineContentManager().release();
}