Reproducir audio
Reproducción básica
Para reproducir un sonido importado, utiliza las mismas funciones que usarías para uno regular. Por ejemplo, usa la función PlaySound2D
o Play
desde un componente de audio como Sound Cue.
Control de reproducción
Rebobinado del tiempo de reproducción
Para rebobinar el tiempo de reproducción de la onda de sonido, utiliza la función RewindPlaybackTime
.
- Blueprint
- C++
// Assuming ImportedSoundWave is a UE reference to a UImportedSoundWave object
// Rewind playback time of the sound wave for 12.5 seconds
ImportedSoundWave->RewindPlaybackTime(12.5f);
En versiones de UE anteriores a la 4.27, si deseas iniciar la reproducción desde un tiempo específico mayor que 0, es posible que necesites usar la función RewindPlaybackTime
previamente. De lo contrario, el sonido podría no reproducirse correctamente debido a problemas internos del motor al manejar ondas procedurales. Este problema se ha resuelto en el motor desde la versión 5.0.
Obteniendo Información de Reproducción
Para obtener el tiempo actual de reproducción de la onda de sonido, usa las funciones GetPlaybackTime
o GetPlaybackPercentage
. También puedes obtener la duración de la onda de sonido usando la función GetDuration
.
- Blueprint
- C++
// Assuming ImportedSoundWave is a UE reference to a UImportedSoundWave object
// Get the current playback time of the sound wave
float PlaybackTime = ImportedSoundWave->GetPlaybackTime();
// Get the current playback percentage of the sound wave
float PlaybackPercentage = ImportedSoundWave->GetPlaybackPercentage();
// Get the duration of the sound wave
float Duration = ImportedSoundWave->GetDuration();
Verificando el Estado de Reproducción
Está Reproduciendo Actualmente
Para determinar si la onda de sonido se está reproduciendo actualmente, puedes usar la función IsPlaying
.
- Blueprint
- C++
// Assuming ImportedSoundWave is a UE reference to a UImportedSoundWave object
// Check if the sound wave is currently playing
bool bIsPlaying = ImportedSoundWave->IsPlaying();
¿Se ha completado la reproducción
Para verificar si la onda de sonido ha terminado de reproducirse, puedes usar la función IsPlaybackFinished
.
- Blueprint
- C++
// Assuming ImportedSoundWave is a UE reference to a UImportedSoundWave object
// Check if the sound wave has finished playback
bool bIsFinished = ImportedSoundWave->IsPlaybackFinished();
Detener la reproducción
Puedes detener la reproducción de la onda de sonido utilizando la función StopPlayback
.
- Blueprint
- C++
// Assuming ImportedSoundWave is a UE reference to a UImportedSoundWave object
// Stop the playback of the sound wave
ImportedSoundWave->StopPlayback();
Se recomienda detener la reproducción de la onda de sonido usando medios externos (por ejemplo, llamando a Stop en el componente de audio) y usar esta función solo si no hay medios externos disponibles. También ten en cuenta que esta función no funciona para reproducciones desde MetaSounds.
Manejo de Eventos
Seguimiento de la Finalización de la Reproducción
Para rastrear el final de la reproducción de audio, enlaza al delegado OnAudioPlaybackFinished
.
- Blueprint
- C++
UCLASS()
class AMyAudioPlayer : public AActor
{
GENERATED_BODY()
public:
UFUNCTION()
void OnAudioFinished()
{
// Handle the end of audio playback
}
void BindAudioDelegate(UImportedSoundWave* ImportedSoundWave)
{
// Bind to the OnAudioPlaybackFinished delegate
ImportedSoundWave->OnAudioPlaybackFinished.AddDynamic(this, &AMyAudioPlayer::OnAudioFinished);
}
};
Gestión de Memoria
Liberando Memoria
Puedes limpiar manualmente los datos de audio usando la función ReleaseMemory
.
- Blueprint
- C++
// Release memory of the sound wave
ImportedSoundWave->ReleaseMemory();
No se recomienda la liberación manual de memoria a menos que tengas requisitos específicos de gestión de memoria o hayas desactivado el recolector de basura.