Advertisement
MysticT

NBPlayer

Jul 3rd, 2012
906
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.55 KB | None | 0 0
  1. -- NBPlayer
  2. -- by MysticT
  3.  
  4. -- load the nb api
  5. if not nb then
  6.     os.loadAPI("nb")
  7.     if not nb then
  8.         print("Couldn't load nb api. Check if you installed it correctly.")
  9.         return
  10.     end
  11. end
  12.  
  13. -- detect a modem and connect
  14. local function connect()
  15.     for _,s in ipairs(rs.getSides()) do
  16.         if peripheral.isPresent(s) and peripheral.getType(s) == "modem" then
  17.             rednet.open(s)
  18.             return true
  19.         end
  20.     end
  21.     return false
  22. end
  23.  
  24. -- try to connect to rednet
  25. if not connect() then
  26.     print("No modem found")
  27.     return
  28. end
  29.  
  30. -- play a song file
  31. local function playFile(sPath)
  32.     local file = fs.open(sPath, "r")
  33.     if file then
  34.         local s = file.readAll()
  35.         file.close()
  36.         local tSong = textutils.unserialize(s)
  37.         if tSong and type(tSong) == "table" then
  38.             local title = tSong.name
  39.             if not title or title == "" then
  40.                 title = "Untitled"
  41.             end
  42.             local author = tSong.author
  43.             if not author or author == "" then
  44.                 author = "Unknown"
  45.             end
  46.             print("Playing: ", title, " - by ", author)
  47.             if tSong.original_author and tSong.original_author ~= "" then
  48.                 print("Original Author: ", tSong.original_author)
  49.             end
  50.             if tSong.lenght then
  51.                 print("Lenght: ", tSong.lenght)
  52.             end
  53.             nb.playSong(tSong)
  54.             return true
  55.         end
  56.         return false, "Unknown file format."
  57.     end
  58.     return false, "Error opening file "..sPath
  59. end
  60.  
  61. -- Start Program
  62.  
  63. -- get arguments
  64. local tArgs = { ... }
  65. if #tArgs ~= 1 then
  66.     print("Usage: nbplay <file>")
  67.     return
  68. end
  69.  
  70. -- load and play the file
  71. local ok, err = playFile(tArgs[1])
  72. if not ok then
  73.     return false, err
  74. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement