Version

Goal:Stream an audio file from the filesystem
Difficulty:Beginner
Duration:5 minutes
Language:All

Note: The audio file content will be read a few kilobytes at a time and its content decoded on the fly as playback progresses.

Python

# Stream an audio file using the OpenAL mixer

import os
import harfang as hg
import time

hg.LoadPlugins()

# mount the system file driver
hg.MountFileDriver(hg.StdFileDriver())

# create a new mixer
mixer = hg.CreateMixer()
mixer.Open()

# start streaming
channel = mixer.Stream(os.path.join(os.getcwd(), "../_data/skaven.it"))

# wait until the user decides to exit the program or the stream ends
print("Playing on channel %d, press Ctrl+C to stop." % channel)

while mixer.GetPlayState(channel) == hg.MixerPlaying:
    time.sleep(0.1)

mixer.Close()

Lua

-- Stream an audio file using the OpenAL mixer

hg = require("harfang")

hg.LoadPlugins()

-- mount the system file driver
hg.MountFileDriver(hg.StdFileDriver())

-- create a new mixer
mixer = hg.CreateMixer()
mixer:Open()

-- start streaming
channel = mixer:Stream('../_data/skaven.it')

-- wait until the user decides to exit the program or the stream ends
print('Playing on channel '..channel..', press Ctrl+C to stop.')

while mixer:GetPlayState(channel) == hg.MixerPlaying do
    hg.Sleep(hg.time_from_ms(100))
end

mixer:Close()