✅ Cách thực hiện:
❗ Quy tắc quan trọng:
-
Trước khi phát âm thanh mới, bạn phải dừng âm thanh cũ.
-
Sử dụng lệnh
stopsound()
vàplaysound(name, url)
.
✅ Ví dụ chi tiết
<krpano version=”1.20.11″ onstart=”startup();”>
<!– Khởi động –>
<action name=”startup”>
loadscene(scene1);
</action><!– Scene 1: Âm thanh 1 –>
<scene name=”scene1″ title=”Cảnh 1″ onstart=”playscene1_audio();”>
<view hlookat=”0″ vlookat=”0″ fov=”90″ />
<image><cube url=”panos/scene1_%s.jpg” /></image>
</scene><!– Scene 2: Âm thanh 2 –>
<scene name=”scene2″ title=”Cảnh 2″ onstart=”playscene2_audio();”>
<view hlookat=”0″ vlookat=”0″ fov=”90″ />
<image><cube url=”panos/scene2_%s.jpg” /></image>
</scene><!– Scene 3: Âm thanh 3 –>
<scene name=”scene3″ title=”Cảnh 3″ onstart=”playscene3_audio();”>
<view hlookat=”0″ vlookat=”0″ fov=”90″ />
<image><cube url=”panos/scene3_%s.jpg” /></image>
</scene><!– Action: phát âm thanh cho scene 1 –>
<action name=”playscene1_audio”>
stopsound(bg);
playsound(bg, “sounds/scene1.mp3″);
</action><!– Action: phát âm thanh cho scene 2 –>
<action name=”playscene2_audio”>
stopsound(bg);
playsound(bg, “sounds/scene2.mp3″);
</action><!– Action: phát âm thanh cho scene 3 –>
<action name=”playscene3_audio”>
stopsound(bg);
playsound(bg, “sounds/scene3.mp3”);
</action>
</krpano>
📝 Ghi chú:
-
"bg"
là tên định danh của âm thanh, có thể đặt tên khác nếu muốn. -
Đường dẫn
"sounds/scene1.mp3"
có thể là:-
Tuyệt đối:
https://yourdomain.com/audio/scene1.mp3
-
Tương đối:
sounds/scene1.mp3
(nằm trong thư mục chứa tour)
-
-
File âm thanh có thể là
.mp3
,.ogg
,.wav
(tùy trình duyệt hỗ trợ)
✅ Gợi ý nâng cao (nếu cần):
-
✅ Thêm nút bật/tắt tiếng toàn tour
-
✅ Phát nhạc lặp lại (
loop=true
) -
✅ Kiểm soát âm lượng (
volume=0.8
)
Bản mở rộng với nút BẬT / TẮT ÂM THANH toàn bộ tour trong Krpano.
✅ TÍNH NĂNG:
-
✅ Mỗi scene phát âm thanh riêng
-
✅ Có nút bật/tắt tiếng (mute/unmute)
-
✅ Nút đổi biểu tượng (loa bật / loa tắt)
-
✅ Điều khiển toàn bộ âm thanh bằng biến
sound_muted
🧩 TOÀN BỘ CODE BỔ SUNG
1. 📦 Khai báo biến âm thanh toàn cục
<var name=”sound_muted” value=”false” />
2. 🔈 Nút bật/tắt âm thanh
<layer name=”sound_toggle”
url=”skin/sound_on.png”
align=”righttop”
x=”140″ y=”20″
scale=”0.5″
handcursor=”true”
onclick=”toggle_sound();”
onhover=”tween(scale, 0.6, 0.2);”
onout=”tween(scale, 0.5, 0.2);” />
3. 🔁 Hành động toggle âm thanh
<action name=”toggle_sound”>
if(sound_muted,
set(sound_muted, false);
set(layer[sound_toggle].url, ‘skin/sound_on.png’);
call(play_current_scene_audio);
,
set(sound_muted, true);
set(layer[sound_toggle].url, ‘skin/sound_off.png’);
stopsound(bg);
);
</action>
4. 🎵 Phát âm thanh mỗi scene có kiểm tra sound_muted
Scene 1:
<action name=”playscene1_audio”>
stopsound(bg);
if(!sound_muted, playsound(bg, “sounds/scene1.mp3”); );
set(global.scene_audio, ‘scene1’);
</action>
Scene 2:
<action name=”playscene2_audio”>
stopsound(bg);
if(!sound_muted, playsound(bg, “sounds/scene2.mp3”); );
set(global.scene_audio, ‘scene2’);
</action>
Scene 3:
<action name=”playscene3_audio”>
stopsound(bg);
if(!sound_muted, playsound(bg, “sounds/scene3.mp3”); );
set(global.scene_audio, ‘scene3’);
</action>
5. 🔄 Hàm phát lại âm thanh đúng scene (khi unmute)
<action name=”play_current_scene_audio”>
if(global.scene_audio === ‘scene1’, call(playscene1_audio); );
if(global.scene_audio === ‘scene2’, call(playscene2_audio); );
if(global.scene_audio === ‘scene3’, call(playscene3_audio); );
</action>
🖼️ Gợi ý icon:
-
sound_on.png
→ icon loa mở -
sound_off.png
→ icon loa tắt -
Đặt trong thư mục
skin/
hoặc cập nhật đường dẫn phù hợp
✅ Kết hợp hoàn chỉnh:
-
Mỗi scene gọi hàm riêng:
playscene1_audio()
,playscene2_audio()
… -
Nút
sound_toggle
dùng để tắt/bật âm thanh mọi scene -
Khi người dùng bật tiếng lại, âm thanh của scene hiện tại sẽ tự phát lại
Xem thêm: