The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

HTML::Object::DOM::AudioTrack - HTML Object DOM AudioTrack Class

SYNOPSIS

    use HTML::Object::DOM::AudioTrack;
    my $audio = HTML::Object::DOM::AudioTrack->new || 
        die( HTML::Object::DOM::AudioTrack->error, "\n" );

VERSION

    v0.2.0

DESCRIPTION

The AudioTrack interface represents a single audio track from one of the HTML media elements, <audio> or <video>.

INHERITANCE

    +-----------------------+     +---------------------------+     +-------------------------------+     +-------------------------------+
    | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::MediaTrack | --> | HTML::Object::DOM::AudioTrack |
    +-----------------------+     +---------------------------+     +-------------------------------+     +-------------------------------+

PROPERTIES

Inherits properties from its parent HTML::Object::DOM::MediaTrack

enabled

A boolean value which controls whether or not the audio track's sound is enabled. Setting this value to false mutes the track's audio.

Example:

    sub swapCommentaryMain
    {
        my $videoElem = $doc->getElementById( 'main-video' );
        my $audioTrackMain;
        my $audioTrackCommentary;

        $videoElem->audioTracks->forEach(sub
        {
            my $track = shift( @_ );
            if( $track->kind == 'main' )
            {
                $audioTrackMain = $track;
            }
            elsif( $track->kind == 'commentary' )
            {
                $audioTrackCommentary = $track;
            }
        });

        if( $audioTrackMain && $audioTrackCommentary )
        {
            my $commentaryEnabled = $audioTrackCommentary->enabled;
            $audioTrackCommentary->enabled = $audioTrackMain->enabled;
            $audioTrackMain->enabled = $commentaryEnabled;
        }
    }

See also Mozilla documentation

id

A string which uniquely identifies the track within the media. This ID can be used to locate a specific track within an audio track list by calling AudioTrackList.getTrackById(). The ID can also be used as the fragment part of the URL if the media supports seeking by media fragment per the Media Fragments URI specification.

See also Mozilla documentation

kind

A string specifying the category into which the track falls. For example, the main audio track would have a kind of "main".

See also Mozilla documentation

label

A string providing a human-readable label for the track. For example, an audio commentary track for a movie might have a label of "Commentary with director John Q. Public and actors John Doe and Jane Eod." This string is empty if no label is provided.

Example:

    use Module::Generic::Array;
    sub getTrackList
    {
        my $el = shift( @_ );
        my $trackList = Module::Generic::Array->new;
        my $wantedKinds = [
            "main", "alternative", "main-desc", "translation", "commentary"
        ];

        $el->audioTracks->forEach(sub
        {
            my $track = shift( @_ );
            if( $wantedKinds->includes( $track->kind ) )
            {
                $trackList->push({
                    id => $track->id,
                    kind => $track->kind,
                    label => $track->label
                });
            }
        });
        return( $trackList );
    }

See also Mozilla documentation

language

A string specifying the audio track's primary language, or an empty string if unknown. The language is specified as a BCP 47 (RFC 5646) language code, such as "en-US" or "pt-BR".

Example:

    use Module::Generic::Array;
    sub getAvailableLanguages
    {
        my $el = shift( @_ );
        my $trackList = Module::Generic::Array->new;
        my $wantedKinds = [
            "main", "translation"
        ];

        $el->audioTracks->forEach(sub
        {
            my $track = shift( @_ );
            if( $wantedKinds->includes( $track->kind ) )
            {
                $trackList->push({
                    id => $track->id,
                    kind => $track->kind,
                    language => $track->language
                });
            }
        });
        return( $trackList );
    }

See also Mozilla documentation

sourceBuffer

The SourceBuffer that created the track. Returns undef if the track was not created by a SourceBuffer or the SourceBuffer has been removed from the MediaSource.sourceBuffers attribute of its parent media source.

See also Mozilla documentation

METHODS

Inherits methods from its parent HTML::Object::DOM::MediaTrack

EXAMPLE

    use feature 'signatures';
    my $sfx = HTML::Object::DOM::Element::Audio->new( 'sfx.wav' );
    my $sounds = $sfx->addTextTrack( 'metadata' );

    # add sounds we care about
    sub addFX( $start, $end, $name )
    {
        my $cue = HTML::Object::DOM::VTTCue->new( $start, $end, '' );
        $cue->id = $name;
        $cue->pauseOnExit = 1; # true
        $sounds->addCue( $cue );
    }
    addFX( 12.783, 13.612, 'dog bark' );
    addFX( 13.612, 15.091, 'kitten mew' );

    sub playSound( $id )
    {
        $sfx->currentTime = $sounds->getCueById( $id )->startTime;
        $sfx->play();
    }

    # play a bark as soon as we can
    $sfx->oncanplaythrough = sub
    {
        playSound( 'dog bark' );
    }
    # meow when the user tries to leave,
    # and have the browser ask them to stay
    $doc->onbeforeunload = sub
    {
        my $e = shift( @_ );
        playSound( 'kitten mew' );
        $e->preventDefault();
    }

AUTHOR

Jacques Deguest <jack@deguest.jp>

SEE ALSO

Mozilla documentation

COPYRIGHT & LICENSE

Copyright(c) 2021 DEGUEST Pte. Ltd.

All rights reserved

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.