jmf播放音乐实例

code如下:

 

 

package arvato.lottery;

import java.io.File;
import java.io.IOException;

import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;

public class PlayMusic {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new PlayMusic().playMusic();
	}

	
	/**
	 * 播放中奖音
	 */
	public void playBonusMusic(){  
	    try {  
	        javax.media.Player player=null;  
	        if(null==player){  
//	            File musicFile=new File("./music/ding.wav");//得到一个MP3文件  
	        	File musicFile=new File("music/ding.wav");//得到一个MP3文件,不加斜杠表示根目录
	            if(musicFile.exists()){  
	                MediaLocator  locator=new MediaLocator("file:"+musicFile.getAbsolutePath());  
	                player = Manager.createRealizedPlayer(locator);  
	                player.prefetch();// 预读文件  
	            }else{  
	                System.err.println(musicFile+"  找不到");  
	            }  
	        }
	        player.start();//播放  
	    } catch (CannotRealizeException ex) {  
	        ex.printStackTrace();  
	    } catch (NoPlayerException ex) {  
	        ex.printStackTrace();  
	    } catch (IOException ex) {  
	        ex.printStackTrace();  
	    }   
	}   
	
	
	/**
	 * 播放鼓声
	 */
	public void playGuMusic(){  
	    try {  
	        javax.media.Player player=null;  
	        if(null==player){  
//	            File musicFile=new File("./music/ding.wav");//得到一个MP3文件  
	        	File musicFile=new File("music/dada.wav");//得到一个MP3文件,不加斜杠表示根目录
	            if(musicFile.exists()){  
	                MediaLocator  locator=new MediaLocator("file:"+musicFile.getAbsolutePath());  
	                player = Manager.createRealizedPlayer(locator);  
	                player.prefetch();// 预读文件  
	            }else{  
	                System.err.println(musicFile+"  找不到");  
	            }  
	        }
	        player.start();//播放  
	    } catch (CannotRealizeException ex) {  
	        ex.printStackTrace();  
	    } catch (NoPlayerException ex) {  
	        ex.printStackTrace();  
	    } catch (IOException ex) {  
	        ex.printStackTrace();  
	    }   
	}   
}
 

 

 

 

refurl:http://helin.iteye.com/blog/264497

 

你可能感兴趣的:(音乐)