Reproducir audio
Para reproducir una onda de sonido importada, utiliza las mismas funciones que usarías para una regular. Por ejemplo, usa la función PlaySound2D
o Play
de un componente de audio como Sound Cue.
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);
Además, en versiones de UE hasta la 4.27, si deseas iniciar la reproducción desde un tiempo específico mayor a 0, debes usar la función RewindPlaybackTime
de antemano. De lo contrario, es posible que el sonido no se reproduzca 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.
Para obtener el tiempo de reproducción actual de la onda de sonido, utiliza 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();
Para determinar si la onda sonora 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();
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();
Puede detener la reproducción de la onda de sonido utilizando la función StopPlayback
. Tenga en cuenta que se recomienda detener la reproducción de la onda de sonido mediante métodos externos (por ejemplo, llamando a Stop en el componente de audio) y utilizar esta función solo si no están disponibles medios externos.
También tenga en cuenta que esta función no funciona para la reproducción desde MetaSounds.
- Blueprint
- C++
// Assuming ImportedSoundWave is a UE reference to a UImportedSoundWave object
// Stop the playback of the sound wave
ImportedSoundWave->StopPlayback();
Para rastrear el final de la reproducción de audio, vincula 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);
}
};
Puede borrar manualmente los datos de audio utilizando la función ReleaseMemory
. No se recomienda hacerlo manualmente, pero puede ser útil si ha deshabilitado el recolector de basura.
- Blueprint
- C++
// Release memory of the sound wave
ImportedSoundWave->ReleaseMemory();