Samples / Stack / MIME Play Audio

MIME — Audio and Play Sound

a success and an error tone, addressed by their ICF path

Class
z2ui5_cl_smps_app_487
Repository
abap2UI5/samples-stack
UI5 libraries
sap.m, sap.ui.core
Lowest UI5 release
1.71
In the browser
Needs a system - opens in the playground for reading
Controls it builds
sap.m.Button, sap.m.Input, sap.m.MessageStrip, sap.m.Page, sap.m.Shell, sap.m.Text, sap.m.VBox, sap.ui.core.mvc.View

The class

" @keywords mime audio sound play_audio mp3 follow_up_action
" @summary a success and an error tone, addressed by their ICF path
CLASS z2ui5_cl_smps_app_487 DEFINITION PUBLIC.

  PUBLIC SECTION.
    INTERFACES z2ui5_if_app.

    DATA magic_key TYPE string.
    DATA: BEGIN OF message,
            text TYPE string VALUE IS INITIAL,
            type TYPE string VALUE `None`,
          END OF message.

  PROTECTED SECTION.
    DATA client TYPE REF TO z2ui5_if_client.

    METHODS view_display.
    METHODS on_event.

  PRIVATE SECTION.
ENDCLASS.


CLASS z2ui5_cl_smps_app_487 IMPLEMENTATION.

  METHOD z2ui5_if_app~main.

    me->client = client.

    IF client->check_on_navigated( ).
      view_display( ).
    ELSEIF client->check_on_event( ).
      on_event( ).
    ENDIF.

  ENDMETHOD.


  METHOD view_display.

    DATA(view) = z2ui5_cl_ui5_view_builder=>factory(
        )->ele( n = `View` ns = `mvc`
            )->a( n = `displayBlock` v = `true`
            )->a( n = `height`       v = `100%`
            )->a( n = `xmlns`        v = `sap.m`
            )->a( n = `xmlns:mvc`    v = `sap.ui.core.mvc` ).

    SELECT
      SINGLE FROM icfservloc
      FIELDS icfactive
      WHERE icf_name = @( `MIME_DEMO` )
      INTO @DATA(icfactive).

    " Note, these are demo sounds and are part of the abap2UI5 sample repo.
    " They are NOT meant to use in production.
    DATA(vbox) = view->ele( `Shell`
        )->ele( `Page`
            )->a( n = `title`          v = `abap2UI5 - Sample: Play Success and Error Sounds`
            )->a( n = `showNavButton`  b = client->check_app_prev_stack( )
            )->a( n = `navButtonPress` v = client->_event_nav_app_leave( )
            )->ele( `VBox`
                )->a( n = `class` v = `sapUiSmallMargin` ).

    IF icfactive = abap_false.
      vbox->tag( `MessageStrip`
          )->a( n = `text`    v = `ICF Service '/SAP/PUBLIC/BC/ABAP/mime_demo' is not active. Sounds will not play. Please activate the ICF service first.`
          )->a( n = `type`    v = `Warning`
          )->a( n = `visible` b = abap_true ).
    ENDIF.

    vbox->tag( `MessageStrip`
        )->a( n = `text`    v = client->_bind( message-text )
        )->a( n = `type`    v = client->_bind( message-type )
        )->a( n = `visible` v = `{= !!$` && client->_bind( message-text ) && ` }` ).
    vbox->tag( `Text`
        )->a( n = `text` v = `The magic key is: abap2UI5` ).
    vbox->tag( `Input`
        )->a( n = `id`          v = `inputApp`
        )->a( n = `placeholder` v = `Enter magic key`
        )->a( n = `value`       v = client->_bind( magic_key )
        )->a( n = `submit`      v = client->_event( `enter` ) ).
    vbox->tag( `Button`
        )->a( n = `press` v = client->_event( `enter` )
        )->a( n = `text`  v = `Submit`
        )->a( n = `type`  v = `Accept` ).

    client->view_display( view->stringify( ) ).
    client->follow_up_action(
        val   = z2ui5_if_client=>cs_event-set_focus
        t_arg = VALUE #( ( `inputApp` ) ) ).

  ENDMETHOD.


  METHOD on_event.

    IF client->get_event( ) = `enter`.

      IF magic_key = `abap2UI5`.
        client->follow_up_action(
            val   = z2ui5_if_client=>cs_event-play_audio
            t_arg = VALUE #( ( `/SAP/PUBLIC/BC/ABAP/mime_demo/z2ui5_smp_success.mp3` ) ) ).
        message-type = `Success`.
        message-text = `Hooray!`.

      ELSE.
        client->follow_up_action(
            val   = z2ui5_if_client=>cs_event-play_audio
            t_arg = VALUE #( ( `/SAP/PUBLIC/BC/ABAP/mime_demo/z2ui5_smp_error.mp3` ) ) ).
        message-type = `Error`.
        message-text = `That wasn't the magic key`.
      ENDIF.
      magic_key = VALUE #( ).
    ENDIF.

  ENDMETHOD.

ENDCLASS.