www.pudn.com > MulPlayer.rar > MulPlayer.java


//1种或2种声音的同时播放 
import javax.microedition.midlet.*; 
import javax.microedition.io.*; 
import javax.microedition.lcdui.*; 
 
import javax.microedition.media.*; 
import javax.microedition.media.control.*; 
import java.util.*; 
import java.io.*; 
 
import javax.microedition.lcdui.ChoiceGroup; 
 
 
public class MulPlayer extends MIDlet implements CommandListener 
{         
    byte[][] imageData = { 
            {-119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 
            82, 0, 0, 0, 12, 0, 0, 0, 12, 8, 0, 0, 0, 0, 115, 30, 3, 59, 
            0, 0, 0, 99, 73, 68, 65, 84, 120, -38, 85, -114, -79, 13, -64, 
            48, 8, 4, -65, -92, -12, 26, 46, 41, -67, 66, 102, -93, 116, 
            -23, 54, 35, 100, -115, -84, -63, 8, -108, 4, 72, -94, 40, 
            20, -16, 39, -92, -1, -121, -69, -51, -47, -38, 88, -26, 14, 
            87, 6, -88, 3, -84, 14, 11, 13, 57, 99, -79, 65, -128, 77, 
            45, 33, 100, 62, -80, 23, 48, 40, -49, 42, -96, 63, -16, 7, 
            -100, 6, 47, -56, 99, 125, -36, -42, -82, 29, 53, 25, 26, 117, 
            -124, -119, -58, -116, 58, 23, -64, 120, 44, 22, -20, -63, 
            84, -76, 0, 0, 0, 72, 116, 69, 88, 116, 83, 111, 102, 116, 
            119, 97, 114, 101, 0, 64, 40, 35, 41, 73, 109, 97, 103, 101, 
            77, 97, 103, 105, 99, 107, 32, 53, 46, 49, 46, 49, 32, 48, 
            48, 47, 48, 50, 47, 48, 49, 32, 81, 58, 49, 54, 32, 99, 114, 
            105, 115, 116, 121, 64, 109, 121, 115, 116, 105, 99, 46, 101, 
            115, 46, 100, 117, 112, 111, 110, 116, 46, 99, 111, 109, 14, 
            75, 17, -72, 0, 0, 0, 42, 116, 69, 88, 116, 83, 105, 103, 110, 
            97, 116, 117, 114, 101, 0, 55, 98, 99, 101, 101, 54, 98, 97, 
            99, 52, 53, 51, 97, 57, 55, 56, 48, 101, 57, 98, 48, 98, 100, 
            98, 53, 102, 100, 50, 52, 102, 54, 49, -90, -117, 41, -104, 
            0, 0, 0, 0, 73, 69, 78, 68, -82, 66, 96, -126},  
            {-119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 
            82, 0, 0, 0, 12, 0, 0, 0, 12, 8, 0, 0, 0, 0, 115, 30, 3, 59, 
            0, 0, 0, 123, 73, 68, 65, 84, 120, -38, 29, -114, -95, 21, 
            -61, 48, 12, 68, 63, 52, 52, 44, 53, 44, 20, -12, 10, 93, -93, 
            -80, -80, -48, 43, 8, 6, -122, 102, -124, -84, -48, 17, 2, 
            75, 13, 11, 5, 85, -55, 71, -92, 123, -70, -9, 117, -72, -37, 
            -42, 107, -19, -69, -71, -29, 83, -32, -34, 64, -90, 99, 66, 
            -69, -26, -17, 83, 17, 67, -31, -72, 104, 22, 83, -119, -112, 
            14, -8, -98, 17, -92, -112, 18, 123, 19, -21, 50, -113, 121, 
            -106, 52, 17, 67, 109, -84, 115, 2, 94, -10, 20, -71, 37, 32, 
            -48, -5, 12, -115, 68, -81, -89, 43, 20, 79, -93, -114, 74, 
            41, 125, -117, 58, 127, 84, -39, 53, 96, -97, 47, 127, -93, 
            0, 0, 0, 72, 116, 69, 88, 116, 83, 111, 102, 116, 119, 97, 
            114, 101, 0, 64, 40, 35, 41, 73, 109, 97, 103, 101, 77, 97, 
            103, 105, 99, 107, 32, 53, 46, 49, 46, 49, 32, 48, 48, 47, 
            48, 50, 47, 48, 49, 32, 81, 58, 49, 54, 32, 99, 114, 105, 115, 
            116, 121, 64, 109, 121, 115, 116, 105, 99, 46, 101, 115, 46, 
            100, 117, 112, 111, 110, 116, 46, 99, 111, 109, 14, 75, 17, 
            -72, 0, 0, 0, 42, 116, 69, 88, 116, 83, 105, 103, 110, 97, 
            116, 117, 114, 101, 0, 56, 53, 51, 54, 52, 55, 57, 51, 97, 
            99, 49, 56, 97, 99, 56, 52, 102, 98, 100, 50, 55, 97, 99, 57, 
            49, 100, 97, 97, 54, 98, 56, 99, -81, 37, -40, 124, 0, 0, 0, 
            0, 73, 69, 78, 68, -82, 66, 96, -126}, 
            {-119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 
            82, 0, 0, 0, 12, 0, 0, 0, 12, 8, 0, 0, 0, 0, 115, 30, 3, 59, 
            0, 0, 0, 120, 73, 68, 65, 84, 120, -38, 37, 78, -85, 21, -61, 
            64, 12, 19, 52, -12, 26, -127, -122, 94, -91, -76, 48, -80, 
            43, 20, 30, 12, -20, 10, 7, -69, 70, 96, 96, -88, 97, -96, 
            -95, 43, -89, 38, -106, -34, -45, 15, 85, -71, -71, -86, 127, 
            -78, 10, 21, 6, 113, 23, 88, 20, -46, -96, -25, 113, -18, 10, 
            75, 12, -32, 21, -94, -75, -126, -48, 0, 93, 112, 19, -22, 
            -63, -101, 113, -16, -53, -97, -8, 51, -41, 38, -108, 61, -24, 
            -37, -65, 45, 35, 24, -105, -7, -11, -18, -128, -114, -98, 
            -103, 83, 58, -70, 98, -63, 109, -20, 82, -50, 25, 38, 106, 
            27, -25, -4, 0, 81, -72, 53, 99, -4, -21, -115, 76, 0, 0, 0, 
            72, 116, 69, 88, 116, 83, 111, 102, 116, 119, 97, 114, 101, 
            0, 64, 40, 35, 41, 73, 109, 97, 103, 101, 77, 97, 103, 105, 
            99, 107, 32, 53, 46, 49, 46, 49, 32, 48, 48, 47, 48, 50, 47, 
            48, 49, 32, 81, 58, 49, 54, 32, 99, 114, 105, 115, 116, 121, 
            64, 109, 121, 115, 116, 105, 99, 46, 101, 115, 46, 100, 117, 
            112, 111, 110, 116, 46, 99, 111, 109, 14, 75, 17, -72, 0, 0, 
            0, 42, 116, 69, 88, 116, 83, 105, 103, 110, 97, 116, 117, 114, 
            101, 0, 98, 57, 57, 48, 57, 98, 56, 51, 50, 52, 53, 97, 98, 
            99, 97, 52, 49, 52, 101, 100, 99, 54, 53, 97, 51, 57, 55, 102, 
            56, 57, 98, 53, -30, -75, 0, -115, 0, 0, 0, 0, 73, 69, 78, 
            68, -82, 66, 96, -126}, 
            {-119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 
            82, 0, 0, 0, 12, 0, 0, 0, 12, 8, 0, 0, 0, 0, 115, 30, 3, 59, 
            0, 0, 0, 115, 73, 68, 65, 84, 120, -38, 29, -115, 33, 18, -123, 
            48, 16, 67, 35, -111, -107, 88, 100, 101, 101, -81, 80, -119, 
            -60, 34, -111, 92, -93, -14, -53, 111, 43, 123, 37, 108, 101, 
            -27, -54, -80, 33, 98, -25, -67, -39, -52, 4, 36, -5, 30, 99, 
            105, 14, -96, 21, 0, 49, 32, 15, 23, 113, -31, 13, 100, 67, 
            119, 14, 67, -126, 31, -12, -24, 109, 74, 18, 86, -32, 24, 
            -63, 36, -117, -53, 54, -17, 104, 117, -107, 20, -100, 84, 
            -102, 106, 126, 60, 95, -83, -62, -78, -28, -71, 124, -53, 
            -64, -7, -103, -13, -29, -93, -76, 127, 10, 75, -86, 70, -66, 
            -22, 6, 54, -114, 91, -22, -68, -61, 0, 0, 0, 72, 116, 69, 
            88, 116, 83, 111, 102, 116, 119, 97, 114, 101, 0, 64, 40, 35, 
            41, 73, 109, 97, 103, 101, 77, 97, 103, 105, 99, 107, 32, 53, 
            46, 49, 46, 49, 32, 48, 48, 47, 48, 50, 47, 48, 49, 32, 81, 
            58, 49, 54, 32, 99, 114, 105, 115, 116, 121, 64, 109, 121, 
            115, 116, 105, 99, 46, 101, 115, 46, 100, 117, 112, 111, 110, 
            116, 46, 99, 111, 109, 14, 75, 17, -72, 0, 0, 0, 42, 116, 69, 
            88, 116, 83, 105, 103, 110, 97, 116, 117, 114, 101, 0, 99, 
            56, 57, 99, 56, 51, 57, 100, 52, 54, 57, 101, 50, 101, 53, 
            52, 48, 101, 52, 52, 101, 57, 53, 51, 49, 54, 56, 51, 51, 53, 
            98, 101, 102, -23, -33, -52, 0, 0, 0, 0, 73, 69, 78, 68, -82, 
            66, 96, -126}, 
            {-119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 
            82, 0, 0, 0, 12, 0, 0, 0, 12, 8, 0, 0, 0, 0, 115, 30, 3, 59, 
            0, 0, 0, 118, 73, 68, 65, 84, 120, -38, 21, -114, -95, 17, 
            -60, 48, 12, 4, 15, 10, 26, -122, 26, 26, 26, -86, 5, -105, 
            -16, -11, 24, 6, 26, -122, 6, 126, 43, 95, 66, 104, -96, -96, 
            -31, -3, 73, 108, 103, 118, -10, 4, -110, -105, 31, -59, -49, 
            77, -126, -31, -42, 90, 109, -42, 31, -127, -61, 25, 17, 3, 
            109, -29, -122, -64, -112, 119, 98, 36, 124, 127, 83, -36, 
            113, 36, -52, -15, 94, -128, 37, -44, 41, 37, 18, -92, -107, 
            15, -80, -34, -44, 20, -24, 28, -11, 89, 25, 80, 26, 51, -10, 
            93, 50, -83, 81, -39, 106, -75, 28, 37, -105, -101, -11, 124, 
            -25, 15, 35, 91, 55, 47, 66, 95, 127, -5, 0, 0, 0, 72, 116, 
            69, 88, 116, 83, 111, 102, 116, 119, 97, 114, 101, 0, 64, 40, 
            35, 41, 73, 109, 97, 103, 101, 77, 97, 103, 105, 99, 107, 32, 
            53, 46, 49, 46, 49, 32, 48, 48, 47, 48, 50, 47, 48, 49, 32, 
            81, 58, 49, 54, 32, 99, 114, 105, 115, 116, 121, 64, 109, 121, 
            115, 116, 105, 99, 46, 101, 115, 46, 100, 117, 112, 111, 110, 
            116, 46, 99, 111, 109, 14, 75, 17, -72, 0, 0, 0, 42, 116, 69, 
            88, 116, 83, 105, 103, 110, 97, 116, 117, 114, 101, 0, 101, 
            53, 55, 97, 49, 97, 99, 53, 97, 101, 52, 56, 97, 50, 48, 101, 
            97, 101, 51, 102, 100, 99, 100, 56, 55, 55, 100, 54, 48, 48, 
            98, 57, 36, 32, 28, 77, 0, 0, 0, 0, 73, 69, 78, 68, -82, 66, 
            96, -126}}; 
             
    Image ie[] = {Image.createImage(imageData[0], 0, imageData[0].length), 
            Image.createImage(imageData[1], 0, imageData[1].length), 
            Image.createImage(imageData[2], 0, imageData[2].length), 
            Image.createImage(imageData[3], 0, imageData[3].length), 
            Image.createImage(imageData[4], 0, imageData[4].length)};     
                    
	String[] se = {"Mid","Mmf", "Amr","Wav","Dat"};             
    int[] types = {Choice.EXCLUSIVE, Choice.MULTIPLE, Choice.POPUP}; 
     
	Display display; 
	
    Command exitCommand  = new Command("Exit", Command.EXIT, 2);
    Command okCommand = new Command("OK", Command.OK, 1); 
     
  	Form testForm; 
  	ChoiceGroup testChoiceGroup; 
 
	Player p = null;
	Player p2 = null;	
	
    InputStream is = null;
    InputStream is2 = null;	
        
    public MulPlayer() { 
 
 		display = Display.getDisplay(this); 
  
    	testForm = new Form("multimedia play tests");   
    	testChoiceGroup = new ChoiceGroup("MULTIPLE", types[1], se, ie);   
 
    	testForm.append(testChoiceGroup); 
   	 	display.setCurrent(testForm); 
  
    	testForm.addCommand(exitCommand);
    	testForm.addCommand(okCommand);
    	testForm.setCommandListener(this);   
          
    }	   
     
    public void startApp() {
    }
     
    public void pauseApp(){ 
	} 
	 
	public void destroyApp(boolean b){
		
		   
		notifyDestroyed(); 
	} 
	 
	public void commandAction(Command c, Displayable s){ 
        try {
            if (c == okCommand) { 
            	 System.out.println("okCommand"); 
           	  
		 
		if ((testChoiceGroup.isSelected(0) == true)&&(testChoiceGroup.isSelected(1) == false)
		   &&(testChoiceGroup.isSelected(2) == false)&&(testChoiceGroup.isSelected(3) == false)
		   &&(testChoiceGroup.isSelected(4) == false)) { 
			reply();		   	
			Thread th0 = new MidThread("mid");
       	    th0.start();  	
       	     
		} else if ((testChoiceGroup.isSelected(0) == false)&&(testChoiceGroup.isSelected(1) == true)
		   &&(testChoiceGroup.isSelected(2) == false)&&(testChoiceGroup.isSelected(3) == false)
		   &&(testChoiceGroup.isSelected(4) == false)) {
			reply(); 
			Thread th1 = new MidThread("mmf");
       	    th1.start();  			
       	     
		} else if ((testChoiceGroup.isSelected(0) == false)&&(testChoiceGroup.isSelected(1) == false)
		   &&(testChoiceGroup.isSelected(2) == true)&&(testChoiceGroup.isSelected(3) == false)
		   &&(testChoiceGroup.isSelected(4) == false)) {
			reply();			 
			Thread th2 = new MidThread("amr");
       	    th2.start();  			
       	     
		} else if ((testChoiceGroup.isSelected(0) == false)&&(testChoiceGroup.isSelected(1) == false)
		   &&(testChoiceGroup.isSelected(2) == false)&&(testChoiceGroup.isSelected(3) == true)
		   &&(testChoiceGroup.isSelected(4) == false)) {  
			reply();			 
			Thread th3 = new MidThread("wav");
       	    th3.start();  	
       	    		 
		} else if ((testChoiceGroup.isSelected(0) == false)&&(testChoiceGroup.isSelected(1) == false)
		   &&(testChoiceGroup.isSelected(2) == false)&&(testChoiceGroup.isSelected(3) == false)
		   &&(testChoiceGroup.isSelected(4) == true)) {
			reply();			 
			Thread th4 = new MidThread("dat");
       	    th4.start();  
       	    
		} else if ((testChoiceGroup.isSelected(0) == true)&&(testChoiceGroup.isSelected(1) == true)
		   &&(testChoiceGroup.isSelected(2) == false)&&(testChoiceGroup.isSelected(3) == false)
		   &&(testChoiceGroup.isSelected(4) == false)) {
			reply();			 
			Thread th0 = new MidThread("mid");
			Thread th1 = new MidThread("mmf");
       	    th0.start(); 
       	    th1.start();  
       	           	    
		} else if ((testChoiceGroup.isSelected(0) == true)&&(testChoiceGroup.isSelected(1) == false)
		   &&(testChoiceGroup.isSelected(2) == true)&&(testChoiceGroup.isSelected(3) == false)
		   &&(testChoiceGroup.isSelected(4) == false)) {
			reply();			 
			Thread th0 = new MidThread("mid");
			Thread th2 = new MidThread("amr");
       	    th0.start(); 
       	    th2.start();  	
       	           	    
		} else if ((testChoiceGroup.isSelected(0) == true)&&(testChoiceGroup.isSelected(1) == false)
		   &&(testChoiceGroup.isSelected(2) == false)&&(testChoiceGroup.isSelected(3) == true)
		   &&(testChoiceGroup.isSelected(4) == false)) {
			reply();			 
			Thread th0 = new MidThread("mid");
       	    Thread th3 = new MidThread("wav");
       	    th0.start();  			
       	    th3.start();  
       	          	    
		} else if ((testChoiceGroup.isSelected(0) == true)&&(testChoiceGroup.isSelected(1) == false)
		   &&(testChoiceGroup.isSelected(2) == false)&&(testChoiceGroup.isSelected(3) == false)
		   &&(testChoiceGroup.isSelected(4) == true)) {
			reply();			 
			Thread th0 = new MidThread("mid");
			Thread th4 = new MidThread("dat");
       	    th0.start();  
       	    th4.start();  			

		} else if ((testChoiceGroup.isSelected(0) == false)&&(testChoiceGroup.isSelected(1) == true)
		   &&(testChoiceGroup.isSelected(2) == true)&&(testChoiceGroup.isSelected(3) == false)
		   &&(testChoiceGroup.isSelected(4) == false)) {
			reply();			 
			Thread th1 = new MidThread("mmf");
			Thread th2 = new MidThread("amr");
       	    th1.start();
       	    th2.start(); 

		} else if ((testChoiceGroup.isSelected(0) == false)&&(testChoiceGroup.isSelected(1) == true)
		   &&(testChoiceGroup.isSelected(2) == false)&&(testChoiceGroup.isSelected(3) == true)
		   &&(testChoiceGroup.isSelected(4) == false)) {
			reply();			 
			Thread th1 = new MidThread("mmf");    	     
			Thread th3 = new MidThread("wav");
       	    th1.start();   			
       	    th3.start(); 
       	    
		} else if ((testChoiceGroup.isSelected(0) == false)&&(testChoiceGroup.isSelected(1) == true)
		   &&(testChoiceGroup.isSelected(2) == false)&&(testChoiceGroup.isSelected(3) == false)
		   &&(testChoiceGroup.isSelected(4) == true)) {
			reply();			 
			Thread th1 = new MidThread("mmf");    	     
			Thread th4 = new MidThread("dat");
       	    th1.start();   			
       	    th4.start();    

		} else if ((testChoiceGroup.isSelected(0) == false)&&(testChoiceGroup.isSelected(1) == false)
		   &&(testChoiceGroup.isSelected(2) == true)&&(testChoiceGroup.isSelected(3) == true)
		   &&(testChoiceGroup.isSelected(4) == false)) {
			reply();			 
			Thread th2 = new MidThread("amr");  	
			Thread th3 = new MidThread("wav");
       	    th2.start();			
       	    th3.start();       	           	           	           	           	          	           	           	           	    			
       	    
		} else if ((testChoiceGroup.isSelected(0) == false)&&(testChoiceGroup.isSelected(1) == false)
		   &&(testChoiceGroup.isSelected(2) == true)&&(testChoiceGroup.isSelected(3) == false)
		   &&(testChoiceGroup.isSelected(4) == true)) {
			reply();			 
			Thread th2 = new MidThread("amr");  	
			Thread th4 = new MidThread("dat");
       	    th2.start();	
       	    th4.start();  
		       	    
		} else if ((testChoiceGroup.isSelected(0) == false)&&(testChoiceGroup.isSelected(1) == false)
		   &&(testChoiceGroup.isSelected(2) == false)&&(testChoiceGroup.isSelected(3) == true)
		   &&(testChoiceGroup.isSelected(4) == true)) { 	
			reply();			 
			Thread th3 = new MidThread("wav");
			Thread th4 = new MidThread("dat");
       	    th3.start(); 
       	    th4.start();  		 
		}  else { 
			System.out.println("Please choice midea more than two "); 
		}       	  

//退出功能
       	 
            } else if (c == exitCommand) {
			testChoiceGroup.setSelectedIndex(0,false);		
       		testChoiceGroup.setSelectedIndex(1,false);		
       		testChoiceGroup.setSelectedIndex(2,false);		
       		testChoiceGroup.setSelectedIndex(3,false);		
       		testChoiceGroup.setSelectedIndex(4,false);	
				System.out.println("-------------exitCommand-------");    				        	
        		if (is != null) {
        			is.close();
					System.out.println("-------------is.close-------");        
        			is = null;
        		}	  
        		if (is2 != null) {
        			is2.close();
					System.out.println("-------------is2.close-------");        
        			is2 = null;        		          				
        	    }
        		if (p != null) {
        			p.stop();
					System.out.println("-------------p.stop-------");        
        			p = null;
        		}	            	
        		if (p2 != null) {
        			p2.stop();
					System.out.println("-------------p2.stop-------");        
        			p2 = null;
        		}	
        		        		
                destroyApp(true);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } 
	} 
 
	private void reply() { 
	try{ 
		if (is != null) {
        	is.close();
 			is = null;
        }	  
        if (is2 != null) {
        	is2.close();
			is2 = null;        		          				
   	    } 
   	    if (p != null) {
        	p.stop();	        
        	p = null;
        }	            	
        if (p2 != null) {
        	p2.stop();   
        	p2 = null;
        }	 
    } catch (Exception io) { 
	    io.printStackTrace();     	 
    } 
	} 
	
    class MidThread extends Thread {
    	
    	String name,pid;
    	
    	public MidThread( String num )
    	{
    		name = num;
    		
    		if (name == "mid"){
    			pid = "audio/midi";
    			
    		} else if (name == "mmf"){
    			pid = "application/vnd.smaf";
    			    			    			
    		} else if (name == "amr"){
    			pid = "audio/amr";
    			    			
    		} else if (name == "wav"){
    			pid = "audio/x-wav";
    			    			
    		} else if (name == "dat"){
    			pid = "audio/x-tone-seq";
    			    			
    		}

    	}

    	
    	public void run(){
       		 
    		try 
    		{ 
    		String nthURL=name+"play."+name;
    		String url = "resource:/audio/"+nthURL;
    		System.out.println("url:"+url);
    		if (is == null) {  		 
        		is = getClass().getResourceAsStream(url.substring(9));
        	
        	System.out.println("is:"+url.substring(9));  
        	} else if (is2 == null) {
        		is2 = getClass().getResourceAsStream(url.substring(9));
        		System.out.println("is2:"+url.substring(9));  
        	}	 
 
    		} catch (Exception ex){
    	        ex.printStackTrace();	 
    		} 
 
 
 			try
 			{
 			if (p == null) {	 
     			p = Manager.createPlayer(is,pid);
	        	System.out.println("----------createplayer-----------");     		 
//	     		p.prefetch(); // opens the MIDI device 
//     		MIDIControl m = (MIDIControl)p.getControl("MIDIControl");
			
				System.out.println("----------getControl-----------");     		 
	        	p.start();	
	            System.out.println("----------p.start----------"); 
            } else if (p != null) {
     			p2 = Manager.createPlayer(is2,pid);
	        	System.out.println("----------createplayer2-----------");     		 
//	     		p2.prefetch(); // opens the MIDI device 
//     		MIDIControl m = (MIDIControl)p.getControl("MIDIControl");
			
				System.out.println("----------getControl2-----------");     		 
	        	p2.start();	
	            System.out.println("----------p2.start----------"); 
        	}	     		
       	 
			} catch (IOException ioe) { 
				ioe.printStackTrace(); 		
 			} catch (MediaException me){
 		        me.printStackTrace();
 			} 

    	}
    	
	} 

}