Skip to content

Commit

Permalink
Use dummy Audio implementations when audio is disabled in config on i…
Browse files Browse the repository at this point in the history
…OS and Android
  • Loading branch information
obigu committed Dec 27, 2023
1 parent 2b691f7 commit 49b7b45
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,10 @@ public Handler getHandler () {

@Override
public AndroidAudio createAudio (Context context, AndroidApplicationConfiguration config) {
return new DefaultAndroidAudio(context, config);
if (!config.disableAudio)
return new DefaultAndroidAudio(context, config);
else
return new DisabledAndroidAudio();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,10 @@ public Handler getHandler () {

@Override
public AndroidAudio createAudio (Context context, AndroidApplicationConfiguration config) {
return new DefaultAndroidAudio(context, config);
if (!config.disableAudio)
return new DefaultAndroidAudio(context, config);
else
return new DisabledAndroidAudio();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,10 @@ public Handler getHandler () {

@Override
public AndroidAudio createAudio (Context context, AndroidApplicationConfiguration config) {
return new DefaultAndroidAudio(context, config);
if (!config.disableAudio)
return new DefaultAndroidAudio(context, config);
else
return new DisabledAndroidAudio();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,10 @@ public Handler getHandler () {

@Override
public AndroidAudio createAudio (Context context, AndroidApplicationConfiguration config) {
return new DefaultAndroidAudio(context, config);
if (!config.disableAudio)
return new DefaultAndroidAudio(context, config);
else
return new DisabledAndroidAudio();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@ public class AsynchronousAndroidAudio extends DefaultAndroidAudio {

public AsynchronousAndroidAudio (Context context, AndroidApplicationConfiguration config) {
super(context, config);
if (!config.disableAudio) {
handlerThread = new HandlerThread("libGDX Sound Management");
handlerThread.start();
handler = new Handler(handlerThread.getLooper());
} else {
handler = null;
handlerThread = null;
}
handlerThread = new HandlerThread("libGDX Sound Management");
handlerThread.start();
handler = new Handler(handlerThread.getLooper());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,24 @@ public class DefaultAndroidAudio implements AndroidAudio {
private final List<AndroidMusic> musics = new ArrayList<AndroidMusic>();

public DefaultAndroidAudio (Context context, AndroidApplicationConfiguration config) {
if (!config.disableAudio) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AudioAttributes audioAttrib = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).build();
soundPool = new SoundPool.Builder().setAudioAttributes(audioAttrib).setMaxStreams(config.maxSimultaneousSounds)
.build();
} else {
soundPool = new SoundPool(config.maxSimultaneousSounds, AudioManager.STREAM_MUSIC, 0);// srcQuality: the sample-rate
// converter quality. Currently
// has no effect. Use 0 for the
// default.
}
manager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
if (context instanceof Activity) {
((Activity)context).setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AudioAttributes audioAttrib = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).build();
soundPool = new SoundPool.Builder().setAudioAttributes(audioAttrib).setMaxStreams(config.maxSimultaneousSounds).build();
} else {
soundPool = null;
manager = null;
soundPool = new SoundPool(config.maxSimultaneousSounds, AudioManager.STREAM_MUSIC, 0);// srcQuality: the sample-rate
// converter quality. Currently
// has no effect. Use 0 for the
// default.
}
manager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
if (context instanceof Activity) {
((Activity)context).setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
}

@Override
public void pause () {
if (soundPool == null) {
return;
}
synchronized (musics) {
for (AndroidMusic music : musics) {
if (music.isPlaying()) {
Expand All @@ -89,9 +80,6 @@ public void pause () {

@Override
public void resume () {
if (soundPool == null) {
return;
}
synchronized (musics) {
for (int i = 0; i < musics.size(); i++) {
if (musics.get(i).wasPlaying) musics.get(i).play();
Expand All @@ -103,18 +91,12 @@ public void resume () {
/** {@inheritDoc} */
@Override
public AudioDevice newAudioDevice (int samplingRate, boolean isMono) {
if (soundPool == null) {
throw new GdxRuntimeException("Android audio is not enabled by the application config.");
}
return new AndroidAudioDevice(samplingRate, isMono);
}

/** {@inheritDoc} */
@Override
public Music newMusic (FileHandle file) {
if (soundPool == null) {
throw new GdxRuntimeException("Android audio is not enabled by the application config.");
}
AndroidFileHandle aHandle = (AndroidFileHandle)file;

MediaPlayer mediaPlayer = createMediaPlayer();
Expand Down Expand Up @@ -167,10 +149,6 @@ public String[] getAvailableOutputDevices () {
*
* @see Audio#newMusic(FileHandle) */
public Music newMusic (FileDescriptor fd) {
if (soundPool == null) {
throw new GdxRuntimeException("Android audio is not enabled by the application config.");
}

MediaPlayer mediaPlayer = createMediaPlayer();

try {
Expand All @@ -190,9 +168,6 @@ public Music newMusic (FileDescriptor fd) {
/** {@inheritDoc} */
@Override
public Sound newSound (FileHandle file) {
if (soundPool == null) {
throw new GdxRuntimeException("Android audio is not enabled by the application config.");
}
AndroidSound androidSound;
AndroidFileHandle aHandle = (AndroidFileHandle)file;
if (aHandle.type() == FileType.Internal) {
Expand All @@ -217,18 +192,12 @@ public Sound newSound (FileHandle file) {
/** {@inheritDoc} */
@Override
public AudioRecorder newAudioRecorder (int samplingRate, boolean isMono) {
if (soundPool == null) {
throw new GdxRuntimeException("Android audio is not enabled by the application config.");
}
return new AndroidAudioRecorder(samplingRate, isMono);
}

/** Kills the soundpool and all other resources */
@Override
public void dispose () {
if (soundPool == null) {
return;
}
synchronized (musics) {
// gah i hate myself.... music.dispose() removes the music from the list...
ArrayList<AndroidMusic> musicsCopy = new ArrayList<AndroidMusic>(musics);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

package com.badlogic.gdx.backends.android;

import com.badlogic.gdx.audio.AudioDevice;
import com.badlogic.gdx.audio.AudioRecorder;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.GdxRuntimeException;

public class DisabledAndroidAudio implements AndroidAudio {

@Override
public void pause () {
}

@Override
public void resume () {
}

@Override
public void notifyMusicDisposed (AndroidMusic music) {
}

@Override
public AudioDevice newAudioDevice (int samplingRate, boolean isMono) {
throw new GdxRuntimeException("Android audio is not enabled by the application config");
}

@Override
public AudioRecorder newAudioRecorder (int samplingRate, boolean isMono) {
throw new GdxRuntimeException("Android audio is not enabled by the application config");
}

@Override
public Sound newSound (FileHandle fileHandle) {
throw new GdxRuntimeException("Android audio is not enabled by the application config");
}

@Override
public Music newMusic (FileHandle file) {
throw new GdxRuntimeException("Android audio is not enabled by the application config");
}

@Override
public boolean switchOutputDevice (String deviceIdentifier) {
return false;
}

@Override
public String[] getAvailableOutputDevices () {
return new String[0];
}

@Override
public void dispose () {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

package com.badlogic.gdx.backends.iosrobovm;

import com.badlogic.gdx.audio.AudioDevice;
import com.badlogic.gdx.audio.AudioRecorder;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.GdxRuntimeException;

public class DisabledIOSAudio implements IOSAudio {
@Override
public void didBecomeActive () {
}

@Override
public void willEnterForeground () {
}

@Override
public void willResignActive () {
}

@Override
public void willTerminate () {
}

@Override
public AudioDevice newAudioDevice (int samplingRate, boolean isMono) {
throw new GdxRuntimeException("iOS audio is not enabled by the application config");
}

@Override
public AudioRecorder newAudioRecorder (int samplingRate, boolean isMono) {
throw new GdxRuntimeException("iOS audio is not enabled by the application config");
}

@Override
public Sound newSound (FileHandle fileHandle) {
throw new GdxRuntimeException("iOS audio is not enabled by the application config");
}

@Override
public Music newMusic (FileHandle file) {
throw new GdxRuntimeException("iOS audio is not enabled by the application config");
}

@Override
public boolean switchOutputDevice (String deviceIdentifier) {
return false;
}

@Override
public String[] getAvailableOutputDevices () {
return new String[0];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ protected Files createFiles () {
}

protected IOSAudio createAudio (IOSApplicationConfiguration config) {
return new OALIOSAudio(config);
if (config.useAudio)
return new OALIOSAudio(config);
else
return new DisabledIOSAudio();
}

protected IOSGraphics createGraphics () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public class OALIOSAudio implements IOSAudio {

public OALIOSAudio (IOSApplicationConfiguration config) {
this.config = config;
if (!config.useAudio) return;
OALSimpleAudio audio = OALSimpleAudio.sharedInstance();
if (audio != null) {
audio.setAllowIpod(config.allowIpod);
Expand Down

0 comments on commit 49b7b45

Please sign in to comment.