or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

application.mdaudio.mdfiles.mdgraphics.mdindex.mdinput.mdnetworking.mdpreloader.mdwebaudio.mdwidgets.md

audio.mddocs/

0

# Audio System

1

2

The GWT backend provides comprehensive audio support through both HTML5 Audio and Web Audio API implementations. It automatically selects the best available audio backend based on browser capabilities and provides fallback mechanisms for maximum compatibility.

3

4

## Core Audio Interfaces

5

6

### GwtAudio { .api }

7

8

```java

9

public interface GwtAudio extends Audio {

10

// Inherited from Audio interface

11

Music newMusic(FileHandle file);

12

Sound newSound(FileHandle file);

13

AudioDevice newAudioDevice(int samplingRate, boolean isMono);

14

AudioRecorder newAudioRecorder(int samplingRate, boolean isMono);

15

}

16

```

17

18

### DefaultGwtAudio { .api }

19

20

```java

21

public class DefaultGwtAudio implements GwtAudio {

22

// Audio creation methods

23

public Music newMusic(FileHandle file);

24

public Sound newSound(FileHandle file);

25

26

// Audio device methods (limited browser support)

27

public AudioDevice newAudioDevice(int samplingRate, boolean isMono);

28

public AudioRecorder newAudioRecorder(int samplingRate, boolean isMono); // Not supported

29

30

// Constructor

31

public DefaultGwtAudio();

32

}

33

```

34

35

## Usage Examples

36

37

### Basic Audio Setup

38

39

```java

40

public class MyGame implements ApplicationListener {

41

private Music backgroundMusic;

42

private Sound jumpSound;

43

44

@Override

45

public void create() {

46

// Load audio files

47

backgroundMusic = Gdx.audio.newMusic(Gdx.files.internal("music/background.ogg"));

48

jumpSound = Gdx.audio.newSound(Gdx.files.internal("sounds/jump.wav"));

49

50

// Configure background music

51

backgroundMusic.setLooping(true);

52

backgroundMusic.setVolume(0.7f);

53

backgroundMusic.play();

54

}

55

56

public void playJumpSound() {

57

jumpSound.play(1.0f); // Play at full volume

58

}

59

60

@Override

61

public void dispose() {

62

backgroundMusic.dispose();

63

jumpSound.dispose();

64

}

65

}

66

```

67

68

### Audio Format Support

69

70

```java

71

// Supported audio formats for web deployment

72

// OGG Vorbis - Widely supported, good compression

73

Music oggMusic = Gdx.audio.newMusic(Gdx.files.internal("music/track.ogg"));

74

75

// MP3 - Universal support but larger files

76

Music mp3Music = Gdx.audio.newMusic(Gdx.files.internal("music/track.mp3"));

77

78

// WAV - Uncompressed, large files but immediate playback

79

Sound wavSound = Gdx.audio.newSound(Gdx.files.internal("sounds/effect.wav"));

80

81

// M4A/AAC - Good compression, supported in most browsers

82

Music m4aMusic = Gdx.audio.newMusic(Gdx.files.internal("music/track.m4a"));

83

```

84

85

### Web-Specific Audio Considerations

86

87

```java

88

public class WebAudioGame implements ApplicationListener {

89

private Music music;

90

private boolean audioUnlocked = false;

91

92

@Override

93

public void create() {

94

music = Gdx.audio.newMusic(Gdx.files.internal("music/background.ogg"));

95

96

// Note: Many browsers require user interaction before audio can play

97

// The GWT backend handles this automatically, but you may need to

98

// wait for the first user input before starting audio

99

}

100

101

@Override

102

public void render() {

103

// Check if audio context is ready (Web Audio API)

104

if (!audioUnlocked && Gdx.input.justTouched()) {

105

// First user interaction - safe to start audio

106

music.play();

107

audioUnlocked = true;

108

}

109

}

110

}

111

```

112

113

## Browser Audio Limitations

114

115

The GWT backend handles several browser-specific audio limitations:

116

117

### Automatic Audio Backend Selection

118

119

The backend automatically chooses between:

120

- **Web Audio API**: Modern browsers, better performance and features

121

- **HTML5 Audio**: Fallback for older browsers or when Web Audio API is unavailable

122

123

### User Interaction Requirements

124

125

```java

126

// Modern browsers require user interaction before audio can play

127

// The GWT backend queues audio operations until user interaction occurs

128

129

public class AudioManager {

130

private Music music;

131

private boolean audioStarted = false;

132

133

public void init() {

134

music = Gdx.audio.newMusic(Gdx.files.internal("music/bgm.ogg"));

135

}

136

137

public void startAudioOnUserInput() {

138

if (!audioStarted && (Gdx.input.justTouched() || Gdx.input.isKeyJustPressed(Input.Keys.ANY_KEY))) {

139

music.play();

140

audioStarted = true;

141

}

142

}

143

}

144

```

145

146

### Audio Format Optimization

147

148

```java

149

// Recommended audio setup for web deployment

150

public void setupOptimalAudio() {

151

// Use OGG for music (good compression, widely supported)

152

Music backgroundMusic = Gdx.audio.newMusic(Gdx.files.internal("music/bg.ogg"));

153

154

// Use short WAV files for sound effects (immediate playback)

155

Sound shortEffect = Gdx.audio.newSound(Gdx.files.internal("sfx/click.wav"));

156

157

// Keep sound files under 1MB for instant loading

158

// Preload all audio files during application loading

159

}

160

```

161

162

### Audio Preloading

163

164

```java

165

// Audio files should be included in preloader for optimal performance

166

public class MyGameGwt extends GwtApplication {

167

@Override

168

public Preloader createPreloader() {

169

return new Preloader("assets.txt") {

170

// assets.txt should include all audio files:

171

// music/background.ogg

172

// sounds/jump.wav

173

// sounds/explosion.wav

174

};

175

}

176

}

177

```