如何在Gluon Charm Down Audio插件中管理声音序列化

时间:2017-06-21 13:43:28

标签: android ios audio javafx gluon

我在此链接中的Gluon Charm Down Audio插件中的声音序列化方面遇到了一些问题:

https://github.com/anktjsh/Gluon-Charm-Down-Plugin-Audio

现在我将解释我想弄清楚的事情:

示例情景

想象一下这种情况:我在游戏结束时一个接一个地获得了每个成就的音频和一系列成就。所以,我需要有这个结果:

第一个声音播放直到结束 - >在第一音频结束时,第二音频开始 - >对于所有n个成就(显然是声音)重复这一点

任何人都可以帮助我了解如何在所有平台(IOS,Android和桌面)中进行操作吗?

这是我尝试将其编码为仅限桌面版(对任何可能的错误感到抱歉):

package com.gluonhq.charm.down.plugins.desktop;

import java.net.URL;
import java.util.Collection;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.gluonhq.charm.down.plugins.AudioService;

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

public class DesktopAudioService extends AudioService {

private MediaPlayer last;
private MediaPlayer lastInLoop;

private boolean isPlaying;

private Collection<MediaPlayer> mediaQueue = new Vector<MediaPlayer>();
private int queueIndex = 0;

@Override
public void play_inner(final String play) {

    if (last != null && isPlaying) {

        mediaQueue.add(new MediaPlayer(new Media(play)));
        queueIndex++;

        last.setOnEndOfMedia(new Runnable() {

            @Override
            public void run() {
                playQueue();
            }
        });
    }

    try {

        if (!isPlaying) {
            last = new MediaPlayer(new Media(play));
            isPlaying = true;

            last.setOnReady(new Runnable() {

                @Override
                public void run() {

                    last.play();
                    isPlaying = true;
                }
            });

            last.setOnEndOfMedia(new Runnable() {

                @Override
                public void run() {
                    isPlaying = false;
                }
            });
        }

    } catch (Exception ex) {
        Logger.getLogger(DesktopAudioService.class.getName()).log(Level.SEVERE, null, ex);
    }

}

private void playQueue() {

    if (mediaQueue == null || mediaQueue.isEmpty())
        return;

    MediaPlayer p = (MediaPlayer) mediaQueue.toArray()[queueIndex - 1];

    p.setOnEndOfMedia(new Runnable() {

        @Override
        public void run() {
            isPlaying = false;
            mediaQueue.remove(p);
            queueIndex--;
            playQueue();
        }

    });

    p.setOnReady(new Runnable() {

        @Override
        public void run() {
            p.play();
            isPlaying = true;
        }
    });

}

public void playInLoop(final String play) {
    try {
        lastInLoop = new MediaPlayer(new Media(play));
        lastInLoop.setCycleCount(lastInLoop.INDEFINITE);
        lastInLoop.setVolume(0.4);
        lastInLoop.play();
    } catch (Exception ex) {
        Logger.getLogger(DesktopAudioService.class.getName()).log(Level.SEVERE, null, ex);
    }

}

public URL getResource(String s) throws ClassNotFoundException {
    StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
    return Class.forName(stackTraceElements[2].getClassName().replace(".lambda$null$1", "")).getResource(s);
}

@Override
public void stop() {
    if (last != null) {
        last.stop();
    }

    if (lastInLoop != null) {
        lastInLoop.stop();
    }
}

@Override
public void pause() {
}

@Override
public void resume() {
}

public boolean isOnPlay() {

    if (last != null)
        return last.getStatus().equals(MediaPlayer.Status.PLAYING);
    else
        return false;
}

}

0 个答案:

没有答案