Changed audio playback package
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -1,37 +1,33 @@
|
||||
import 'package:flutter_soloud/flutter_soloud.dart';
|
||||
import 'package:audioplayers/audioplayers.dart';
|
||||
import 'package:wolf_3d_data_types/wolf_3d_data_types.dart';
|
||||
import 'package:wolf_3d_synth/src/imf_renderer.dart';
|
||||
|
||||
class WolfAudio {
|
||||
bool _isInitialized = false;
|
||||
|
||||
// --- Music State ---
|
||||
AudioSource? _currentMusicSource;
|
||||
SoundHandle? _currentMusicHandle;
|
||||
final AudioPlayer _musicPlayer = AudioPlayer();
|
||||
|
||||
WolfensteinData? activeGame;
|
||||
|
||||
/// Initializes the SoLoud audio engine.
|
||||
/// Initializes the audio engine.
|
||||
Future<void> init() async {
|
||||
if (_isInitialized) return;
|
||||
|
||||
try {
|
||||
await SoLoud.instance.init(
|
||||
sampleRate: 44100,
|
||||
bufferSize: 2048,
|
||||
channels: Channels.stereo,
|
||||
);
|
||||
// audioplayers doesn't require complex global initialization like SoLoud,
|
||||
// but setting the audio context can be useful for mobile platforms later.
|
||||
await _musicPlayer.setPlayerMode(PlayerMode.mediaPlayer);
|
||||
_isInitialized = true;
|
||||
print("WolfAudio: SoLoud initialized successfully.");
|
||||
print("WolfAudio: AudioPlayers initialized successfully.");
|
||||
} catch (e) {
|
||||
print("WolfAudio: Failed to initialize SoLoud - $e");
|
||||
print("WolfAudio: Failed to initialize AudioPlayers - $e");
|
||||
}
|
||||
}
|
||||
|
||||
/// Disposes of the audio engine and frees resources.
|
||||
void dispose() {
|
||||
stopMusic();
|
||||
SoLoud.instance.deinit();
|
||||
_musicPlayer.dispose();
|
||||
_isInitialized = false;
|
||||
}
|
||||
|
||||
@@ -44,51 +40,42 @@ class WolfAudio {
|
||||
if (!_isInitialized) return;
|
||||
|
||||
// Stop currently playing music to prevent overlap
|
||||
stopMusic();
|
||||
await stopMusic();
|
||||
|
||||
try {
|
||||
// Render hardware instructions into PCM and wrap in WAV
|
||||
final pcmSamples = ImfRenderer.render(track);
|
||||
final wavBytes = ImfRenderer.createWavFile(pcmSamples);
|
||||
|
||||
_currentMusicSource = await SoLoud.instance.loadMem(
|
||||
'track.wav',
|
||||
wavBytes,
|
||||
);
|
||||
_currentMusicHandle = await SoLoud.instance.play(
|
||||
_currentMusicSource!,
|
||||
looping: looping,
|
||||
// Configure looping behavior
|
||||
await _musicPlayer.setReleaseMode(
|
||||
looping ? ReleaseMode.loop : ReleaseMode.stop,
|
||||
);
|
||||
|
||||
// Play the generated WAV file directly from memory
|
||||
await _musicPlayer.play(BytesSource(wavBytes));
|
||||
} catch (e) {
|
||||
print("WolfAudio: Error playing music track - $e");
|
||||
}
|
||||
}
|
||||
|
||||
/// Halts playback and frees memory for the current track.
|
||||
void stopMusic() {
|
||||
/// Halts playback for the current track.
|
||||
Future<void> stopMusic() async {
|
||||
if (!_isInitialized) return;
|
||||
|
||||
if (_currentMusicHandle != null) {
|
||||
SoLoud.instance.stop(_currentMusicHandle!);
|
||||
_currentMusicHandle = null;
|
||||
}
|
||||
if (_currentMusicSource != null) {
|
||||
SoLoud.instance.disposeSource(_currentMusicSource!);
|
||||
_currentMusicSource = null;
|
||||
}
|
||||
await _musicPlayer.stop();
|
||||
}
|
||||
|
||||
/// Pauses the current track.
|
||||
void pauseMusic() {
|
||||
if (_isInitialized && _currentMusicHandle != null) {
|
||||
SoLoud.instance.setPause(_currentMusicHandle!, true);
|
||||
Future<void> pauseMusic() async {
|
||||
if (_isInitialized) {
|
||||
await _musicPlayer.pause();
|
||||
}
|
||||
}
|
||||
|
||||
/// Resumes a paused track.
|
||||
void resumeMusic() {
|
||||
if (_isInitialized && _currentMusicHandle != null) {
|
||||
SoLoud.instance.setPause(_currentMusicHandle!, false);
|
||||
Future<void> resumeMusic() async {
|
||||
if (_isInitialized) {
|
||||
await _musicPlayer.resume();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,7 +96,6 @@ class WolfAudio {
|
||||
final index = level.musicIndex;
|
||||
|
||||
if (index < data.music.length) {
|
||||
// 3. FIXED THIS: Call your playMusic method
|
||||
await playMusic(data.music[index]);
|
||||
} else {
|
||||
print(
|
||||
|
||||
Reference in New Issue
Block a user