Last active
July 21, 2016 07:36
-
-
Save angel-vladov/7b56fa8d06d9cf9905fcad629a995fd9 to your computer and use it in GitHub Desktop.
Cordova hook for ringer volume controled Media on Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// Changes your app to use STREAM_RING instead of STREAM_MEDIA | |
// v1.0 | |
// Install mkdirp before using this hook. Run npm install mkdirp --save | |
// Place this file in hooks/after-prepare | |
// This hook will change AudioManager.STREAM_MUSIC to AudioManager.STREAM_RING | |
var fs = require('fs'); | |
var path = require('path'); | |
var mkdirp = require('mkdirp'); | |
var rootdir = process.argv[2]; | |
var androidCordovaMedia = [ | |
'src/org/apache/cordova/media/AudioHandler.java', | |
'src/org/apache/cordova/media/AudioPlayer.java', | |
]; | |
function androidStreamRing(codePath) { | |
var javaCode = fs.readFileSync(codePath, 'utf8'); | |
javaCode = javaCode.replace(new RegExp('AudioManager.STREAM_MUSIC', 'g'), 'AudioManager.STREAM_RING'); | |
fs.writeFileSync(codePath, javaCode, 'utf8'); | |
process.stdout.write('Android Stream Ring changed to STREAM_RING\n'); | |
} | |
function updateAndroidAudio() { | |
for (var i = 0; i < androidCordovaMedia.length; i++) { | |
var codePath = androidCordovaMedia[i]; | |
codePath = path.join('platforms/android', codePath); | |
if (fs.existsSync(codePath)) { | |
androidStreamRing(codePath); | |
} else { | |
throw 'Cordova Media implementation changed. Modify "' + path.basename(process.env.CORDOVA_HOOK) + '".'; | |
} | |
} | |
} | |
if (rootdir) { | |
var platforms = (process.env.CORDOVA_PLATFORMS ? process.env.CORDOVA_PLATFORMS.split(',') : []); | |
for(var x = 0; x < platforms.length; x++) { | |
try { | |
var platform = platforms[x].trim().toLowerCase(); | |
if (platform == 'android') { | |
updateAndroidAudio(); | |
} | |
} | |
catch (e) { | |
process.stdout.write(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment