J2se中的声音---AudioPlayer
2016-07-07 15:05
323 查看
1 package cn.gp.tools; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import sun.audio.AudioPlayer; import sun.audio.AudioStream; /** * 背景音效工具类 * @author 小风微灵 * */ public class MusicUtil { /** * 播放触发音效 * @param isPlay 是否播放 * @param pathIndex 播放序列 */ public static void playMusic(boolean isPlay,int pathIndex) { // 打 开 一 个 声 音 文 件 流 作 为 输 入 InputStream in; try { String musicPath=ImageUtil.getProgramRootPath()+"music/btn_music_"+pathIndex+".wav"; in = new FileInputStream (musicPath); //System.out.println("音乐路径:"+musicPath); AudioStream as = new AudioStream (in); // 用 输 入 流 创 建 一 个AudioStream 对 象 //System.out.println("成功转换成音乐流:"); if(isPlay){//【改为:isPlay后音效恢复】 AudioPlayer.player.start (as); //“player” 是AudioPlayer 中 一 静 态 成 员 用 于 控 制 播 放 //System.err.println("音乐播放中...."); }else{ AudioPlayer.player.stop (as); //System.err.println("音乐停止...."); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 播放指定音乐 * @param isPlay 是否播放 * @param musicName 音乐名称 */ public static void playMusic(boolean isPlay,String musicName) { // 打 开 一 个 声 音 文 件 流 作 为 输 入 InputStream in = null; AudioStream as = null; try { String musicPath=ImageUtil.getProgramRootPath()+"music/"+musicName; in = new FileInputStream (musicPath); //System.out.println("音乐路径:"+musicPath); as = new AudioStream (in); // 用 输 入 流 创 建 一 个AudioStream 对 象 //System.out.println("成功转换成音乐流:"); if(isPlay){ AudioPlayer.player.start (as); //“player” 是AudioPlayer 中 一 静 态 成 员 用 于 控 制 播 放 //System.err.println("音乐播放中...."); }else{ AudioPlayer.player.stop (as); //System.err.println("音乐停止...."); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ try { as.close(); } catch (IOException e) { e.printStackTrace(); } try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 默认播放的音效 * @param isPlay 是否播放 */ public static void playMusic(boolean isPlay) { // 打 开 一 个 声 音 文 件 流 作 为 输 入 InputStream in; try { String musicPath=ImageUtil.getProgramRootPath()+"music/btn_music_5.wav"; in = new FileInputStream (musicPath); //System.out.println("音乐路径:"+musicPath); AudioStream as = new AudioStream (in); // 用 输 入 流 创 建 一 个AudioStream 对 象 //System.out.println("成功转换成音乐流:"); if(isPlay){ AudioPlayer.player.start (as); //“player” 是AudioPlayer 中 一 静 态 成 员 用 于 控 制 播 放 //System.err.println("音乐播放中...."); }else{ AudioPlayer.player.stop (as); //System.err.println("音乐停止...."); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
相关文章推荐
- Apache ab使用POST参数进行压力测试 (服务端为Django)
- tomcat启动报[ERROR]Unable to set localhost. This prevents creation of a GUID
- Linux常用命令
- mahout之推荐系统源码笔记(2) ---相似度计算之RowSimilarityJob
- Linux系统结构
- OpenGL的glScalef缩放变换函数详解
- centos安装lamp环境
- linux configure,make,make install
- How to Install Linux KVM and Create Guest VM with Examples
- ajax完美解决跨域问题(jsonp、nginx反向代理)
- centos7添加虚拟IP
- Linux运维利器之ClusterShell
- Apache Shiro 简介
- 常见Tomcat无法启动故障
- nginx,tomcat配置动静分离
- linux开发常用命令
- hadoop-Rpc使用实例
- Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds...
- linux struct utsname 结构详解是utsname 函数使用说明
- LINUX的LAMP环境搭配