最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

vlcj - How can I properly fade out one audio track and then play the next track at 100%? - Stack Overflow

programmeradmin3浏览0评论

I'm using vlcj 4.8.3 on macOS with the 3.0.20 App installed. When I execute a version of the MCV example that sets the volume before starting the next track or sleeps for less than about 15 millis after starting the next track before setting the volume, then the volume is zero (reported as -1) while playing the second track. For the version of the MCV example, the sleep Millis method parameter is the key. For the earlier version I mention, you move the play(mrl) call after the setVolume(100) call. I should also mention the call to setVolume(100) is not required for the first track.

I execute the MCV example like this:

$ java -cp $CP Tutorial sleepMillis m4aFile1 m4aFile2

Here is the MCV example. Please ignore the fact that the cleanup is missing.

import uk.co.caprica.vlcj.playerponent.AudioPlayerComponent;

public class Tutorial {
    private final AudioPlayerComponent mpc = new AudioPlayerComponent();

    public static void main(String[] args) {
        Tutorial tutorial = new Tutorial();
        for (int i=1; i<args.length; i++)
            tutorial.start(args[0], args[i]);
        System.exit(0);
    }
    public void start (String sleepMillis, String mrl) {
        mpc.mediaPlayer().submit(new Runnable() {
            public void run () {
                mpc.mediaPlayer().audio().setVolume(100);
            }
        });
        System.out.printf("Playing %s\n", mrl);

        mpc.mediaPlayer().submit(new Runnable() {
            public void run () {
                mpc.mediaPlayer().media().play(mrl);
                System.out.printf("sleeping for %d millis\n", Integer.parseInt(sleepMillis));
                try {Thread.sleep(Integer.parseInt(sleepMillis));} catch (Exception ex) {}
                mpc.mediaPlayer().audio().setVolume(100);
            }
        });

        try {Thread.sleep(3000);} catch (Exception ex) {}
        fadeAndStop();
        while (true) {
            try {Thread.sleep(1000);} catch (Exception ex) {}
            boolean isp = mpc.mediaPlayer().status().isPlaying();
            if (!isp)
                break;
        }
    }
    private void fadeAndStop () {
        mpc.mediaPlayer().submit(new Runnable() {
            public void run () {
                float secsToFade = 3.0f;
                int sleepMillis = 50;
                int vol = mpc.mediaPlayer().audio().volume();
                System.out.println("fadeAndStop: vol is " + vol);
                double decrPerIterDbl = vol / ( secsToFade * 1000.0 / (float) sleepMillis);
                int decrPerIter = (int) Math.round(decrPerIterDbl+.5);
                while (vol > 0) {
                    vol -= decrPerIter;
                    try {
                        if (vol < 0) vol = 0;
                        mpc.mediaPlayer().audio().setVolume(vol);
                        try {Thread.sleep(sleepMillis);} catch (Exception ex) {}
                    } catch (Throwable ex) {
                        break;
                    }
                }
                mpc.mediaPlayer().controls().stop();
            }
        });
    }
}

Just to reiterate, or clarify, these are the lines that appear to work:

mpc.mediaPlayer().media().play(mrl);
System.out.printf("sleeping for %d millis\n", Integer.parseInt(sleepMillis));
try {Thread.sleep(Integer.parseInt(sleepMillis));} catch (Exception ex) {}
mpc.mediaPlayer().audio().setVolume(100);

If I set, as above, the value of sleepMillis to be anywhere between 0 and 13, badness happens.

If I change the above code to the following, which makes it look intuitive, then badness happens:

mpc.mediaPlayer().audio().setVolume(100);
// no amount of sleeping works in this scenario
mpc.mediaPlayer().media().play(mrl);
发布评论

评论列表(0)

  1. 暂无评论