Skip to content

Commit 3c78430

Browse files
Site changes [skip-ci]
1 parent 94c95e2 commit 3c78430

File tree

3 files changed

+25664
-25528
lines changed

3 files changed

+25664
-25528
lines changed

llms-full.txt

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9865,23 +9865,29 @@ The web server you're loading content from has to support [HTTP range requests](
98659865
</div>
98669866

98679867
```lua
9868+
local function play_sound(self, hash)
9869+
go.set(self.component, "sound", hash) -- override the resource data on the component
9870+
sound.play(self.component) -- start playing the sound
9871+
end
9872+
98689873
local function parse_range(s)
98699874
local _, _, rstart, rend, size = string.find(s, "(%d+)-(%d+)/(%d+)") -- "bytes 0-16383/103277"
98709875
return rstart, rend, size
98719876
end
98729877

9878+
-- Callback for the http response.
98739879
local function http_result(self, _id, response, extra)
98749880
if response.status == 200 or response.status == 206 then
9881+
-- Successful request
98759882
local relative_path = self.filename
98769883
local range = response.headers['content-range'] -- content-range = "bytes 0-16383/103277"
98779884
local rstart, rend, filesize = parse_range(range)
9878-
9879-
-- Create the Defold resource, "partial" will enable the streaming mode
9885+
-- Create the Defold resource
9886+
-- "partial" will enable the streaming mode
98809887
print("Creating resource", relative_path)
98819888
local hash = resource.create_sound_data(relative_path, { data = response.response, filesize = filesize, partial = true })
9882-
9883-
go.set(self.component, "sound", hash) -- override the resource data on the component
9884-
sound.play(self.component) -- start the playing
9889+
-- send "play_sound" to the component
9890+
play_sound(self, hash)
98859891
end
98869892
end
98879893

@@ -9896,10 +9902,36 @@ end
98969902

98979903
## Resource providers
98989904

9899-
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.
9905+
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()](https://defold.com/ref/liveupdate/#liveupdate.add_mount).
9906+
9907+
You can find a working example in [https://github.com/defold/example-sound-streaming](https://github.com/defold/example-sound-streaming).
99009908

9901-
In this example, we have added a new file provider by adding a live update mount, by calling using [liveupdate.add_mount()](https://defold.com/ref/liveupdate/#liveupdate.add_mount).
9909+
```lua
9910+
-- See http_result() from above example
9911+
9912+
local function load_web_sound(base_url, relative_path)
9913+
local url = base_url .. "/" .. relative_path
9914+
local headers = {}
9915+
-- Request the initial part of the file
9916+
headers['Range'] = string.format("bytes=%d-%d", 0, 16384-1)
99029917

9918+
http.request(url, "GET", http_result, headers, nil, { ignore_cache = true })
9919+
end
9920+
9921+
function init(self)
9922+
self.base_url = "http://my.server.com"
9923+
self.filename = "/path/to/sound.ogg"
9924+
9925+
liveupdate.add_mount("webmount", self.base_url, 100, function ()
9926+
-- once the mount is ready, we can start our request for downloading the first chunk
9927+
load_web_sound(self.base_url, self.filename)
9928+
end)
9929+
end
9930+
9931+
function final(self)
9932+
liveupdate.remove_mount("webmount")
9933+
end
9934+
```
99039935

99049936
## Sound chunk cache
99059937

manuals/sound-streaming.md

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,29 @@ The web server you're loading content from has to support [HTTP range requests](
4949
</div>
5050

5151
```lua
52+
local function play_sound(self, hash)
53+
go.set(self.component, "sound", hash) -- override the resource data on the component
54+
sound.play(self.component) -- start playing the sound
55+
end
56+
5257
local function parse_range(s)
5358
local _, _, rstart, rend, size = string.find(s, "(%d+)-(%d+)/(%d+)") -- "bytes 0-16383/103277"
5459
return rstart, rend, size
5560
end
5661

62+
-- Callback for the http response.
5763
local function http_result(self, _id, response, extra)
5864
if response.status == 200 or response.status == 206 then
65+
-- Successful request
5966
local relative_path = self.filename
6067
local range = response.headers['content-range'] -- content-range = "bytes 0-16383/103277"
6168
local rstart, rend, filesize = parse_range(range)
62-
63-
-- Create the Defold resource, "partial" will enable the streaming mode
69+
-- Create the Defold resource
70+
-- "partial" will enable the streaming mode
6471
print("Creating resource", relative_path)
6572
local hash = resource.create_sound_data(relative_path, { data = response.response, filesize = filesize, partial = true })
66-
67-
go.set(self.component, "sound", hash) -- override the resource data on the component
68-
sound.play(self.component) -- start the playing
73+
-- send "play_sound" to the component
74+
play_sound(self, hash)
6975
end
7076
end
7177

@@ -80,10 +86,36 @@ end
8086

8187
## Resource providers
8288

83-
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.
89+
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).
90+
91+
You can find a working example in [https://github.com/defold/example-sound-streaming](https://github.com/defold/example-sound-streaming).
8492

85-
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).
93+
```lua
94+
-- See http_result() from above example
8695

96+
local function load_web_sound(base_url, relative_path)
97+
local url = base_url .. "/" .. relative_path
98+
local headers = {}
99+
-- Request the initial part of the file
100+
headers['Range'] = string.format("bytes=%d-%d", 0, 16384-1)
101+
102+
http.request(url, "GET", http_result, headers, nil, { ignore_cache = true })
103+
end
104+
105+
function init(self)
106+
self.base_url = "http://my.server.com"
107+
self.filename = "/path/to/sound.ogg"
108+
109+
liveupdate.add_mount("webmount", self.base_url, 100, function ()
110+
-- once the mount is ready, we can start our request for downloading the first chunk
111+
load_web_sound(self.base_url, self.filename)
112+
end)
113+
end
114+
115+
function final(self)
116+
liveupdate.remove_mount("webmount")
117+
end
118+
```
87119

88120
## Sound chunk cache
89121

0 commit comments

Comments
 (0)