Audio"

From Documentation
m ((via JWB))
 
(17 intermediate revisions by 4 users not shown)
Line 8: Line 8:
  
 
= Employment/Purpose =
 
= Employment/Purpose =
An <tt>audio</tt> component is used to play the audio at the browser. Like <tt>image</tt>, you could use the <tt>src</tt> property to specify an URL of an audio resource, or the <tt>setContent</tt> method to specify a dynamically generated audio.  
+
An <code>audio</code> component is used to play the audio at the browser. Like <code>image</code>, you could use the <code>src</code> property to specify an URL of an audio resource, or the <code>setContent</code> method to specify a dynamically generated audio. Developers might be able to control the play of an audio by the <code>play</code>, <code>stop</code> and <code>pause</code> methods.
  
 +
= Example =
 +
[[Image:ZKComRef_Audio_Example.png]]
 +
 +
<source lang="xml" >
 +
<audio src="music.wav" controls="true"></audio>
 +
</source>
 +
The audio supports controls property {{versionSince| 7.0.0}}
  
[Since ZK 7.0.0]
+
= Supports HTML5 =
the <tt>audio</tt> only works for browsers supporting HTML5 audio tag, it supports the properties like <tt>autoplay</tt>, <tt>controls</tt>, <tt>loop</tt>, <tt>muted</tt> and <tt>preload</tt>, and it could have multiple <tt>src</tt> attributes. Developers might be able to control the play of an audio by the <tt>play</tt>, <tt>stop</tt> and <tt>pause</tt> methods.
+
{{versionSince| 7.0.0}}
  
= Example =
+
The audio component has now been enhanced to support HTML 5, it includes the properties like <code>autoplay</code>, <code>controls</code>, <code>loop</code>, <code>muted</code> and <code>preload</code>.
[[Image:ZKComRef_Audio_Example.png]]
 
  
 +
= Multiple Sources=
 +
{{versionSince| 7.0.0}}
 +
Most browsers do not support all the audio formats,so we could specify multiple source files in different formats for different browsers. For examples:
 
<source lang="xml" >
 
<source lang="xml" >
  <audio id="audio" src="music.wav, music.mp3, backup.ogg" controls="true"></audio>
+
  <audio src="music.wav, music.mp3, music.ogg" controls="true"></audio>
 +
</source>
 +
 
 +
= StateChangeEvent =
 +
{{versionSince| 9.6.0}}
 +
 
 +
When you call <code>play(), stop(), pause()</code> or the audio is played to the end, an <code>StateChangeEvent</code> will be fired. You can check the current state by calling event.getState(). There are 4 states: <code>Audio.PLAY, Audio.STOP, Audio.PAUSE and Audio.END</code>.
 +
 
 +
For example:
 +
 
 +
If you want to do something after the audio starts to play, you can write codes as shown below (MVVM pattern).
 +
 
 +
<source language="xml">
 +
  <audio onStateChange="@command('stateChange', event=event)" />
 +
</source>
 +
<source language="java">
 +
  @Command
 +
  public void stateChange(@BindingParam("event") StateChangeEvent event) {
 +
    if (event.getState() == Audio.PLAY) {
 +
      // do something...
 +
    }
 +
  }
 
</source>
 
</source>
  
 
=Supported Events=
 
=Supported Events=
  
{| border="1" | width="100%"
+
{| class='wikitable' | width="100%"
 
! <center>Name</center>
 
! <center>Name</center>
 
! <center>Event Type</center>
 
! <center>Event Type</center>
 
|-
 
|-
| None
+
| <center><code>onStateChange</code></center>
| None
+
| '''Event:''' <javadoc>org.zkoss.zk.ui.event.StateChangeEvent</javadoc>
 +
Notifies when invoking play(), stop(), pause() or the audio is played to the end.
 
|}
 
|}
 
*Inherited Supported Events: [[ZK_Component_Reference/Base_Components/XulElement#Supported_Events | XulElement]]
 
*Inherited Supported Events: [[ZK_Component_Reference/Base_Components/XulElement#Supported_Events | XulElement]]
Line 34: Line 65:
 
=Supported Children=
 
=Supported Children=
  
  *NONE
+
  * [[ZK_Component_Reference/Multimedia_and_Miscellaneous/Track|Track]]
  
 
=Use Cases=
 
=Use Cases=
  
{| border='1px' | width="100%"
+
{| class='wikitable' | width="100%"
 
! Version !! Description !! Example Location
 
! Version !! Description !! Example Location
 
|-
 
|-
Line 49: Line 80:
 
{{LastUpdated}}
 
{{LastUpdated}}
  
{| border='1px' | width="100%"
+
{| class='wikitable' | width="100%"
 
! Version !! Date !! Content
 
! Version !! Date !! Content
 
|-
 
|-
| &nbsp;
+
| 7.0.0
| &nbsp;
+
| August, 2013
| &nbsp;
+
| <javadoc>org.zkoss.zul.Audio</javadoc> now supports HTML 5
 +
|-
 +
| 9.5.0
 +
| September 2020
 +
| [https://tracker.zkoss.org/browse/ZK-4648 ZK-4648]: Audio supports to add tracks
 +
|-
 +
| 9.6.0
 +
| May 2021
 +
| [https://tracker.zkoss.org/browse/ZK-4779 ZK-4779]: audio supports to fire an event upon its playing state
 
|}
 
|}
  
 
{{ZKComponentReferencePageFooter}}
 
{{ZKComponentReferencePageFooter}}

Latest revision as of 08:44, 8 July 2022

Audio

Employment/Purpose

An audio component is used to play the audio at the browser. Like image, you could use the src property to specify an URL of an audio resource, or the setContent method to specify a dynamically generated audio. Developers might be able to control the play of an audio by the play, stop and pause methods.

Example

ZKComRef Audio Example.png

 <audio src="music.wav" controls="true"></audio>
The audio supports controls property Since  7.0.0

Supports HTML5

Since 7.0.0

The audio component has now been enhanced to support HTML 5, it includes the properties like autoplay, controls, loop, muted and preload.

Multiple Sources

Since 7.0.0 Most browsers do not support all the audio formats,so we could specify multiple source files in different formats for different browsers. For examples:

 <audio src="music.wav, music.mp3, music.ogg" controls="true"></audio>

StateChangeEvent

Since 9.6.0

When you call play(), stop(), pause() or the audio is played to the end, an StateChangeEvent will be fired. You can check the current state by calling event.getState(). There are 4 states: Audio.PLAY, Audio.STOP, Audio.PAUSE and Audio.END.

For example:

If you want to do something after the audio starts to play, you can write codes as shown below (MVVM pattern).

  <audio onStateChange="@command('stateChange', event=event)" />
  @Command
  public void stateChange(@BindingParam("event") StateChangeEvent event) {
    if (event.getState() == Audio.PLAY) {
      // do something...
    }
  }

Supported Events

Name
Event Type
onStateChange
Event: StateChangeEvent

Notifies when invoking play(), stop(), pause() or the audio is played to the end.

Supported Children

* Track

Use Cases

Version Description Example Location
     

Version History

Last Update : 2022/07/08


Version Date Content
7.0.0 August, 2013 Audio now supports HTML 5
9.5.0 September 2020 ZK-4648: Audio supports to add tracks
9.6.0 May 2021 ZK-4779: audio supports to fire an event upon its playing state



Last Update : 2022/07/08

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.