扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
//游戏关卡控制 private int gameLevel = 0;// //游戏音乐控制 private Player treadSoundPlayer; Player backgroundMusicPlayer; private int volumeLevel = 100; //音量大小的控制 private VolumeControl vc; private boolean playState = false; //(播放/关闭)状态 ////////////////////////////////声音处理/////////////////////////////////////// public void playerUpdate(Player p, String event, Object eventData) { //player监听 try { if (event == PlayerListener.END_OF_MEDIA) { playState = false; } if (event == PlayerListener.STARTED) { playState = true;// this.musicVolume(); } } catch (Exception e) { System.out.println("Error at 11: " + event + e.toString()); } } protected void musicStart() { //随机产生一个音乐 try { backgroundMusicPlayer = null; backgroundMusicPlayer = createPlayer("/2c.mid", "audio/midi");// backgroundMusicPlayer.addPlayerListener(this);// backgroundMusicPlayer.setLoopCount( -1); vc = (VolumeControl) backgroundMusicPlayer.getControl("VolumeControl"); if (vc != null) { if (gameLevel == 0) backgroundMusicPlayer.setLoopCount( -1); else backgroundMusicPlayer.setLoopCount(5); backgroundMusicPlayer.start(); } } catch (Exception e) { System.out.println("Error at 20: " + backgroundMusicPlayer + e.toString()); } } protected void musicClose() { //关掉音乐 try { if(backgroundMusicPlayer != null){ backgroundMusicPlayer.close(); backgroundMusicPlayer = null; } } catch (Exception e) { System.out.println("Error at 30: " + e.toString()); } } protected void musicPause() { //暂停音乐 try { if (backgroundMusicPlayer != null && playState) pausetime = backgroundMusicPlayer.getMediaTime(); backgroundMusicPlayer.stop(); } catch (Exception e) { System.out.println("Error at 40:" + e.toString()); } } protected void musicContinue() { //音乐继续 try { if (backgroundMusicPlayer != null && !playState) backgroundMusicPlayer.setMediaTime(pausetime); backgroundMusicPlayer.start(); } catch (Exception e) { System.out.println("Error at 50:" + e.toString()); } } protected void musicVolume() //设置音乐的音量 { if (vc != null) vc.setLevel(volumeLevel); } protected Player createPlayer(String filename, String format) { //创建音乐 Player p = null; try { InputStream is = getClass().getResourceAsStream(filename); p = Manager.createPlayer(is, format); p.prefetch(); } catch (Exception x) { System.out.println("Error at 60: " + filename + x.toString()); } return p; }
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=330523
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者