我正在编写一个使用VLCJ处理音频输出的音频播放器。
我可以播放音频,并且可以在播放当前轨道时跳到其他轨道(在下面的示例中使用next()
)。
但是,如果我在当前曲目结束后致电next()
。它将加载轨道,但轨道不会发出任何噪音。
之后,如果我手动跳至另一首曲目(再次使用next()
),它会起作用。
这是一个最小的工作示例:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.sun.jna.NativeLibrary;
import uk.co.caprica.vlcj.component.AudioMediaPlayerComponent;
import uk.co.caprica.vlcj.player.MediaPlayer;
import uk.co.caprica.vlcj.player.MediaPlayerEventAdapter;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
public class VLCJFinishedBug {
String[] trackList = new String[] {
"D:\\test\\1.mp3",
"D:\\test\\2.mp3",
"D:\\test\\3.mp3",
"D:\\test\\4.mp3",
"D:\\test\\5.mp3",
"D:\\test\\6.mp3",
"D:\\test\\7.mp3",
"D:\\test\\8.mp3",
"D:\\test\\9.mp3",
};
int trackIndex = 0;
private MediaPlayer mediaPlayer;
private static final String NATIVE_LIBRARY_SEARCH_PATH = "stage\\lib\\vlc";
public VLCJFinishedBug( ) {
NativeLibrary.addSearchPath( RuntimeUtil.getLibVlcLibraryName(), NATIVE_LIBRARY_SEARCH_PATH );
mediaPlayer = (new AudioMediaPlayerComponent().getMediaPlayer();
mediaPlayer.addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
public void finished ( MediaPlayer player ) {
next();
}
});
}
private void playTrack( String trackLocation ) {
System.out.println ( "Playing track: " + trackLocation );
mediaPlayer.playMedia( trackLocation );
}
private String nextTrackString() {
trackIndex++;
if ( trackIndex >= trackList.length ) {
System.out.println( "Reach end of track list." );
System.exit( 0 );
}
return ( trackList [ trackIndex ] );
}
public void start() {
trackIndex = 0;
playTrack ( trackList[ 0 ] );
}
public void next() {
playTrack ( nextTrackString() );
}
public void goToNearEnd() {
mediaPlayer.setPosition( .98f );
}
public static void main ( String[] args ) {
VLCJFinishedBug player = new VLCJFinishedBug();
JButton start = new JButton ( "Start" );
start.addActionListener( e -> player.start() );
JButton next = new JButton ( "Next" );
next.addActionListener( e -> player.next() );
JButton goToNearEnd = new JButton ( "Go To Near End" );
goToNearEnd.addActionListener( e -> player.goToNearEnd() );
JPanel content = new JPanel();
content.add( start );
content.add( next );
content.add( goToNearEnd );
JFrame frame = new JFrame( "VLC Play Test" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setContentPane( content );
frame.pack();
frame.setVisible( true );
}
}
如何在当前曲目结束后开始播放新曲目?