Overview
The player functions handle YouTube IFrame API integration, song loading, and playback controls in MYMUSICK. These functions provide the core music playback functionality.Global Variables
Functions
onYouTubeIframeAPIReady()
Initializes the YouTube IFrame Player when the API is ready. This function is automatically called by the YouTube IFrame API.No return value
Implementation
index.html
Behavior
- Creates a YouTube player instance with ID
yt-player - Sets player dimensions to 0x0 (hidden player)
- Disables autoplay on initialization
- Registers
onPlayerStateChangeevent handler
loadSong(song)
Loads and plays a song using the YouTube player.No return value
Implementation
index.html
Usage Example
Behavior
- Validates player instance exists
- Updates “Now Playing” text display
- Loads the YouTube video by ID
- Automatically starts playback
togglePlayPause()
Toggles between play and pause states for the current song.No return value
Implementation
index.html
Usage Example
Behavior
- Pauses video if currently playing
- Resumes video if currently paused
- Relies on global
isPlayingstate variable
onPlayerStateChange(event)
Callback function triggered when the YouTube player state changes.YouTube player state change event
YouTube PlayerState constant (e.g., YT.PlayerState.PLAYING)
No return value
Implementation
index.html
Behavior
- Updates
isPlayingglobal variable based on player state - Makes the play/pause button visible
- Updates button text and icon:
- Shows “⏸️ Pausar” when playing
- Shows “▶️ Reproducir” when paused
YouTube Player States
Integration Flow
DOM Elements Referenced
Button element with ID
playPauseBtn - Controls playbackSpan element with ID
nowPlaying - Displays current song infoDiv element with ID
yt-player - Container for YouTube player