[Android] [PATCH] MediaPlayer: use libvlc equalizer api
Thomas Guillem
thomas at gllm.fr
Fri Jul 3 17:31:13 CEST 2015
There is an issue on the jni part, will check that on monday.
---
libvlc/jni/Android.mk | 1 -
libvlc/jni/libvlcjni-equalizer.c | 113 -------------
libvlc/jni/libvlcjni-mediaplayer.c | 167 ++++++++++++++++++
libvlc/jni/libvlcjni.c | 7 +
libvlc/jni/utils.h | 4 +
libvlc/src/org/videolan/libvlc/MediaPlayer.java | 188 +++++++++++++++++++--
.../src/org/videolan/vlc/PlaybackService.java | 59 ++++++-
.../videolan/vlc/gui/audio/EqualizerFragment.java | 10 +-
8 files changed, 404 insertions(+), 145 deletions(-)
delete mode 100644 libvlc/jni/libvlcjni-equalizer.c
diff --git a/libvlc/jni/Android.mk b/libvlc/jni/Android.mk
index 22a7f8e..eb3df57 100644
--- a/libvlc/jni/Android.mk
+++ b/libvlc/jni/Android.mk
@@ -6,7 +6,6 @@ LOCAL_MODULE := libvlcjni
LOCAL_SRC_FILES := libvlcjni.c
LOCAL_SRC_FILES += libvlcjni-mediaplayer.c
-LOCAL_SRC_FILES += libvlcjni-equalizer.c
LOCAL_SRC_FILES += libvlcjni-vlcobject.c
LOCAL_SRC_FILES += libvlcjni-media.c libvlcjni-medialist.c libvlcjni-mediadiscoverer.c
LOCAL_SRC_FILES += native_crash_handler.c thumbnailer.c
diff --git a/libvlc/jni/libvlcjni-equalizer.c b/libvlc/jni/libvlcjni-equalizer.c
deleted file mode 100644
index f842506..0000000
--- a/libvlc/jni/libvlcjni-equalizer.c
+++ /dev/null
@@ -1,113 +0,0 @@
-/*****************************************************************************
- * libvlcjni-equalizer.c
- *****************************************************************************
- * Copyright © 2010-2013 VLC authors and VideoLAN
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation; either version 2.1 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
- *****************************************************************************/
-
-#include "libvlcjni-vlcobject.h"
-
-/**
- * return band list as float[]
- */
-jfloatArray Java_org_videolan_libvlc_MediaPlayer_getBands(JNIEnv *env, jobject thiz)
-{
- unsigned count = libvlc_audio_equalizer_get_band_count();
- jfloatArray bands = (*env)->NewFloatArray(env, count);
-
- for (unsigned i = 0; i < count; ++i)
- {
- jfloat band = libvlc_audio_equalizer_get_band_frequency(i);
- (*env)->SetFloatArrayRegion(env, bands, i, 1, &band);
- }
- return bands;
-}
-
-/**
- * return preset list as String[]
- */
-jobjectArray Java_org_videolan_libvlc_MediaPlayer_getPresets(JNIEnv *env, jobject thiz)
-{
- unsigned count = libvlc_audio_equalizer_get_preset_count();
- jclass stringClass = (*env)->FindClass(env, "java/lang/String");
- jobjectArray presets = (*env)->NewObjectArray(env, count, stringClass, NULL);
-
- for (unsigned i = 0; i < count; ++i)
- {
- const char *name = libvlc_audio_equalizer_get_preset_name(i);
- jstring jname = (*env)->NewStringUTF(env, name);
- (*env)->SetObjectArrayElement(env, presets, i, jname);
- }
- return presets;
-}
-
-/**
- * return preset n° <index> as float[] (first element is preamp, then bands)
- */
-jfloatArray Java_org_videolan_libvlc_MediaPlayer_getPreset(JNIEnv *env, jobject thiz, jint index)
-{
- unsigned count = libvlc_audio_equalizer_get_band_count();
- jfloatArray array = (*env)->NewFloatArray(env, count + 1);
- libvlc_equalizer_t *p_equalizer = libvlc_audio_equalizer_new_from_preset(index);
- if (p_equalizer != NULL)
- {
- jfloat preamp = libvlc_audio_equalizer_get_preamp(p_equalizer);
- (*env)->SetFloatArrayRegion(env, array, 0, 1, &preamp);
-
- for (unsigned i = 0; i < count; ++i)
- {
- jfloat band = libvlc_audio_equalizer_get_amp_at_index(p_equalizer, i);
- (*env)->SetFloatArrayRegion(env, array, i + 1, 1, &band);
- }
- libvlc_audio_equalizer_release(p_equalizer);
- }
- return array;
-}
-
-/**
- * apply equalizer settings (param bands is float[] (first element is preamp, then bands))
- */
-//"--audio-filter=equalizer", "--equalizer-bands=-3.5 -4.5 -1 0 0 5 8 8 8 8",
-jint Java_org_videolan_libvlc_MediaPlayer_nativeSetEqualizer(JNIEnv *env, jobject thiz, jfloatArray bands)
-{
- jint res = -1;
- vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);
-
- if (!p_obj)
- return -1;
-
- if (bands == NULL)
- return libvlc_media_player_set_equalizer(p_obj->u.p_mp, NULL);
-
- jfloat *cbands = (*env)->GetFloatArrayElements(env, bands, NULL);
- if (cbands == NULL)
- return res;
-
- jsize input_count = (*env)->GetArrayLength(env, bands);
- unsigned band_count = libvlc_audio_equalizer_get_band_count();
- if (input_count == band_count+1) // first item is preamp
- {
- libvlc_equalizer_t *p_equalizer = libvlc_audio_equalizer_new();
- libvlc_audio_equalizer_set_preamp(p_equalizer, cbands[0]);
- for (unsigned i = 0; i < band_count; ++i)
- {
- libvlc_audio_equalizer_set_amp_at_index(p_equalizer, cbands[i+1], i);
- }
- res = libvlc_media_player_set_equalizer(p_obj->u.p_mp, p_equalizer);
- libvlc_audio_equalizer_release(p_equalizer);
- }
- return res;
-}
diff --git a/libvlc/jni/libvlcjni-mediaplayer.c b/libvlc/jni/libvlcjni-mediaplayer.c
index 44da76c..b6ac209 100644
--- a/libvlc/jni/libvlcjni-mediaplayer.c
+++ b/libvlc/jni/libvlcjni-mediaplayer.c
@@ -47,6 +47,25 @@ struct vlcjni_object_sys
jobject jwindow;
};
+static libvlc_equalizer_t *
+Equalizer_getInstance(JNIEnv *env, jobject thiz)
+{
+ intptr_t i_ptr = (intptr_t)
+ (*env)->GetLongField(env, thiz,
+ fields.MediaPlayer.Equalizer.mInstanceID);
+ if (!i_ptr)
+ throw_IllegalStateException(env, "can't get Equalizer instance");
+ return (libvlc_equalizer_t*) i_ptr;
+}
+
+static void
+VLCJniObject_setInstance(JNIEnv *env, jobject thiz, libvlc_equalizer_t *p_eq)
+{
+ (*env)->SetLongField(env, thiz,
+ fields.MediaPlayer.Equalizer.mInstanceID,
+ (jlong)(intptr_t)p_eq);
+}
+
static bool
MediaPlayer_event_cb(vlcjni_object *p_obj, const libvlc_event_t *p_ev,
java_event *p_java_event)
@@ -849,3 +868,151 @@ Java_org_videolan_libvlc_MediaPlayer_nativeSetSubtitleFile(JNIEnv *env,
(*env)->ReleaseStringUTFChars(env, jpath, psz_path);
return ret;
}
+
+jboolean
+Java_org_videolan_libvlc_MediaPlayer_nativeSetEqualizer(JNIEnv *env,
+ jobject thiz,
+ jobject jequalizer)
+{
+ vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);
+ libvlc_equalizer_t *p_eq = NULL;
+
+ if (!p_obj)
+ return false;
+
+ if (jequalizer)
+ {
+ p_eq = Equalizer_getInstance(env, jequalizer);
+ if (!p_eq)
+ return false;
+ }
+
+ return libvlc_media_player_set_equalizer(p_obj->u.p_mp, p_eq) == 0 ? true: false;
+}
+
+jint
+Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeGetPresetCount(JNIEnv *env)
+{
+ return libvlc_audio_equalizer_get_preset_count();
+}
+
+jstring
+Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeGetPresetName(JNIEnv *env,
+ jint index)
+{
+ const char *psz_name;
+
+ if (index < 0)
+ {
+ throw_IllegalArgumentException(env, "index invalid");
+ return NULL;
+ }
+
+ psz_name = libvlc_audio_equalizer_get_preset_name(index);
+
+ return psz_name ? (*env)->NewStringUTF(env, psz_name) : NULL;
+
+}
+
+jint
+Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeGetBandCount(JNIEnv *env)
+{
+ return libvlc_audio_equalizer_get_band_count();
+}
+
+jfloat
+Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeGetBandFrequency(JNIEnv *env,
+ jint index)
+{
+fprintf(stderr, "index: %d\n", index);
+ if (index < 0)
+ {
+ throw_IllegalArgumentException(env, "index invalid");
+ return 0.0;
+ }
+
+ return libvlc_audio_equalizer_get_band_frequency(index);
+}
+
+void
+Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeNew(JNIEnv *env,
+ jobject thiz)
+{
+ libvlc_equalizer_t *p_eq = libvlc_audio_equalizer_new();
+ if (!p_eq)
+ throw_IllegalStateException(env, "can't create Equalizer instance");
+
+ VLCJniObject_setInstance(env, thiz, p_eq);
+}
+
+void
+Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeNewFromPreset(JNIEnv *env,
+ jobject thiz,
+ jint index)
+{
+ libvlc_equalizer_t *p_eq = libvlc_audio_equalizer_new_from_preset(index);
+ if (!p_eq)
+ throw_IllegalStateException(env, "can't create Equalizer instance");
+
+ VLCJniObject_setInstance(env, thiz, p_eq);
+}
+
+void
+Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeRelease(JNIEnv *env,
+ jobject thiz)
+{
+ libvlc_equalizer_t *p_eq = Equalizer_getInstance(env, thiz);
+ if (!p_eq)
+ return;
+
+ libvlc_audio_equalizer_release(p_eq);
+ VLCJniObject_setInstance(env, thiz, NULL);
+}
+
+jfloat
+Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeGetPreAmp(JNIEnv *env,
+ jobject thiz)
+{
+ libvlc_equalizer_t *p_eq = Equalizer_getInstance(env, thiz);
+ if (!p_eq)
+ return 0.0;
+
+ return libvlc_audio_equalizer_get_preamp(p_eq);
+}
+
+jboolean
+Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeSetPreAmp(JNIEnv *env,
+ jobject thiz,
+ jfloat preamp)
+{
+ libvlc_equalizer_t *p_eq = Equalizer_getInstance(env, thiz);
+ if (!p_eq)
+ return false;
+
+ return libvlc_audio_equalizer_set_preamp(p_eq, preamp) == 0 ? true : false;
+}
+
+jfloat
+Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeGetAmp(JNIEnv *env,
+ jobject thiz,
+ jint index)
+{
+ libvlc_equalizer_t *p_eq = Equalizer_getInstance(env, thiz);
+ if (!p_eq)
+ return 0.0;
+
+ return libvlc_audio_equalizer_get_amp_at_index(p_eq, index);
+}
+
+jboolean
+Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeSetAmp(JNIEnv *env,
+ jobject thiz,
+ jint index,
+ jfloat amp)
+{
+ libvlc_equalizer_t *p_eq = Equalizer_getInstance(env, thiz);
+ if (!p_eq)
+ return false;
+
+ return libvlc_audio_equalizer_set_amp_at_index(p_eq, amp, index) == 0 ? true : false;
+}
diff --git a/libvlc/jni/libvlcjni.c b/libvlc/jni/libvlcjni.c
index 3303533..e6215af 100644
--- a/libvlc/jni/libvlcjni.c
+++ b/libvlc/jni/libvlcjni.c
@@ -184,6 +184,8 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved)
"org/videolan/libvlc/MediaPlayer$Chapter", true);
GET_CLASS(fields.MediaPlayer.TrackDescription.clazz,
"org/videolan/libvlc/MediaPlayer$TrackDescription", true);
+ GET_CLASS(fields.MediaPlayer.Equalizer.clazz,
+ "org/videolan/libvlc/MediaPlayer$Equalizer", true);
GET_ID(GetStaticMethodID,
fields.LibVLC.onNativeCrashID,
@@ -195,6 +197,11 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved)
fields.VLCObject.clazz,
"mInstance", "J");
+ GET_ID(GetFieldID,
+ fields.MediaPlayer.Equalizer.mInstanceID,
+ fields.MediaPlayer.Equalizer.clazz,
+ "mInstance", "J");
+
GET_ID(GetMethodID,
fields.VLCObject.dispatchEventFromNativeID,
fields.VLCObject.clazz,
diff --git a/libvlc/jni/utils.h b/libvlc/jni/utils.h
index 1a0a946..11d8859 100644
--- a/libvlc/jni/utils.h
+++ b/libvlc/jni/utils.h
@@ -71,6 +71,10 @@ struct fields {
struct {
jclass clazz;
} TrackDescription;
+ struct {
+ jclass clazz;
+ jfieldID mInstanceID;
+ } Equalizer;
jclass clazz;
jmethodID createTitleFromNativeID;
diff --git a/libvlc/src/org/videolan/libvlc/MediaPlayer.java b/libvlc/src/org/videolan/libvlc/MediaPlayer.java
index 1aa154d..4ffd600 100644
--- a/libvlc/src/org/videolan/libvlc/MediaPlayer.java
+++ b/libvlc/src/org/videolan/libvlc/MediaPlayer.java
@@ -175,6 +175,149 @@ public class MediaPlayer extends VLCObject<MediaPlayer.Event> {
return new TrackDescription(id, name);
}
+ public static class Equalizer {
+ @SuppressWarnings("unused") /* Used from JNI */
+ private long mInstance;
+
+ private Equalizer() {
+ nativeNew();
+ }
+
+ private Equalizer(int index) {
+ nativeNewFromPreset(index);
+ }
+
+ /**
+ * Create a new default equalizer, with all frequency values zeroed.
+ * The new equalizer can subsequently be applied to a media player by invoking
+ * {@link MediaPlayer#setEqualizer}.
+ *
+ * @return equalizer or NULL on error, should be released via {@link Equalizer#release()}
+ * when it's not needed anymore.
+ */
+ public static Equalizer create() {
+ return new Equalizer();
+ }
+
+ /**
+ * Create a new equalizer, with initial frequency values copied from an existing
+ * preset.
+ * The new equalizer can subsequently be applied to a media player by invoking
+ * {@link MediaPlayer#setEqualizer}.
+ *
+ * @return equalizer or NULL on error, should be released via {@link Equalizer#release()}
+ * when it's not needed anymore.
+ */
+ public static Equalizer createFromPreset(int index) {
+ return new Equalizer(index);
+ }
+
+ /**
+ * Release a previously created equalizer instance.
+ */
+
+ public void release() {
+ nativeRelease();
+ }
+
+ /**
+ * Get the number of equalizer presets.
+ */
+ public static int getPresetCount() {
+ return nativeGetPresetCount();
+ }
+
+ /**
+ * Get the name of a particular equalizer preset.
+ * This name can be used, for example, to prepare a preset label or menu in a user
+ * interface.
+ *
+ * @param index index of the preset, counting from zero.
+ * @return preset name, or NULL if there is no such preset
+ */
+
+ public static String getPresetName(int index) {
+ return nativeGetPresetName(index);
+ }
+
+ /**
+ * Get the number of distinct frequency bands for an equalizer.
+ */
+ public static int getBandCount() {
+ return nativeGetBandCount();
+ }
+
+ /**
+ * Get a particular equalizer band frequency.
+ * This value can be used, for example, to create a label for an equalizer band control
+ * in a user interface.
+ *
+ * @param index index of the band, counting from zero.
+ * @return equalizer band frequency (Hz), or -1 if there is no such band
+ */
+ public static float getBandFrequency(int index) {
+ return nativeGetBandFrequency(index);
+ }
+
+ /**
+ * Get the current pre-amplification value from an equalizer.
+ *
+ * @return preamp value (Hz)
+ */
+ public float getPreAmp() {
+ return nativeGetPreAmp();
+ }
+
+ /**
+ * Set a new pre-amplification value for an equalizer.
+ * The new equalizer settings are subsequently applied to a media player by invoking
+ * {@link MediaPlayer#setEqualizer}.
+ * The supplied amplification value will be clamped to the -20.0 to +20.0 range.
+ *
+ * @param preamp value (-20.0 to 20.0 Hz)
+ * @return true on success.
+ */
+ public boolean setPreAmp(float preamp) {
+ return nativeSetPreAmp(preamp);
+ }
+
+ /**
+ * Get the amplification value for a particular equalizer frequency band.
+ *
+ * @param index counting from zero, of the frequency band to get.
+ * @return amplification value (Hz); NaN if there is no such frequency band.
+ */
+ public float getAmp(int index) {
+ return nativeGetAmp(index);
+ }
+
+ /**
+ * Set a new amplification value for a particular equalizer frequency band.
+ * The new equalizer settings are subsequently applied to a media player by invoking
+ * {@link MediaPlayer#setEqualizer}.
+ * The supplied amplification value will be clamped to the -20.0 to +20.0 range.
+ *
+ * @param index counting from zero, of the frequency band to set.
+ * @param amp amplification value (-20.0 to 20.0 Hz).
+ * \return true on success.
+ */
+ public boolean setAmp(int index, float amp) {
+ return nativeSetAmp(index, amp);
+ }
+
+ private static native int nativeGetPresetCount();
+ private static native String nativeGetPresetName(int index);
+ private static native int nativeGetBandCount();
+ private static native float nativeGetBandFrequency(int index);
+ private native void nativeNew();
+ private native void nativeNewFromPreset(int index);
+ private native void nativeRelease();
+ private native float nativeGetPreAmp();
+ private native boolean nativeSetPreAmp(float preamp);
+ private native float nativeGetAmp(int index);
+ private native boolean nativeSetAmp(int index, float amp);
+ }
+
private Media mMedia = null;
private boolean mPlaying = false;
private boolean mPlayRequested = false;
@@ -191,9 +334,9 @@ public class MediaPlayer extends VLCObject<MediaPlayer.Event> {
public synchronized void onSurfacesDestroyed(AWindow vout) {
if (mVoutCount > 0)
setVideoTrack(-1);
- /* Wait for Vout destruction (mVoutCount = 0) in order to be sure that the surface is not
- * used after leaving this callback. This shouldn't be needed when using MediaCodec or
- * AndroidWindow (i.e. after Android 2.3) since the surface is ref-counted */
+ /* Wait for Vout destruction (mVoutCount = 0) in order to be sure that the surface is not
+ * used after leaving this callback. This shouldn't be needed when using MediaCodec or
+ * AndroidWindow (i.e. after Android 2.3) since the surface is ref-counted */
while (mVoutCount > 0) {
try {
wait();
@@ -450,22 +593,39 @@ public class MediaPlayer extends VLCObject<MediaPlayer.Event> {
}
/**
- * Set a new video subtitle file.
+ * Apply new equalizer settings to a media player.
*
- * @param path local path.
+ * The equalizer is first created by invoking {@link Equalizer#create()} or
+ * {@link Equalizer#createFromPreset(int)}}.
+ *
+ * It is possible to apply new equalizer settings to a media player whether the media
+ * player is currently playing media or not.
+ *
+ * Invoking this method will immediately apply the new equalizer settings to the audio
+ * output of the currently playing media if there is any.
+ *
+ * If there is no currently playing media, the new equalizer settings will be applied
+ * later if and when new media is played.
+ *
+ * Equalizer settings will automatically be applied to subsequently played media.
+ *
+ * To disable the equalizer for a media player invoke this method passing null.
+ *
+ * @param equalizer can be released with {@link Equalizer#release()} after this call.
* @return true on success.
*/
- public synchronized boolean setSubtitleFile(String path) {
- return nativeSetSubtitleFile(path);
+ public synchronized boolean setEqualizer(Equalizer equalizer) {
+ return nativeSetEqualizer(equalizer);
}
/**
- * TODO: this doesn't respect API
+ * Set a new video subtitle file.
*
- * @param bands
+ * @param path local path.
+ * @return true on success.
*/
- public synchronized void setEqualizer(float[] bands) {
- nativeSetEqualizer(bands);
+ public synchronized boolean setSubtitleFile(String path) {
+ return nativeSetSubtitleFile(path);
}
/**
@@ -550,10 +710,6 @@ public class MediaPlayer extends VLCObject<MediaPlayer.Event> {
public native void setChapter(int chapter);
public native void navigate(int navigate);
- public native float[] getBands();
- public native String[] getPresets();
- public native float[] getPreset(int index);
-
public synchronized void setEventListener(EventListener listener) {
super.setEventListener(listener);
}
@@ -599,7 +755,6 @@ public class MediaPlayer extends VLCObject<MediaPlayer.Event> {
private native void nativePlay();
private native void nativeStop();
private native void nativeSetVideoTitleDisplay(int position, int timeout);
- private native void nativeSetEqualizer(float[] bands);
private native boolean nativeSetAudioOutput(String aout);
private native Title[] nativeGetTitles();
private native Chapter[] nativeGetChapters(int title);
@@ -620,4 +775,5 @@ public class MediaPlayer extends VLCObject<MediaPlayer.Event> {
private native long nativeGetSpuDelay();
private native boolean nativeSetSpuDelay(long delay);
private native boolean nativeSetSubtitleFile(String path);
+ private native boolean nativeSetEqualizer(Equalizer equalizer);
}
diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.java b/vlc-android/src/org/videolan/vlc/PlaybackService.java
index 4828114..2621b09 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.java
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.java
@@ -1521,7 +1521,7 @@ public class PlaybackService extends Service {
media.setEventListener(mMediaListener);
mMediaPlayer.setMedia(media);
media.release();
- mMediaPlayer.setEqualizer(VLCOptions.getEqualizer(this));
+ setEqualizer(VLCOptions.getEqualizer(this));
mMediaPlayer.setVideoTitleDisplay(MediaPlayer.Position.Disable, 0);
changeAudioFocus(true);
mMediaPlayer.setEventListener(mMediaPlayerListener);
@@ -1805,22 +1805,65 @@ public class PlaybackService extends Service {
}
@MainThread
- public float[] getBands() {
- return mMediaPlayer.getBands();
+ public float[] getEqualizerBands() {
+ final int count = MediaPlayer.Equalizer.getBandCount();
+ if (count <= 0)
+ return null;
+
+ final float[] bands = new float[count];
+ for (int i = 0; i < count; ++i)
+ bands[i] = MediaPlayer.Equalizer.getBandFrequency(i);
+
+ return bands;
}
+
+ /**
+ * apply equalizer settings (param bands is float[] (first element is preamp, then bands))
+ * @param bands
+ */
@MainThread
public void setEqualizer(float[] bands) {
- mMediaPlayer.setEqualizer(bands);
+ if (bands == null) {
+ mMediaPlayer.setEqualizer(null);
+ } else {
+ final MediaPlayer.Equalizer eq = MediaPlayer.Equalizer.create();
+ eq.setPreAmp(bands[0]);
+ for (int i = 0; i < bands.length - 1; ++i)
+ eq.setAmp(i, bands[i + 1]);
+ mMediaPlayer.setEqualizer(eq);
+ eq.release();
+ }
}
@MainThread
- public String[] getPresets() {
- return mMediaPlayer.getPresets();
+ public String[] getEqualizerPresets() {
+ final int count = MediaPlayer.Equalizer.getPresetCount();
+ if (count <= 0)
+ return null;
+ final String [] presets = new String[count];
+ for (int i = 0; i < count; ++i) {
+ presets[i] = MediaPlayer.Equalizer.getPresetName(i);
+ }
+ return presets;
}
+ /**
+ * return preset n° <index> as float[] (first element is preamp, then bands)
+ */
@MainThread
- public float[] getPreset(int index) {
- return mMediaPlayer.getPreset(index);
+ public float[] getEqualizerPresetBands(int index) {
+ final int count = MediaPlayer.Equalizer.getBandCount();
+
+ if (count <= 0)
+ return null;
+ final MediaPlayer.Equalizer eq = MediaPlayer.Equalizer.createFromPreset(index);
+
+ final float[] bands = new float[count + 1];
+ bands[0] = eq.getPreAmp();
+ for (int i = 0; i < count; ++i)
+ bands[i+1] = eq.getAmp(i);
+ eq.release();
+ return bands;
}
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/EqualizerFragment.java b/vlc-android/src/org/videolan/vlc/gui/audio/EqualizerFragment.java
index dfbda3f..5e07dc7 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/EqualizerFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/EqualizerFragment.java
@@ -41,12 +41,8 @@ import android.widget.Spinner;
import org.videolan.vlc.PlaybackService;
import org.videolan.vlc.R;
-import org.videolan.vlc.VLCApplication;
import org.videolan.vlc.gui.PlaybackServiceFragment;
import org.videolan.vlc.interfaces.OnEqualizerBarChangeListener;
-import org.videolan.vlc.util.Preferences;
-import org.videolan.vlc.util.Util;
-import org.videolan.vlc.util.VLCInstance;
import org.videolan.vlc.util.VLCOptions;
import org.videolan.vlc.widget.EqualizerBar;
@@ -98,8 +94,8 @@ public class EqualizerFragment extends PlaybackServiceFragment {
final float[] bands;
final String[] presets;
- bands = mService.getBands();
- presets = mService.getPresets();
+ bands = mService.getEqualizerBands();
+ presets = mService.getEqualizerPresets();
final float[] equalizerOption = VLCOptions.getEqualizer(context);
if (equalizer == null)
equalizer = equalizerOption;
@@ -166,7 +162,7 @@ public class EqualizerFragment extends PlaybackServiceFragment {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (mService == null)
return;
- float[] preset = mService.getPreset(pos);
+ float[] preset = mService.getEqualizerPresetBands(pos);
if (preset == null)
return;
--
2.1.4
More information about the Android
mailing list