ExPlayer.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:3k
源码类别:

J2ME

开发平台:

Java

  1. package exframework;
  2. /**
  3.  * <p>Title: ExFramework</p>
  4.  *
  5.  * <p>Description: lizhenpeng</p>
  6.  *
  7.  * <p>Copyright: Copyright (c) 2005</p>
  8.  *
  9.  * <p>Company: LP&P</p>
  10.  *
  11.  * @author lipeng
  12.  * @version 1.0
  13.  */
  14. import javax.microedition.media.*;
  15. import javax.microedition.media.control.*;
  16. import java.io.*;
  17. public class ExPlayer implements AllAction,PlayerListener
  18. {
  19.   public void playerUpdate(Player player,String event,Object eventData)
  20.   {
  21.     if(event==PlayerListener.END_OF_MEDIA)
  22.     {
  23.       try
  24.       {
  25.         player.start();
  26.       }
  27.       catch(Exception e)
  28.       {
  29.         System.out.println(e);
  30.       }
  31.     }
  32.   }
  33.   public ExPlayer()
  34.   {
  35.   }
  36.   public void allAction(MainForm form)
  37.   {
  38.     /*
  39.          String []contentType=Manager.getSupportedContentTypes(null);
  40.          for(int i = 0;i<contentType.length;i++)
  41.          {
  42.       System.out.println(contentType[i]);
  43.       String[] protocol = Manager.getSupportedProtocols(contentType[i]);
  44.       for(int j= 0;j<protocol.length;j++)
  45.       {
  46.         System.out.print(protocol[j]+",");
  47.       }
  48.       System.out.println();
  49.          }
  50.     try
  51.     {
  52. //      Manager.playTone(100,1000,100);
  53.       byte tempo=50; // set tempo to 120 bpm
  54.       byte d=8; // eighth-note
  55.       byte C4=ToneControl.C4;
  56.       byte D4=(byte)(C4+2); // a whole step
  57.       byte E4=(byte)(C4+4); // a major third
  58.       byte G4=(byte)(C4+7); // a fifth
  59.       byte rest=ToneControl.SILENCE; // rest
  60.       byte[] mySequence=
  61.       {
  62.         ToneControl.VERSION,1,  //设置版本号,当前必须设为1
  63.         ToneControl.TEMPO,tempo, //设置声音播放速度,值越大,播放越快
  64.         ToneControl.SET_VOLUME,100, //设置音量,值越大,音量越大
  65.         ToneControl.BLOCK_START,0, //预定义播放块,当前块号为0
  66.         E4,d,D4,d,C4,d,E4,d,
  67.         E4,d,E4,d,E4,d,rest,d, //块的内容
  68.         ToneControl.BLOCK_END,0,  //块定义结束符
  69.         ToneControl.PLAY_BLOCK,0,  //播放当前块号为0的块,块号必须提前定义
  70.         D4,d,D4,d,D4,d,rest,d, //不使用块号方式播放的内容,必须位于块定义后面
  71.       };
  72.       Player p=Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR);
  73.       p.realize();
  74.       //
  75.       ToneControl c=(ToneControl)p.getControl("ToneControl");
  76.       c.setSequence(mySequence);
  77.       p.prefetch();
  78.       p.start();
  79.     }
  80.     catch(Exception e)
  81.     {
  82.     }
  83.     try
  84.     {
  85.       InputStream is=getClass().getResourceAsStream("/"+"Testsound.mid");
  86.       Player player=Manager.createPlayer(is,"audio/midi");
  87.       player.realize();
  88.       player.prefetch();
  89.       //System.out.println(player.getMediaTime());
  90.       //System.out.println(player.getDuration());
  91.       player.start();
  92.       VolumeControl control=(VolumeControl)player.getControl("VolumeControl"); //获得控制接口,此接口的获得必须在获取播放信息或者获取播放设备后面,如果在它们前面,将会出现无法播放声音的情况
  93.       if(control!=null) //必须检查是否为null,因为有些声音格式可能不支持音量控制
  94.       {
  95.         control.setLevel(5); //设置音量级别为50
  96. //control.setMute(true); //设置静音
  97.       }
  98.       //System.out.println(player.getMediaTime());
  99.       //player.addPlayerListener(this);
  100.     }
  101.     catch(Exception e)
  102.     {
  103.       System.out.println(e);
  104.     }  */
  105.     LPAudioPlayer player = new LPAudioPlayer("Testsound.mid","audio/midi");
  106.     //LPAudioPlayer player2 = new LPAudioPlayer("bg.mid","audio/midi",true);
  107.     player.threadPlay();
  108.   }
  109. }