Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added setPan behavior for android #443

Merged
merged 2 commits into from
Aug 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions sound.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@ function isRelativePath(path) {
return !/^(\/|http(s?)|asset)/.test(path);
}

function calculateRelativeVolume(volume, pan) {
// calculates a lower volume relative to the pan value
const relativeVolume = (volume * (1 - Math.abs(pan)));
return Number(relativeVolume.toFixed(1));
}

function setAndroidVolumes(sound) {
// calculates the volumes for left and right channels
if (sound._pan) {
const relativeVolume = calculateRelativeVolume(sound._volume, sound._pan);
if (sound._pan < 0) {
// left is louder
RNSound.setVolume(sound._key, sound._volume, relativeVolume);
} else {
// right is louder
RNSound.setVolume(sound._key, relativeVolume, sound._volume);
}
} else {
// no panning, same volume on both channels
RNSound.setVolume(sound._key, sound._volume, sound._volume);
}
}

function Sound(filename, basePath, onError, options) {
var asset = resolveAssetSource(filename);
if (asset) {
Expand Down Expand Up @@ -146,15 +169,29 @@ Sound.prototype.getVolume = function() {
Sound.prototype.setVolume = function(value) {
this._volume = value;
if (this._loaded) {
if (IsAndroid || IsWindows) {
RNSound.setVolume(this._key, value, value);
if (IsAndroid) {
setAndroidVolumes(this)
} else {
RNSound.setVolume(this._key, value);
}
}
return this;
};

Sound.prototype.setPan = function(value) {
this._pan = value;
if (this._loaded) {
if (IsWindows) {
throw new Error('#setPan not supported on windows');
} else if (IsAndroid) {
setAndroidVolumes(this)
} else {
RNSound.setPan(this._key, value);
}
}
return this;
};

Sound.prototype.getSystemVolume = function(callback) {
if(!IsWindows) {
RNSound.getSystemVolume(callback);
Expand All @@ -173,13 +210,6 @@ Sound.prototype.getPan = function() {
return this._pan;
};

Sound.prototype.setPan = function(value) {
if (this._loaded) {
RNSound.setPan(this._key, this._pan = value);
}
return this;
};

Sound.prototype.getNumberOfLoops = function() {
return this._numberOfLoops;
};
Expand Down