Skip to content

Commit

Permalink
Use a hash function to compute key based on file name.
Browse files Browse the repository at this point in the history
  • Loading branch information
varungupta85 committed Dec 13, 2017
1 parent febaa72 commit 42f3295
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions sound.js
Expand Up @@ -4,12 +4,21 @@ var RNSound = require('react-native').NativeModules.RNSound;
var IsAndroid = RNSound.IsAndroid;
var IsWindows = RNSound.IsWindows;
var resolveAssetSource = require("react-native/Libraries/Image/resolveAssetSource");
var nextKey = 0;

function isRelativePath(path) {
return !/^(\/|http(s?)|asset)/.test(path);
}

// Hash function to compute key from the filename
function djb2Code(str) {
var hash = 5381, i, char;
for (i = 0; i < str.length; i++) {
char = str.charCodeAt(i);
hash = ((hash << 5) + hash) + char; /* hash * 33 + c */
}
return hash;
}

function Sound(filename, basePath, onError, options) {
var asset = resolveAssetSource(filename);
if (asset) {
Expand All @@ -24,7 +33,7 @@ function Sound(filename, basePath, onError, options) {
}

this._loaded = false;
this._key = nextKey++;
this._key = djb2Code(filename);
this._duration = -1;
this._numberOfChannels = -1;
this._volume = 1;
Expand Down

0 comments on commit 42f3295

Please sign in to comment.