使用JMF在java中停止音乐

时间:2014-04-02 14:19:01

标签: java jmf

所以我得到这个代码为RPG游戏播放音乐。第一种方法是播放战斗音乐,另一种方法是在你获胜时大张旗鼓。我的问题是音乐堆栈,最终它是一千种不同的战斗音乐和风扇。当战斗结束并且应该播放时,如何停止音乐,反之亦然?

这是代码:

import java.io.File;

import javax.media.Format;

import javax.media.Manager;

import javax.media.MediaLocator;

import javax.media.Player;

import javax.media.PlugInManager;

import javax.media.format.AudioFormat;


public class PlayMusic {

    public void playBattle() {

        try{
            Player player = Manager.createPlayer(new MediaLocator(new File("battle.wav").toURI().toURL()));
            player.start();
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public void playWin() {     


        try{
            Player player = Manager.createPlayer(new MediaLocator(new File("fanfare.wav").toURI().toURL()));
            player.start();
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

import java.io.File;

import javax.media.Format;

import javax.media.Manager;

import javax.media.MediaLocator;

import javax.media.Player;

import javax.media.PlugInManager;

import javax.media.format.AudioFormat;


public class PlayMusic {

 Player battlePlayer = Manager.createPlayer(new MediaLocator(new File("battle.wav").toURI().toURL()));
  Player winPlayer = Manager.createPlayer(new MediaLocator(new File("fanfare.wav").toURI().toURL()));


    public void playBattle() {

        try{

            battlePlayer.start();
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public void playWin() {     


        try{

            winPlayer.start();
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }

 public void stopWin() {     


        try{

            winPlayer.stop();
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }

 public void stopBattle() {     


        try{

            battlePlayer.stop();
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }


}