1
0
mirror of https://github.com/danog/termux-api.git synced 2024-11-26 20:04:42 +01:00

AudioAPI: avoid redundant code (#134)

This commit is contained in:
tomty89 2018-03-23 07:41:42 +08:00 committed by Fredrik Fornwall
parent 91b8ed4249
commit 9552c418ba

View File

@ -15,71 +15,48 @@ public class AudioAPI {
static void onReceive(TermuxApiReceiver apiReceiver, final Context context, Intent intent) {
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
AudioTrack at = new AudioTrack(
final String SampleRate = am.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE);
final String framesPerBuffer = am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER);
final String AudioUnprocessed = am.getProperty(AudioManager.PROPERTY_SUPPORT_AUDIO_SOURCE_UNPROCESSED);
final int volume_level = am.getStreamVolume(AudioManager.STREAM_MUSIC);
final int maxvolume_level = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
final boolean bluetootha2dp = am.isBluetoothA2dpOn();
final boolean wiredhs = am.isWiredHeadsetOn();
final int nativeoutput = AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_MUSIC);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
AudioTrack at = new AudioTrack(
new AudioAttributes.Builder()
.setFlags(256) // FLAG_LOW_LATENCY
.build(),
.setFlags(256) // FLAG_LOW_LATENCY
.build(),
new AudioFormat.Builder()
.build(),
.build(),
4, // bytes; i.e. one 16bit 2ch frame
AudioTrack.MODE_STREAM,
AudioManager.AUDIO_SESSION_ID_GENERATE
);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
final String SampleRate = am.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE);
final String framesPerBuffer = am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER);
final String AudioUnprocessed = am.getProperty(AudioManager.PROPERTY_SUPPORT_AUDIO_SOURCE_UNPROCESSED);
final int nativeoutput = AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_MUSIC);
final int volume_level = am.getStreamVolume(AudioManager.STREAM_MUSIC);
final int maxvolume_level = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//final int bc = at.getBufferCapacityInFrames(); only available api 24 and up and is
// always returns same value as the initial getbuffersizeinframes.
);
final int bs = at.getBufferSizeInFrames();
final boolean bluetootha2dp = am.isBluetoothA2dpOn();
final boolean wiredhs = am.isWiredHeadsetOn();
//final int bc = at.getBufferCapacityInFrames(); only available api 24 and up and apparently
// always returns same value as the initial getbuffersizeinframes.
at.release();
}
ResultReturner.returnData(apiReceiver, intent, new ResultReturner.ResultJsonWriter() {
public void writeJson(JsonWriter out) throws Exception {
out.beginObject();
out.name("PROPERTY_OUTPUT_SAMPLE_RATE").value(SampleRate);
out.name("PROPERTY_OUTPUT_FRAMES_PER_BUFFER").value(framesPerBuffer);
out.name("PROPERTY_SUPPORT_AUDIO_SOURCE_UNPROCESSED").value(AudioUnprocessed);
out.name("STREAM_MUSIC_VOLUME").value(volume_level);
out.name("STREAM_MUSIC_MAXVOLUME").value(maxvolume_level);
out.name("CURRENT_NATIVE_OUTPUT_SAMPLERATE").value(nativeoutput);
ResultReturner.returnData(apiReceiver, intent, new ResultReturner.ResultJsonWriter() {
public void writeJson(JsonWriter out) throws Exception {
out.beginObject();
out.name("PROPERTY_OUTPUT_SAMPLE_RATE").value(SampleRate);
out.name("PROPERTY_OUTPUT_FRAMES_PER_BUFFER").value(framesPerBuffer);
out.name("PROPERTY_SUPPORT_AUDIO_SOURCE_UNPROCESSED").value(AudioUnprocessed);
out.name("STREAM_MUSIC_VOLUME").value(volume_level);
out.name("STREAM_MUSIC_MAXVOLUME").value(maxvolume_level);
out.name("BLUETOOTH_A2DP_IS_ON").value(bluetootha2dp);
out.name("WIREDHEADSET_IS_CONNECTED").value(wiredhs);
out.name("CURRENT_NATIVE_OUTPUT_SAMPLERATE").value(nativeoutput);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
out.name("AUDIOTRACK_BUFFER_SIZE_IN_FRAMES").value(bs);
out.name("BLUETOOTH_A2DP_IS_ON").value(bluetootha2dp);
out.name("WIREDHEADSET_IS_CONNECTED").value(wiredhs);
out.endObject();
}
});
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
final String SampleRate = am.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE);
final String framesPerBuffer = am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER);
final String AudioUnprocessed = am.getProperty(AudioManager.PROPERTY_SUPPORT_AUDIO_SOURCE_UNPROCESSED);
final int nativeoutput = AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_MUSIC);
final int volume_level = am.getStreamVolume(AudioManager.STREAM_MUSIC);
final int maxvolume_level = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
final boolean WiredHs = am.isWiredHeadsetOn();
final boolean bluetootha2dp = am.isBluetoothA2dpOn();
at.release();
ResultReturner.returnData(apiReceiver, intent, new ResultReturner.ResultJsonWriter() {
public void writeJson(JsonWriter out) throws Exception {
out.beginObject();
out.name("PROPERTY_OUTPUT_SAMPLE_RATE").value(SampleRate);
out.name("PROPERTY_OUTPUT_FRAMES_PER_BUFFER").value(framesPerBuffer);
out.name("PROPERTY_SUPPORT_AUDIO_SOURCE_UNPROCESSED").value(AudioUnprocessed);
out.name("STREAM_MUSIC_VOLUME").value(volume_level);
out.name("STREAM_MUSIC_MAXVOLUME").value(maxvolume_level);
out.name("CURRENT_NATIVE_OUTPUT_SAMPLERATE").value(nativeoutput);
out.name("BLUETOOTH_A2DP_IS_ON").value(bluetootha2dp);
out.name("WIREDHEADSET_IS_CONNECTED").value(WiredHs);
out.endObject();
}
});
}
out.endObject();
}
});
}
}