Skip to content

NotEnoughNetMusic/NetMusicPeripheral

Repository files navigation

网络音乐机:电脑外设

目前文档我还没写,先拿AI生成的用着吧

我感觉我写模组已经写的神志不清了🤮

以下是AI替我写的文档

NetMusicPeripheral Lua API Documentation

Overview

The music_player peripheral provides control over NetMusic music players, allowing you to play, manage, and interact with music CDs and online music streams.

Basic Player Control

play()

Starts playing the current CD in the music player.

Example:

local player = peripheral.find("music_player")
player.play()

stop()

Stops the currently playing music.

Example:

player.stop()

isPlaying() → boolean

Checks if the music player is currently playing.

Returns:

  • boolean - true if playing, false otherwise

Example:

if player.isPlaying() then
    print("Music is currently playing")
end

CD Management

hasCD() → boolean

Checks if there is a CD in the music player.

Returns:

  • boolean - true if a CD is present, false otherwise

Example:

if player.hasCD() then
    print("CD loaded")
end

pushCD(from: string, slot: number)

Takes a CD from another container and places it in the music player.

Parameters:

  • from (string) - Direction of the container (e.g., "front", "back", "left", "right")
  • slot (number) - Slot number in the container (Lua indexing starts at 1)

Throws:

  • If computer is not connected
  • If container not found
  • If target is not a container
  • If slot index is out of range
  • If item is not a valid music CD
  • If music player already contains an item

Example:

player.pushCD("right", 1)  -- Take CD from right container, slot 1

pullCD(to: string, slot: number)

Removes the CD from the music player and places it in another container.

Parameters:

  • to (string) - Direction of the target container
  • slot (number) - Slot number in the target container

Throws:

  • If computer is not connected
  • If container not found
  • If target is not a container
  • If slot index is out of range
  • If music player is empty
  • If cannot place item in target slot
  • If target slot is full
  • If target slot contains different items

Example:

player.pullCD("left", 3)  -- Place CD in left container, slot 3

CD Information

getUrl() → string

Gets the URL of the current CD.

Returns:

  • string - The song URL, or nil if no valid CD

Example:

local url = player.getUrl()
if url then
    print("Playing from: " .. url)
end

getName() → string

Gets the name of the current song.

Returns:

  • string - The song name, or nil if no valid CD

Example:

local name = player.getName()
print("Now playing: " .. (name or "Unknown"))

getTransName() → string

Gets the translated name of the current song.

Returns:

  • string - The translated song name, or nil if no valid CD

getSecond() → number

Gets the duration of the current song in seconds.

Returns:

  • number - Song duration in seconds, or 0 if no valid CD

Example:

local duration = player.getSecond()
print("Song duration: " .. duration .. " seconds")

getArtists() → table

Gets the list of artists for the current song.

Returns:

  • table - Array of artist names, or empty table if no valid CD

Example:

local artists = player.getArtists()
for i, artist in ipairs(artists) do
    print("Artist " .. i .. ": " .. artist)
end

getVip() → boolean

Checks if the current song requires VIP access.

Returns:

  • boolean - true if VIP song, false otherwise

getReadonly() → boolean

Checks if the current CD is read-only.

Returns:

  • boolean - true if read-only, false otherwise

Playback Control

getCurrentTime() → number

Gets the current playback position in seconds.

Returns:

  • number - Current playback time in seconds

Example:

local currentTime = player.getCurrentTime()
local totalTime = player.getSecond()
print(string.format("Progress: %.1f / %.1f seconds", currentTime, totalTime))

CD Writing

setCDUrl(url: string, second: number, songName: string?)

Writes new song data to the current CD.

Parameters:

  • url (string) - The song URL
  • second (number) - Song duration in seconds
  • songName (string, optional) - Custom song name (default: "电脑刻录唱片")

Throws:

  • If music player is empty
  • If CD is read-only

Example:

player.setCDUrl("https://example.com/song.mp3", 180, "My Custom Song")

URL Utilities

idToUrl(id: number) → string

Converts a NetEase Music ID to a playable URL.

Parameters:

  • id (number) - NetEase Music song ID

Returns:

  • string - Playable URL

Example:

local url = player.idToUrl(123456)
player.setCDUrl(url, 240, "Converted Song")

urlToId(url: string) → number

Extracts NetEase Music ID from a URL.

Parameters:

  • url (string) - NetEase Music URL

Returns:

  • number - Song ID, or nil if invalid URL

Example:

local id = player.urlToId("https://music.163.com/song/media/outer/url?id=123456.mp3")
print("Song ID: " .. (id or "Unknown"))

Playlist Features (Requires NetMusicList Mod)

Note: These functions require the NetMusicList mod to be installed.

setListIndex(index: number)

Sets the current playlist index.

Parameters:

  • index (number) - Playlist index (Lua indexing starts at 1)

Throws:

  • If NetMusicList mod not installed
  • If music player is empty
  • If item is not a playlist

Example:

player.setListIndex(5)  -- Play the 5th song in the playlist

getListIndex() → number

Gets the current playlist index.

Returns:

  • number - Current playlist index (1-based)

Throws:

  • If NetMusicList mod not installed
  • If music player is empty
  • If item is not a playlist

Example:

local currentIndex = player.getListIndex()
print("Currently playing track: " .. currentIndex)

getListCount() → number

Gets the total number of songs in the playlist.

Returns:

  • number - Total playlist count

Throws:

  • If NetMusicList mod not installed
  • If music player is empty

Example:

local totalTracks = player.getListCount()
print("Playlist contains " .. totalTracks .. " tracks")

setPlayMode(mode: string)

Sets the playlist playback mode.

Parameters:

  • mode (string) - Playback mode (e.g., "sequential", "random", "repeat")

Throws:

  • If NetMusicList mod not installed
  • If music player is empty

Example:

player.setPlayMode("random")  -- Enable random playback

Error Handling

Always wrap peripheral calls in error handling:

local player = peripheral.find("music_player")
if player then
    local success, err = pcall(function()
        player.play()
    end)
    if not success then
        print("Error: " .. err)
    end
else
    print("No music player found")
end

Complete Example

-- Simple music player controller
local player = peripheral.find("music_player")

if player and player.hasCD() then
    print("Now playing: " .. (player.getName() or "Unknown"))
    print("Duration: " .. player.getSecond() .. " seconds")
    
    if not player.isPlaying() then
        player.play()
    end
    
    -- Monitor playback
    while player.isPlaying() do
        local current = player.getCurrentTime()
        local total = player.getSecond()
        local progress = math.floor((current / total) * 100)
        print(string.format("Progress: %d%%", progress))
        sleep(5)
    end
else
    print("No CD loaded or player not found")
end

About

给网络音乐机提供了CC:Tweaked支持

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages