Skip to content
Closed
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
44 changes: 40 additions & 4 deletions src/android/AudioHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class AudioHandler extends CordovaPlugin {
public static String TAG = "AudioHandler";
HashMap<String, AudioPlayer> players; // Audio player object
ArrayList<AudioPlayer> pausedForPhone; // Audio players that were paused when phone call came in
ArrayList<AudioPlayer> pausedForScreenLock; // Audio players that were paused when phone locked the screen

private int origVolumeStream = -1;
private CallbackContext messageChannel;

Expand Down Expand Up @@ -92,13 +94,20 @@ else if (action.equals("stopRecordingAudio")) {
else if (action.equals("startPlayingAudio")) {
String target = args.getString(1);
String fileUriStr;
boolean playAudioWhenScreenIsLocked=true;
try {
Uri targetUri = resourceApi.remapUri(Uri.parse(target));
fileUriStr = targetUri.toString();
} catch (IllegalArgumentException e) {
fileUriStr = target;
}
this.startPlayingAudio(args.getString(0), FileHelper.stripFileProtocol(fileUriStr));
try {
JSONObject playArgs = new JSONObject(args.getString(2));
playAudioWhenScreenIsLocked = playArgs.getBoolean("playAudioWhenScreenIsLocked");
} catch (JSONException e) {
playAudioWhenScreenIsLocked = true;
}
this.startPlayingAudio(args.getString(0), FileHelper.stripFileProtocol(fileUriStr),playAudioWhenScreenIsLocked);
}
else if (action.equals("seekToAudio")) {
this.seekToAudio(args.getString(0), args.getInt(1));
Expand Down Expand Up @@ -209,14 +218,19 @@ else if ("idle".equals(data)) {
//--------------------------------------------------------------------------

private AudioPlayer getOrCreatePlayer(String id, String file) {
return getOrCreatePlayer(id,file,true);
}

private AudioPlayer getOrCreatePlayer(String id, String file, boolean playAudioWhenScreenIsLocked) {
AudioPlayer ret = players.get(id);
if (ret == null) {
if (players.isEmpty()) {
onFirstPlayerCreated();
}
ret = new AudioPlayer(this, id, file);
ret = new AudioPlayer(this, id, file, playAudioWhenScreenIsLocked);
players.put(id, ret);
}
ret.setPlayAudioWhenScreenIsLocked(playAudioWhenScreenIsLocked);
return ret;
}

Expand Down Expand Up @@ -262,8 +276,8 @@ public void stopRecordingAudio(String id) {
* @param id The id of the audio player
* @param file The name of the audio file.
*/
public void startPlayingAudio(String id, String file) {
AudioPlayer audio = getOrCreatePlayer(id, file);
public void startPlayingAudio(String id, String file, boolean playAudioWhenScreenIsLocked) {
AudioPlayer audio = getOrCreatePlayer(id, file, playAudioWhenScreenIsLocked);
audio.startPlaying(file);
}

Expand Down Expand Up @@ -407,4 +421,26 @@ void sendEventMessage(String action, JSONObject actionData) {
messageChannel.sendPluginResult(pluginResult);
}
}

@Override
public void onResume(boolean multitasking) {
super.onResume(multitasking);
for (AudioPlayer audioPlayer : pausedForScreenLock) {
// if we need to resume playing the function readyPlayer dosen't needs paramenters
audioPlayer.startPlaying(null);
}
pausedForScreenLock = null;
}

@Override
public void onPause(boolean multitasking) {
pausedForScreenLock = new ArrayList<AudioPlayer>();
for (AudioPlayer audioPlayer : players.values()) {
if (audioPlayer.getState()==AudioPlayer.STATE.MEDIA_RUNNING.ordinal()&&!audioPlayer.isPlayAudioWhenScreenIsLocked()){
audioPlayer.pausePlaying();
pausedForScreenLock.add(audioPlayer);
}
}
super.onPause(multitasking);
}
}
17 changes: 14 additions & 3 deletions src/android/AudioPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,29 @@ public enum STATE { MEDIA_NONE,
private MediaPlayer player = null; // Audio player object
private boolean prepareOnly = true; // playback after file prepare flag
private int seekOnPrepared = 0; // seek to this location once media is prepared

/**

private boolean playAudioWhenScreenIsLocked = true; // If set to false the playback will be paused when app is paused

public boolean isPlayAudioWhenScreenIsLocked() {
return playAudioWhenScreenIsLocked;
}

public void setPlayAudioWhenScreenIsLocked(boolean playAudioWhenScreenIsLocked) {
this.playAudioWhenScreenIsLocked = playAudioWhenScreenIsLocked;
}

/**
* Constructor.
*
* @param handler The audio handler object
* @param id The id of this audio player
*/
public AudioPlayer(AudioHandler handler, String id, String file) {
public AudioPlayer(AudioHandler handler, String id, String file, boolean playAudioWhenScreenIsLocked) {
this.handler = handler;
this.id = id;
this.audioFile = file;
this.recorder = new MediaRecorder();
this.playAudioWhenScreenIsLocked = playAudioWhenScreenIsLocked;

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
this.tempFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/tmprecording.3gp";
Expand Down