Methods & Properties
Methods
Shared (all components)
| Method | Description |
|---|---|
play() | Start playback |
pause() | Pause playback |
setTracks(tracks: TracksInput) | Set playlist programmatically |
setStrings(s: Partial<Strings>) | Override UI strings |
loadPlaylist(url: string) | Fetch and parse an M3U playlist |
Video-only
| Method | Description |
|---|---|
requestFullscreen() | Enter fullscreen mode |
requestPiP() | Enter Picture-in-Picture mode |
Properties
Shared (all components)
| Property | Type | Access | Description |
|---|---|---|---|
currentTrack | Track | null | get | Currently playing track object |
currentTime | number | get/set | Playback position in seconds |
duration | number | get | Total duration in seconds |
paused | boolean | get | Whether playback is paused |
Video-only
| Property | Type | Access | Description |
|---|---|---|---|
playbackRate | number | get/set | Playback speed (0.5 – 2.0) |
volume | number | get/set | Volume level (0.0 – 1.0) |
muted | boolean | get/set | Mute state |
Usage Examples
Playback control
js
const player = document.querySelector('py-audio')
player.play()
player.pause()
console.log(player.paused) // false
console.log(player.currentTime) // 12.5
console.log(player.duration) // 3600Seeking
js
player.currentTime = 60 // jump to 1:00Programmatic tracks
js
player.setTracks([
{ name: 'Chapter 1', time: '0:00' },
{ name: 'Chapter 2', time: '5:30' },
{ name: 'Chapter 3', time: '12:00' },
])Multi-file:
js
player.setTracks([
{
name: 'Episode 1',
src: '/audio/ep1.mp3',
chapters: [
{ name: 'Intro', time: '0:00' },
{ name: 'Main', time: '3:00' },
],
},
{
name: 'Episode 2',
src: '/audio/ep2.mp3',
},
])String overrides
js
player.setStrings({
nowPlaying: 'Đang phát',
playlist: 'Danh sách',
trackCount: '{count} bài',
})Load M3U playlist
js
player.loadPlaylist('https://example.com/playlist.m3u')Video properties
js
const video = document.querySelector('py-video')
video.volume = 0.5
video.muted = false
video.playbackRate = 1.5
video.requestFullscreen()
video.requestPiP()Current track info
js
const track = player.currentTrack
if (track) {
console.log(track.name)
}