Closed agamemnus closed 9 years ago
Possibly fixed the second issue.
File.exists (via File plugin) doesn't work for APK expansion files. The code then tries setting the file to
file = Environment.getExternalStorageDirectory().getPath() + "/" + file
, creating a malformed URL. So, had to modify loadAudioFile
in AudioPlayer.java
:
/**
* load audio file
* @throws IOException
* @throws IllegalStateException
* @throws SecurityException
* @throws IllegalArgumentException
*/
private void loadAudioFile(String file) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException {
if (this.isStreaming(file)) {
this.player.setDataSource(file);
this.player.setAudioStreamType(AudioManager.STREAM_MUSIC);
//if it's a streaming file, play mode is implied
this.setMode(MODE.PLAY);
this.setState(STATE.MEDIA_STARTING);
this.player.setOnPreparedListener(this);
this.player.prepareAsync();
} else {
if (file.startsWith("/android_asset/")) {
String f = file.substring(15);
android.content.res.AssetFileDescriptor fd = this.handler.cordova.getActivity().getAssets().openFd(f);
this.player.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
} else {
File fp = new File(file);
if (fp.exists()) {
FileInputStream fileInputStream = new FileInputStream(file);
this.player.setDataSource(fileInputStream.getFD());
fileInputStream.close();
} else {
String original_file = file;
file = Environment.getExternalStorageDirectory().getPath() + "/" + file;
File fp = new File(file);
if (fp.exists()) {
this.player.setDataSource(file);
} else {
FileInputStream fileInputStream = new FileInputStream(original_file);
this.player.setDataSource(fileInputStream.getFD());
fileInputStream.close();
}
}
}
this.setState(STATE.MEDIA_STARTING);
this.player.setOnPreparedListener(this);
this.player.prepare();
// Get duration
this.duration = getDurationInSeconds();
}
}
}
Have you try with an html audio tag to play your audio file or is it a necessity to use le Media plugin?
If the File plugin always concatenates files paths with external storage directory's base path it's a big problem for its compatibility with this plugin... :s
I have to use the Media plugin.
The audio tag doesn't run audio without click events (very bad for games..), can't play concurrent audio, and can't play short pieces of audio without a .5-1s delay. It's unusable for games.
The File plugin does not always concatenate -- it does this only when (new File.exists(file))
is false, so a better solution is to figure out why File.exists is returning false in this case. I don't know enough Java to do this.
I added a pull request although I barely know what I'm doing...:
Ok. It seems like the file really just isn't visible. The audio elements aren't being played.
Possibly this will help to fix it? (I don't know..)
https://github.com/mcfarljw/cordova-plugin-expansion/blob/master/src/android/ExpansionPlugin.java
Okay.
I did a day-long workaround around the Media issue: I am now using a modified version of @goldfire's howler.js (with preload buffers for long files) and loading my files via XMLHttpRequest and AudioContext: https://github.com/agamemnus/howler.js
Is it possible with this plugin? Trying to read audio, .json, and .css files just seems to crash everything.
Edit: Here's a log.
Edit: Possibly fixed THAT one (will mention how later if this pans out), but here's another one:
Note that I am using the Media plugin on this.