Skip to content

Commit 9a0a9de

Browse files
authored
Merge pull request #537 from defold/sound-example-liveupdate-example
Added http stream example to sound stream manual
2 parents cc67d56 + 09b3298 commit 9a0a9de

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

docs/en/manuals/sound-streaming.md

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,29 @@ The web server you're loading content from has to support [HTTP range requests](
3838
:::
3939

4040
```lua
41+
local function play_sound(self, hash)
42+
go.set(self.component, "sound", hash) -- override the resource data on the component
43+
sound.play(self.component) -- start playing the sound
44+
end
45+
4146
local function parse_range(s)
4247
local _, _, rstart, rend, size = string.find(s, "(%d+)-(%d+)/(%d+)") -- "bytes 0-16383/103277"
4348
return rstart, rend, size
4449
end
4550

51+
-- Callback for the http response.
4652
local function http_result(self, _id, response, extra)
4753
if response.status == 200 or response.status == 206 then
54+
-- Successful request
4855
local relative_path = self.filename
4956
local range = response.headers['content-range'] -- content-range = "bytes 0-16383/103277"
5057
local rstart, rend, filesize = parse_range(range)
51-
52-
-- Create the Defold resource, "partial" will enable the streaming mode
58+
-- Create the Defold resource
59+
-- "partial" will enable the streaming mode
5360
print("Creating resource", relative_path)
5461
local hash = resource.create_sound_data(relative_path, { data = response.response, filesize = filesize, partial = true })
55-
56-
go.set(self.component, "sound", hash) -- override the resource data on the component
57-
sound.play(self.component) -- start the playing
62+
-- send "play_sound" to the component
63+
play_sound(self, hash)
5864
end
5965
end
6066

@@ -69,10 +75,36 @@ end
6975

7076
## Resource providers
7177

72-
You can of course use other means to load the initial chunk of the sound file. The important thing to remember is that the rest of the chunks are loaded from the resource system and it's resource providers.
78+
You can use other means to load the initial chunk of the sound file. The important thing to remember is that the rest of the chunks are loaded from the resource system and its resource providers. In this example, we add a new (http) file provider by adding a live update mount, by calling using [liveupdate.add_mount()](/ref/liveupdate/#liveupdate.add_mount).
79+
80+
You can find a working example in [https://github.com/defold/example-sound-streaming](https://github.com/defold/example-sound-streaming).
81+
82+
```lua
83+
-- See http_result() from above example
84+
85+
local function load_web_sound(base_url, relative_path)
86+
local url = base_url .. "/" .. relative_path
87+
local headers = {}
88+
-- Request the initial part of the file
89+
headers['Range'] = string.format("bytes=%d-%d", 0, 16384-1)
90+
91+
http.request(url, "GET", http_result, headers, nil, { ignore_cache = true })
92+
end
93+
94+
function init(self)
95+
self.base_url = "http://my.server.com"
96+
self.filename = "/path/to/sound.ogg"
7397

74-
In this example, we have added a new file provider by adding a live update mount, by calling using [liveupdate.add_mount()](/ref/liveupdate/#liveupdate.add_mount).
98+
liveupdate.add_mount("webmount", self.base_url, 100, function ()
99+
-- once the mount is ready, we can start our request for downloading the first chunk
100+
load_web_sound(self.base_url, self.filename)
101+
end)
102+
end
75103

104+
function final(self)
105+
liveupdate.remove_mount("webmount")
106+
end
107+
```
76108

77109
## Sound chunk cache
78110

@@ -84,4 +116,4 @@ You can also control the size of each sound chunk by changing the [`sound.stream
84116

85117
::: important
86118
The total size of the sound chunk cache should be larger than the number of loaded sound files times the stream chunk size. Otherwise, you risk evicting new chunks each frame and sounds won't play properly
87-
:::
119+
:::

0 commit comments

Comments
 (0)