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

pause and volume for android #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
56 changes: 52 additions & 4 deletions src/android/PGLowLatencyAudio.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ public class PGLowLatencyAudio extends CordovaPlugin {
public static final String PRELOAD_AUDIO="preloadAudio";
public static final String PLAY="play";
public static final String STOP="stop";
public static final String PAUSE="pause";
public static final String LOOP="loop";
public static final String UNLOAD="unload";
public static final String VOLUME="volume";


public static final int DEFAULT_POLYPHONY_VOICES = 15;

Expand Down Expand Up @@ -156,12 +159,17 @@ else if ( soundMap.containsKey(audioID) )
return false;
}
}
else if ( STOP.equals( action ) || UNLOAD.equals( action ) )
else if ( STOP.equals( action ) || PAUSE.equals( action ) || UNLOAD.equals( action ) )
{
if ( assetMap.containsKey(audioID) )
{
PGLowLatencyAudioAsset asset = assetMap.get( audioID );
asset.stop();
if(PAUSE.equals( action )) {
asset.pause();
}
else {
asset.stop();
}

callbackContext.success("OK");
return true;
Expand All @@ -171,8 +179,15 @@ else if ( soundMap.containsKey(audioID) )
ArrayList<Integer> streams = streamMap.get( audioID );
if ( streams != null )
{
for ( int x=0; x< streams.size(); x++)
soundPool.stop( streams.get(x) );
for ( int x=0; x< streams.size(); x++) {
if(PAUSE.equals( action )) {
soundPool.pause( streams.get(x) );
}
else {
soundPool.stop( streams.get(x) );
}

}
}
streamMap.remove( audioID );

Expand Down Expand Up @@ -212,6 +227,39 @@ else if ( soundMap.containsKey(audioID) ){
return false;
}
}
if ( VOLUME.equals( action ) )
{
float volLeft = (float) args.getDouble(1);
float volRight = (float) args.getDouble(2);
if ( assetMap.containsKey(audioID) ) {

PGLowLatencyAudioAsset asset = assetMap.get( audioID );
asset.volume(volLeft, volRight);

callbackContext.success("OK");
return true;

}
else if ( soundMap.containsKey(audioID) ){
//TODO test
ArrayList<Integer> streams = streamMap.get( audioID );

if ( streams != null )
{
for ( int x=0; x< streams.size(); x++) {
soundPool.setVolume(streams.get(x), volLeft, volRight);
}
}
callbackContext.success("OK");
return true;
}
else
{
//TODO error handling
return false;
}
}


}
catch (Exception ex)
Expand Down
20 changes: 20 additions & 0 deletions src/android/PGLowLatencyAudioAsset.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ public void stop() throws IOException
}
}

public void pause() throws IOException
{
for ( int x=0; x<voices.size(); x++)
{
PGPolyphonicVoice voice = voices.get(x);
voice.pause();
}
}

public void loop() throws IOException
{
PGPolyphonicVoice voice = voices.get(playIndex);
Expand All @@ -72,5 +81,16 @@ public void unload() throws IOException
}
voices.removeAll(voices);
}

public void volume(float left, float right) {
for ( int x=0; x<voices.size(); x++)
{
PGPolyphonicVoice voice = voices.get(x);
voice.volume(left,right);
}


}


}
16 changes: 16 additions & 0 deletions src/android/PGPolyphonicVoice.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ else if ( !playing )
mp.start();
}
}

public void pause() throws IOException
{
if ( mp.isLooping() || mp.isPlaying() )
{
state = INVALID;
mp.pause();
}
}

public void stop() throws IOException
{
Expand All @@ -90,6 +99,13 @@ public void unload() throws IOException
this.stop();
mp.release();
}

public void volume(float left, float right) {

mp.setVolume(left,right);

}


public void onPrepared(MediaPlayer mPlayer)
{
Expand Down
9 changes: 9 additions & 0 deletions www/LowLatencyAudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ var LowLatencyAudio = {
return exec(success, fail, "LowLatencyAudio", "play", [id]);
},

pause: function (id, success, fail) {
return exec(success, fail, "LowLatencyAudio", "pause", [id]);
},

stop: function (id, success, fail) {
return exec(success, fail, "LowLatencyAudio", "stop", [id]);
},
Expand All @@ -26,7 +30,12 @@ var LowLatencyAudio = {

unload: function (id, success, fail) {
return exec(success, fail, "LowLatencyAudio", "unload", [id]);
},

volume: function (id, left, right, success, fail) {
return cordova.exec(success, fail, "LowLatencyAudio", "volume", [id,left,right]);
}

};

module.exports = LowLatencyAudio;
Expand Down