返回顶部

J2se中的声音---AudioPlayer

  

1
package cn.gp.tools; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 import java.io.InputStream; 7 8 import sun.audio.AudioPlayer; 9 import sun.audio.AudioStream; 10 11 /** 12 * 背景音效工具类 13 * @author 小风微灵 14 * 15 */ 16 public class MusicUtil { 17 18 19 /** 20 * 播放触发音效 21 * @param isPlay 是否播放 22 * @param pathIndex 播放序列 23 */ 24 public static void playMusic(boolean isPlay,int pathIndex) { 25 // 打 开 一 个 声 音 文 件 流 作 为 输 入 26 InputStream in; 27 try { 28 String musicPath=ImageUtil.getProgramRootPath()+"music/btn_music_"+pathIndex+".wav"; 29 in = new FileInputStream (musicPath); 30 //System.out.println("音乐路径:"+musicPath); 31 AudioStream as = new AudioStream (in); // 用 输 入 流 创 建 一 个AudioStream 对 象 32 //System.out.println("成功转换成音乐流:"); 33 if(isPlay){//【改为:isPlay后音效恢复】 34 AudioPlayer.player.start (as); //“player” 是AudioPlayer 中 一 静 态 成 员 用 于 控 制 播 放 35 //System.err.println("音乐播放中...."); 36 }else{ 37 AudioPlayer.player.stop (as); 38 //System.err.println("音乐停止...."); 39 } 40 } catch (FileNotFoundException e) { 41 e.printStackTrace(); 42 } catch (IOException e) { 43 e.printStackTrace(); 44 } 45 } 46 /** 47 * 播放指定音乐 48 * @param isPlay 是否播放 49 * @param musicName 音乐名称 50 */ 51 public static void playMusic(boolean isPlay,String musicName) { 52 // 打 开 一 个 声 音 文 件 流 作 为 输 入 53 InputStream in = null; 54 AudioStream as = null; 55 try { 56 String musicPath=ImageUtil.getProgramRootPath()+"music/"+musicName; 57 in = new FileInputStream (musicPath); 58 //System.out.println("音乐路径:"+musicPath); 59 as = new AudioStream (in); // 用 输 入 流 创 建 一 个AudioStream 对 象 60 //System.out.println("成功转换成音乐流:"); 61 if(isPlay){ 62 AudioPlayer.player.start (as); //“player” 是AudioPlayer 中 一 静 态 成 员 用 于 控 制 播 放 63 //System.err.println("音乐播放中...."); 64 }else{ 65 AudioPlayer.player.stop (as); 66 //System.err.println("音乐停止...."); 67 } 68 } catch (FileNotFoundException e) { 69 e.printStackTrace(); 70 } catch (IOException e) { 71 e.printStackTrace(); 72 } 73 finally{ 74 try { 75 as.close(); 76 } catch (IOException e) { 77 e.printStackTrace(); 78 } 79 try { 80 in.close(); 81 } catch (IOException e) { 82 e.printStackTrace(); 83 } 84 85 } 86 } 87 /** 88 * 默认播放的音效 89 * @param isPlay 是否播放 90 */ 91 public static void playMusic(boolean isPlay) { 92 // 打 开 一 个 声 音 文 件 流 作 为 输 入 93 InputStream in; 94 try { 95 String musicPath=ImageUtil.getProgramRootPath()+"music/btn_music_5.wav"; 96 in = new FileInputStream (musicPath); 97 //System.out.println("音乐路径:"+musicPath); 98 AudioStream as = new AudioStream (in); // 用 输 入 流 创 建 一 个AudioStream 对 象 99 //System.out.println("成功转换成音乐流:"); 100 if(isPlay){ 101 AudioPlayer.player.start (as); //“player” 是AudioPlayer 中 一 静 态 成 员 用 于 控 制 播 放 102 //System.err.println("音乐播放中...."); 103 }else{ 104 AudioPlayer.player.stop (as); 105 //System.err.println("音乐停止...."); 106 } 107 } catch (FileNotFoundException e) { 108 e.printStackTrace(); 109 } catch (IOException e) { 110 e.printStackTrace(); 111 } 112 } 113 }

 

posted @ 2016-07-07 15:05  小风微灵-彦  阅读(268)  评论(0编辑  收藏  举报
加载中……