eaglelandsonce commited on
Commit
c2c73ea
·
verified ·
1 Parent(s): 4dad856

Create audio.py

Browse files
Files changed (1) hide show
  1. audio.py +28 -0
audio.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import random
3
+ import base64
4
+
5
+ def select_audio_file():
6
+ audio_files = ["start1.mp3", "start2.mp3", "start3.mp3"]
7
+ selected_audio = random.choice(audio_files)
8
+ audio_file_path = os.path.join("audios", selected_audio)
9
+ return audio_file_path
10
+
11
+ def get_audio_html(audio_data_url):
12
+ audio_html = f"""
13
+ <audio id="start-audio" autoplay>
14
+ <source src="{audio_data_url}" type="audio/mp3">
15
+ Your browser does not support the audio element.
16
+ </audio>
17
+ <script>
18
+ document.getElementById('start-audio').play();
19
+ </script>
20
+ """
21
+ return audio_html
22
+
23
+ def play_audio():
24
+ audio_file_path = select_audio_file()
25
+ audio_bytes = open(audio_file_path, "rb").read()
26
+ audio_data_url = f"data:audio/mp3;base64,{base64.b64encode(audio_bytes).decode()}"
27
+ audio_html = get_audio_html(audio_data_url)
28
+ return audio_html