开发者

Android: is therey anyway to volume control by javascript

开发者 https://www.devze.com 2023-03-12 13:56 出处:网络
Android : Phonegap has javasript functions to to开发者_StackOverflow中文版 play,pause,resume & stop the media player. Is there any way to control the volume(increase/decrease) by javascripst funct

Android : Phonegap has javasript functions to to开发者_StackOverflow中文版 play,pause,resume & stop the media player. Is there any way to control the volume(increase/decrease) by javascripst function.

1.phonegap.js file included

in html page :

<html>
<head> <script>
var my_media = null;
        var mediaTimer = null;

        function playAudio(src) {

            my_media = new Media(src, onSuccess, onError);
            my_media.play();
            ....
}
</script> </head> <body>
<a href="#" onclick="playAudio('http://audio.ibeat.org/content/p1rj1s/p1rj1s.mp3');">Play Audio</a>

</body> </html>

in java class :

...
super.loadUrl("file:///android_asset/www/testmus.html");


I know this question is fairly old, but I came across it looking for something else.

There is a newish and not well-documented function on the PhoneGap Media class for Android:

setVolume()

So using the example above:

// level is a value from 0-100
var level = 50;

my_media.setVolume(level);

It's not heavily documented because I believe it's only on Android.


This is old, but as we were just solving this issue, I thought I'd put it up here.

The cordova my_media.setVolume(level); doesn't actually set the master volume of your audio stream, it just sets the volume of your audio asset relative to the overall audio stream's volume. For instance, if I have my device's music player stream set to half of the maximum, and I am playing my audio into the music player stream, then the loudest I can ever make it is half of maximum volume. For our uses, this was not enough.

To get around this, you can create an audio slider for your specific audio stream, hide it, and then programmatically set the slider's volume. This was figured out for ios and explained here -iOS 7: MPMusicPlayerController volume deprecated. How to change device volume now?

The code to create controllable audio sliders using cordova was originally done for ios, and then it was forked to Android. I would post the links, but I'm link-limited due to my non-existent reputation.

We combined these two, added in the ios volume control code, and then ported that over to Android as well so that there is now a cordova library to allow for Andoid and iOS music stream volume control here - https://github.com/shibbs/VolumeSlider

We have created a pull request to merge our version of the audio controller in with the devgeeks cordova volume controller code, but for the time being, the place to get it will be from the shibbs/ linked above.

A code snippet showing how you would go about it is :

my_media = new Media(soundFile, onSuccess, onError); //create the media object
my_media.setVolume(1.0); //set your volume to w/e you want, we maxed it
volumeSlider = plugins.volumeSlider; //create the volume slider object
volumeSlider.createVolumeSlider(10,350,300,30); // origin x,     origin y, width, height
volumeSlider.setVolumeSlider(1.0); //set your volume slider value to between 0 and 1

volumeSlider.resetVolumeSlider(); //this will reset the device volume to whatever it was set to when you created the volumeSlider. 


You would have to do what (I assume) phonegap does and use a Javascript bridge to the existing Java functions that control the volume. Volume control is in adjustVolume and adjustStreamVolume(): you can add custom javascript handlers as shown here.

0

精彩评论

暂无评论...
验证码 换一张
取 消