@@ -20,7 +20,7 @@ export class VlcPlayer {
20
20
21
21
/**
22
22
* Open the VLC media player with an empty playlist. VLC will not take
23
- * over the display until the first media is added to the playlist.
23
+ * over the display until the first media is enqueueed to the playlist.
24
24
*/
25
25
async open ( ) {
26
26
return new Promise ( ( resolve , reject ) => {
@@ -38,13 +38,12 @@ export class VlcPlayer {
38
38
if ( this . _verbose ) {
39
39
this . _vlc . stderr . on ( "data" , data => process . stderr . write ( data ) ) ;
40
40
}
41
- this . _writecmd = promisify ( this . _vlc . stdin . write ) ;
42
41
return resolve ( ) ;
43
42
} ) ;
44
43
}
45
44
46
45
/**
47
- * Start playing after being paused or add the given file at the end of
46
+ * Start playing after being paused or enqueue the given file at the end of
48
47
* the playlist and start playing it. If the file is already in the
49
48
* playlist, jump to that media position in the playlist instead.
50
49
*
@@ -56,7 +55,7 @@ export class VlcPlayer {
56
55
}
57
56
else {
58
57
if ( ! this . _playlist . has ( mediaPath ) ) {
59
- await this . add ( mediaPath ) ;
58
+ await this . enqueue ( mediaPath ) ;
60
59
}
61
60
let index = this . _playlist . get ( mediaPath ) ;
62
61
await this . exec ( `goto ${ index } ` ) ;
@@ -71,25 +70,20 @@ export class VlcPlayer {
71
70
}
72
71
73
72
/**
74
- * Add the specified media at the end of the playlist. Pause the media
75
- * if it is the first one.
73
+ * Enqueue the specified media at the end of the playlist.
76
74
*
77
75
* @param {string } mediaPath The path of the media to play.
78
76
*/
79
- async add ( mediaPath ) {
77
+ async enqueue ( mediaPath ) {
80
78
if ( ! fs . existsSync ( mediaPath ) ) {
81
79
throw new Error ( `Media '${ mediaPath } ' not found.` ) ;
82
80
}
83
81
if ( this . _playlist . has ( mediaPath ) ) {
84
82
throw new Error ( `Media '${ mediaPath } ' already in the playlist.` ) ;
85
83
}
86
- let firstFile = this . _playlistIndex === 3 ;
87
84
this . _playlist . set ( mediaPath , this . _playlistIndex ) ;
88
85
this . _playlistIndex ++ ;
89
- await this . exec ( `add ${ mediaPath } ` ) ;
90
- if ( firstFile ) {
91
- await this . exec ( "pause" ) ;
92
- }
86
+ await this . exec ( `enqueue ${ mediaPath } ` ) ;
93
87
}
94
88
95
89
/**
@@ -125,17 +119,16 @@ export class VlcPlayer {
125
119
*
126
120
* @param {string } command - The command to execute.
127
121
*/
128
- async exec ( command ) {
129
- if ( ! command ) {
130
- throw new Error ( "The command parameter must be provided." ) ;
131
- }
132
- let retries = 0 ;
133
- let writeSuccess = false ;
134
- while ( ! writeSuccess && retries < 10 ) {
135
- if ( retries > 0 ) await _msleep ( 50 ) ;
136
- retries ++ ;
137
- writeSuccess = await this . _writecmd ( `${ command } \n` ) ;
138
- }
122
+ exec ( command ) {
123
+ return new Promise ( ( resolve , reject ) => {
124
+ if ( ! command ) {
125
+ return reject ( "The command parameter must be provided." ) ;
126
+ }
127
+ this . _vlc . stdin . write ( `${ command } \n` , err => {
128
+ if ( err ) reject ( err ) ;
129
+ return resolve ( ) ;
130
+ } ) ;
131
+ } ) ;
139
132
}
140
133
}
141
134
0 commit comments