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