Skip to content

Commit

Permalink
fix(android): get external files directory for Android 10+ (#317)
Browse files Browse the repository at this point in the history
* fix: fetching from external file directory
* chore: cleanup createAudioFilePath
* fix: string format %l should be %d
* refactor: remove duplicate getAbsolutePath
* chore: update createAudioFilePath comment block
* fix: update fileName conditional for null
  • Loading branch information
erisu committed Oct 22, 2021
1 parent 458765b commit 4093f7e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/android/AudioHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public AudioHandler() {
this.pausedForFocus = new ArrayList<AudioPlayer>();
}

public Context getApplicationContext() {
return cordova.getActivity().getApplicationContext();
}

protected void getWritePermission(int requestCode)
{
Expand Down
36 changes: 22 additions & 14 deletions src/android/AudioPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one
*/
package org.apache.cordova.media;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
Expand Down Expand Up @@ -78,6 +79,7 @@ public enum STATE { MEDIA_NONE,
// private static int MEDIA_ERR_NONE_SUPPORTED = 4;

private AudioHandler handler; // The AudioHandler object
private Context context; // The Application Context object
private String id; // The id of this player (used to identify Media object in JavaScript)
private MODE mode = MODE.NONE; // Playback or Recording mode
private STATE state = STATE.MEDIA_NONE; // State of recording or playback
Expand All @@ -101,19 +103,29 @@ public enum STATE { MEDIA_NONE,
*/
public AudioPlayer(AudioHandler handler, String id, String file) {
this.handler = handler;
context = handler.getApplicationContext();
this.id = id;
this.audioFile = file;
this.tempFiles = new LinkedList<String>();

}

private String generateTempFile() {
String tempFileName = null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
tempFileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/tmprecording-" + System.currentTimeMillis() + ".3gp";
} else {
tempFileName = "/data/data/" + handler.cordova.getActivity().getPackageName() + "/cache/tmprecording-" + System.currentTimeMillis() + ".3gp";
}
return tempFileName;
/**
* Creates an audio file path from the provided fileName or creates a new temporary file path.
*
* @param fileName the audio file name, if null a temporary 3gp file name is provided
* @return String
*/
private String createAudioFilePath(String fileName) {
File dir = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
? context.getExternalFilesDir(null)
: context.getCacheDir();

fileName = (fileName == null || fileName.isEmpty())
? String.format("tmprecording-%d.3gp", System.currentTimeMillis())
: fileName;

return dir.getAbsolutePath() + File.separator + fileName;
}

/**
Expand Down Expand Up @@ -155,7 +167,7 @@ public void startRecording(String file) {
this.recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
this.recorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS); // RAW_AMR);
this.recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); //AMR_NB);
this.tempFile = generateTempFile();
this.tempFile = createAudioFilePath(null);
this.recorder.setOutputFile(this.tempFile);
try {
this.recorder.prepare();
Expand Down Expand Up @@ -185,11 +197,7 @@ public void moveFile(String file) {
/* this is a hack to save the file as the specified name */

if (!file.startsWith("/")) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
file = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + file;
} else {
file = "/data/data/" + handler.cordova.getActivity().getPackageName() + "/cache/" + file;
}
file = createAudioFilePath(file);
}

int size = this.tempFiles.size();
Expand Down

0 comments on commit 4093f7e

Please sign in to comment.