media.eh
Basic media support. More...
Types
Functions
Description
This header provides basic support for media playback. All functions
in this header may raise
ERR_MEDIA error if an error occurs
during media playback (for instance, if sound is turned off on device
or if device is busy).
Tone generation
Simple tones can be produced using function
play_tone(note, duration, volume). Here
-
note is a number from 0 to 127, where 60 corresponds to the middle C;
-
duration is given in milliseconds;
-
volume is a number from 0 to 100, where 0 is a silence, 100 is a maximum
volume at the current hardware level.
This function returns immediately, note is played back in the background.
use "media"
use "sys"
const C4 = 60
const D4 = C4 + 2
const E4 = C4 + 4
const G4 = C4 + 7
const PAUSE = -1
const TEMP = 250
def main(args: [String]) {
var melody = [
E4, D4, C4, E4,
E4, E4, E4, PAUSE,
D4, D4, D4, PAUSE,
E4, G4, G4, PAUSE,
E4, D4, C4, E4,
E4, E4, E4, PAUSE,
D4, D4, E4, D4, C4
]
for (var i=0, i<melody.len, i+=1) {
if (melody[i] != PAUSE)
play_tone(melody[i], TEMP)
sleep(TEMP)
}
}
|
Players and content types
Player controls rendering of a time based media data (audio or video).
To create new player you need to specify input data (in form of
IStream)
and the content type in form of MIME type string. Here are few common MIME types
Content type |
Common extensions |
Description |
audio/x-wav |
.wav .wave |
Wave audio files |
audio/basic |
.au .snd |
AU audio files |
audio/mpeg |
.mp3 |
MP3 audio files |
audio/midi |
.mid .midi |
MIDI files |
audio/x-tone-seq |
.jts |
Tone sequences
|
Note, that device may not support all of these formats. Standard requires only
support of WAV and tone sequences. The list of content types supported by
particular device can be obtained using
get_supported_ctypes().
var input = readurl("http://mywebsite/sound.wav")
var player = new Player(input, "audio/x-wav")
player.loops = 5
player.start()
|
Type details
A player of media data.
Function details
def get_supported_ctypes(): [String];
|
Returns list of all supported content types.
def play_tone(note: Int, duration: Int, volume: Int = 100);
|
Plays a specified tone.
A
note is in the range 0..127.
A
volume is in the range 0..100.
A
duration is given in milliseconds and must be positive.
This function is non-blocking, it returns immediately and note
is played back in the background.
Creates new player with given data source and content type.
def Player.get_ctype(): String;
|
Returns the content type of the media that is being played back by this player.
def Player.set_loops(count: Int);
|
Sets the number of times the player will loop and play the content.
By default, the media is played once. If loop count is set to -1, content will
be played indefinitely.
def Player.get_duration(): Long;
|
Returns the duration of the media in milliseconds.
def Player.get_time(): Long;
|
Returns current position of the media in milliseconds.
If current position is unknown, the function returns -1.
def Player.set_time(time: Long);
|
Sets current position in the media to play from.
For some content types may be not very accurate. There are media types
that cannot support the setting of media time, for these types,
ERR_MEDIA error is raised.
Starts playback of this player.
If player was stopped previously, playback is resumed from where it
was previously stopped. If the player has reached the end of media,
calling start() will automatically start the playback from the
beginning of the media.
Pauses playback at the current media time.
Closes this player and releases its resources.
Calling this method will also close associated input stream.