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

Implement get audio session id (Android) #9

Open
wants to merge 10 commits 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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
.DS_Store
android/build
**/.*
!.gitignore
local.properties
Binary file not shown.
Binary file not shown.
Binary file removed android/.gradle/6.7.1/fileChanges/last-build.bin
Binary file not shown.
Binary file removed android/.gradle/6.7.1/fileHashes/fileHashes.bin
Binary file not shown.
Binary file removed android/.gradle/6.7.1/fileHashes/fileHashes.lock
Binary file not shown.
Binary file not shown.
Empty file.
Binary file removed android/.gradle/6.7.1/javaCompile/classAnalysis.bin
Binary file not shown.
Binary file removed android/.gradle/6.7.1/javaCompile/jarAnalysis.bin
Binary file not shown.
Binary file removed android/.gradle/6.7.1/javaCompile/javaCompile.lock
Binary file not shown.
Binary file removed android/.gradle/6.7.1/javaCompile/taskHistory.bin
Binary file not shown.
Binary file not shown.
2 changes: 0 additions & 2 deletions android/.gradle/buildOutputCleanup/cache.properties

This file was deleted.

Binary file removed android/.gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
Binary file removed android/.gradle/checksums/checksums.lock
Binary file not shown.
Binary file removed android/.gradle/checksums/md5-checksums.bin
Binary file not shown.
Binary file removed android/.gradle/checksums/sha1-checksums.bin
Binary file not shown.
Binary file removed android/.gradle/checksums/sha256-checksums.bin
Binary file not shown.
Binary file removed android/.gradle/checksums/sha512-checksums.bin
Binary file not shown.
Empty file.
Empty file.
8 changes: 0 additions & 8 deletions android/local.properties

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ void _play()
mediaPlayer.start();
}

int _getAudioSessionId(){
return mediaPlayer.getAudioSessionId();
}

int feed(byte[] data) throws Exception
{
throw new Exception("Cannot feed a Media Player");
Expand Down
206 changes: 157 additions & 49 deletions android/src/main/java/com/dooboolab/TauEngine/FlautoPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,25 @@


import android.media.MediaPlayer;
import android.media.audiofx.AudioEffect;
import android.media.audiofx.Equalizer;
import android.media.audiofx.LoudnessEnhancer;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;

import android.media.AudioFocusRequest;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;
import java.lang.Thread;

import com.dooboolab.TauEngine.Flauto.*;
import com.dooboolab.TauEngine.Flauto;

public class FlautoPlayer implements MediaPlayer.OnErrorListener
{
Expand Down Expand Up @@ -102,6 +105,12 @@ public class FlautoPlayer implements MediaPlayer.OnErrorListener
static int currentPlayerID = 0;
private int myPlayerId = 0;

//--------------- EQ related -----------------------//
private List<Object> rawAudioEffects = new ArrayList();
private List<AudioEffect> audioEffects = new ArrayList<AudioEffect>();
private Map<String, AudioEffect> audioEffectsMap = new HashMap<String, AudioEffect>();
private Integer audioSessionId;


static final String ERR_UNKNOWN = "ERR_UNKNOWN";
static final String ERR_PLAYER_IS_NULL = "ERR_PLAYER_IS_NULL";
Expand Down Expand Up @@ -130,6 +139,13 @@ public boolean openPlayer ()
return true;
}

public int getAudioSessionId() throws Exception {
if (player == null) {
throw new Exception("getAudioSessionId() : player is null");
}
return player._getAudioSessionId();
}

public void closePlayer ( )
{
stop();
Expand Down Expand Up @@ -188,7 +204,6 @@ public boolean startPlayer (t_CODEC codec, String fromURI, byte[] dataBuffer, in
}
}


try
{
if (fromURI == null && codec == t_CODEC.pcm16)
Expand Down Expand Up @@ -318,34 +333,32 @@ void setTimer(long duration)
TimerTask task = new TimerTask() {
@Override
public void run() {
mainHandler.post(new Runnable()
mainHandler.post(new Runnable()
{
@Override
public void run()
{
@Override
public void run()
try
{
try
if (player != null)
{
if (player != null)
{

long position = player._getCurrentPosition();
long duration = player._getDuration();
if (position > duration)
{
position = duration;
}
m_callBack.updateProgress(position, duration);
long position = player._getCurrentPosition();
long duration = player._getDuration();
if (position > duration)
{
position = duration;
}
} catch (Exception e)
{
logDebug( "Exception: " + e.toString());
stopPlayer();
m_callBack.updateProgress(position, duration);
}
} catch (Exception e)
{
logDebug( "Exception: " + e.toString());
stopPlayer();
}
});


}
}
});
}
};

mTimer = new Timer();
Expand Down Expand Up @@ -391,34 +404,34 @@ void stop()

public boolean play()
{
if (player == null)
if (player == null)
{
return false;
}
try
{
if (latentVolume >= 0)
{
return false;
setVolume(latentVolume);
}
try
if (latentSpeed >= 0)
{
if (latentVolume >= 0)
{
setVolume(latentVolume);
}
if (latentSpeed >= 0)
{
setSpeed(latentSpeed);
}
if (subsDurationMillis > 0)
setTimer(subsDurationMillis);
if (latentSeek >= 0)
{
seekToPlayer(latentSeek);
}
setSpeed(latentSpeed);
}
if (subsDurationMillis > 0)
setTimer(subsDurationMillis);
if (latentSeek >= 0)
{
seekToPlayer(latentSeek);
}


}catch (Exception e)
{
}catch (Exception e)
{

}
player._play();
return true;
}
player._play();
return true;
}

public boolean isDecoderSupported (t_CODEC codec )
Expand Down Expand Up @@ -561,9 +574,104 @@ public Map<String, Object> getProgress ( )
return dic;
}

// ----------------------------Equalizer related----------------------------------------//

public void initEqualizer(Integer audioSessionId) throws Exception {
System.out.printf("audioSessionId: %d", audioSessionId);
if(audioSessionId == null) {
this.audioSessionId = this.getAudioSessionId();
} else {
this.audioSessionId = audioSessionId;
}

// Init with basic info
this.rawAudioEffects.add(mapOf("type","AndroidEqualizer", "enabled", false));

clearAudioEffects();
if (this.audioSessionId != null) {
for (Object rawAudioEffect : this.rawAudioEffects) {
Map<?, ?> json = (Map<?, ?>)rawAudioEffect;
AudioEffect audioEffect = decodeAudioEffect(rawAudioEffect, this.audioSessionId);
if ((Boolean)json.get("enabled")) {
audioEffect.setEnabled(true);
}
this.audioEffects.add(audioEffect);
this.audioEffectsMap.put((String)json.get("type"), audioEffect);
}
}
}

public void setAudioSessionId(Integer audioSessionId) {
this.audioSessionId = audioSessionId;
}

public void equalizerBandSetGain(int bandIndex, double gain) {
((Equalizer) Objects.requireNonNull(this.audioEffectsMap.get("AndroidEqualizer"))).setBandLevel((short)bandIndex,
(short)(Math.round(gain * 1000.0)));
}

public void audioEffectSetEnabled(String type, boolean enabled) {
Objects.requireNonNull(this.audioEffectsMap.get(type)).setEnabled(enabled);
}

public Map<String, Object> equalizerAudioEffectGetParameters() {
Equalizer equalizer = (Equalizer)this.audioEffectsMap.get("AndroidEqualizer");
ArrayList<Object> rawBands = new ArrayList<>();
for (short i = 0; i < Objects.requireNonNull(equalizer).getNumberOfBands(); i++) {
rawBands.add(mapOf(
"index", i,
"lowerFrequency", (double)equalizer.getBandFreqRange(i)[0] / 1000.0,
"upperFrequency", (double)equalizer.getBandFreqRange(i)[1] / 1000.0,
"centerFrequency", (double)equalizer.getCenterFreq(i) / 1000.0,
"gain", equalizer.getBandLevel(i) / 1000.0
));
}
return mapOf(
"parameters", mapOf(
"minDecibels", equalizer.getBandLevelRange()[0] / 1000.0,
"maxDecibels", equalizer.getBandLevelRange()[1] / 1000.0,
"bands", rawBands
)
);
}

private AudioEffect decodeAudioEffect(final Object json, int audioSessionId) {
Map<?, ?> map = (Map<?, ?>) json;
String type = (String) map.get("type");
switch (Objects.requireNonNull(type)) {
case "AndroidLoudnessEnhancer":
if (Build.VERSION.SDK_INT < 19)
throw new RuntimeException("AndroidLoudnessEnhancer requires minSdkVersion >= 19");
int targetGain = (int) Math.round((((Double) map.get("targetGain")) * 1000.0));
LoudnessEnhancer loudnessEnhancer = new LoudnessEnhancer(audioSessionId);
loudnessEnhancer.setTargetGain(targetGain);
return loudnessEnhancer;
case "AndroidEqualizer":
return new Equalizer(0, audioSessionId);
default:
throw new IllegalArgumentException("Unknown AudioEffect type: " + map.get("type"));
}
}

private void clearAudioEffects() {
for (Iterator<AudioEffect> it = this.audioEffects.iterator(); it.hasNext();) {
AudioEffect audioEffect = it.next();
audioEffect.release();
it.remove();
}
this.audioEffectsMap.clear();
}

static Map<String, Object> mapOf(Object... args) {
Map<String, Object> map = new HashMap<>();
for (int i = 0; i < args.length; i += 2) {
map.put((String)args[i], args[i + 1]);
}
return map;
}


void logDebug (String msg)
void logDebug (String msg)
{
m_callBack.log ( t_LOG_LEVEL.DBG , msg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ void _stop()
blockThread = null;
}

int _getAudioSessionId() {
return sessionId;
}

void _finish()
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ void _stop()
}
}

int _getAudioSessionId() {
return sessionId;
}

void _finish()
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ abstract class FlautoPlayerEngineInterface
abstract long _getCurrentPosition();
abstract int feed(byte[] data) throws Exception;
abstract void _play();
abstract int _getAudioSessionId() throws Exception;


}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ void _stop() {

}


int _getAudioSessionId() {
return mediaPlayer.getAudioSessionId();
}

void _pausePlayer() throws Exception {
if (mediaPlayer == null) {
Expand Down
3 changes: 3 additions & 0 deletions ios/Classes/FlautoPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@
- (long)getPosition;
- (long)getDuration;
- (void)logDebug: (NSString*)msg;
- (void)initEqualizer:(NSDictionary*) params;
- (void)enableEqualizer:(bool)enable;
- (void) setEqualizerBandGain: (int) bandIndex gain: (float) gain;



Expand Down
Loading