Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to download the .dfpwm file and play it through all connected speakers
- local function streamAudioFromURL(url)
- -- List of all connected speakers
- local speakers = {}
- -- Find all peripherals and check if they're speakers
- for _, peripheralName in ipairs(peripheral.getNames()) do
- if peripheral.getType(peripheralName) == "speaker" then
- table.insert(speakers, peripheral.wrap(peripheralName)) -- Wrap the speaker
- end
- end
- -- If no speakers were found, notify the user
- if #speakers == 0 then
- print("No speakers found on the network.")
- return
- end
- -- Make the HTTP GET request to fetch the .dfpwm file
- local response, data = http.get(url)
- -- Check for a valid response
- if not response then
- print("Error downloading file.")
- return
- end
- -- Save the data to a temporary file
- local file = fs.open("temp_audio.dfpwm", "w")
- file.write(data)
- file.close()
- -- Read the audio file and play it through all speakers
- local audioFile = fs.open("temp_audio.dfpwm", "r")
- local audioData = audioFile.readAll() -- Read the entire file content
- audioFile.close()
- -- Check if audio data was successfully read
- if not audioData or #audioData == 0 then
- print("Error: No valid audio data found.")
- return
- end
- -- Play the audio on all speakers
- for _, speaker in ipairs(speakers) do
- local success, err = pcall(function()
- speaker.playAudio({audioData}) -- Pass the audio data as a table
- end)
- if not success then
- print("Failed to play audio on a speaker: " .. err)
- else
- print("Playing audio on speaker.")
- end
- end
- print("Playing audio from: " .. url)
- end
- -- Main program
- print("Please paste the URL to stream the .dfpwm audio:")
- local url = read()
- -- Stream the audio from the provided URL
- streamAudioFromURL(url)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement