Advertisement
Kiggs

Untitled

Nov 16th, 2024 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.98 KB | None | 0 0
  1. -- Function to download the .dfpwm file and play it through all connected speakers
  2. local function streamAudioFromURL(url)
  3.     -- List of all connected speakers
  4.     local speakers = {}
  5.  
  6.     -- Find all peripherals and check if they're speakers
  7.     for _, peripheralName in ipairs(peripheral.getNames()) do
  8.         if peripheral.getType(peripheralName) == "speaker" then
  9.             table.insert(speakers, peripheral.wrap(peripheralName))  -- Wrap the speaker
  10.         end
  11.     end
  12.  
  13.     -- If no speakers were found, notify the user
  14.     if #speakers == 0 then
  15.         print("No speakers found on the network.")
  16.         return
  17.     end
  18.  
  19.     -- Make the HTTP GET request to fetch the .dfpwm file
  20.     local response, data = http.get(url)
  21.  
  22.     -- Check for a valid response
  23.     if not response then
  24.         print("Error downloading file.")
  25.         return
  26.     end
  27.  
  28.     -- Save the data to a temporary file
  29.     local file = fs.open("temp_audio.dfpwm", "w")
  30.     file.write(data)
  31.     file.close()
  32.  
  33.     -- Read the audio file and play it through all speakers
  34.     local audioFile = fs.open("temp_audio.dfpwm", "r")
  35.     local audioData = audioFile.readAll()  -- Read the entire file content
  36.     audioFile.close()
  37.  
  38.     -- Check if audio data was successfully read
  39.     if not audioData or #audioData == 0 then
  40.         print("Error: No valid audio data found.")
  41.         return
  42.     end
  43.  
  44.     -- Play the audio on all speakers
  45.     for _, speaker in ipairs(speakers) do
  46.         local success, err = pcall(function()
  47.             speaker.playAudio({audioData})  -- Pass the audio data as a table
  48.         end)
  49.  
  50.         if not success then
  51.             print("Failed to play audio on a speaker: " .. err)
  52.         else
  53.             print("Playing audio on speaker.")
  54.         end
  55.     end
  56.  
  57.     print("Playing audio from: " .. url)
  58. end
  59.  
  60. -- Main program
  61. print("Please paste the URL to stream the .dfpwm audio:")
  62. local url = read()
  63.  
  64. -- Stream the audio from the provided URL
  65. streamAudioFromURL(url)
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement