J2ME音频播放核心代码

J2ME音频播放:一种是自己编写一段音频(存放在数组中);另一种是加载音频资源文件。
 
Player实例的状态有五种:
1.Unrealized 未实现
2.Realized   以实现
3.Prefetched 预抓取
4.Started    开始
5.Closed     关闭
Player实例要获取系统资源并处理媒体内容以确定其内容类型或执行其他操作,可以通过调用实例的deallocate方法停止实现过程,但必须在Player实例完成到已实现状态的转变之前进行调用,一旦实现,Player实例将不能返回到未实现状态。   

package com.mopietek;

import java.io.IOException;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.ToneControl;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class SequenceTone extends MIDlet{

	
	public Display display;
	public Form form;
	
	public SequenceTone(){
		display = Display.getDisplay(this);
		form = new Form("Info Form");
	}
	
	
	
	public void toneSequence(){
		
		byte tempo = 30;
		byte d = 8;
		byte C4 = ToneControl.C4;
		byte D4 = (byte) (C4 + 2);
		byte E4 = (byte) (C4 + 4);
		byte F4 = (byte) (C4 + 5);
		byte G4 = (byte) (C4 + 7);
		byte rest = ToneControl.SILENCE;
		byte [] mySequence = {
				ToneControl.VERSION,1,ToneControl.TEMPO,tempo,ToneControl.BLOCK_START,
				0,C4, d, F4, d, F4, d, C4, d, F4, d, F4, d, C4, d, F4, d,
				ToneControl.BLOCK_END,0,ToneControl.BLOCK_START,1,C4, d, E4, d, E4, C4, d, E4,ToneControl.BLOCK_END,1,
				ToneControl.PLAY_BLOCK,0,
				ToneControl.PLAY_BLOCK,1,
				ToneControl.PLAY_BLOCK,0
				};
		
		try{
		     //创建Player                      //资源
		     Player p = Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR);
		     p.realize();  //已实现
		     ToneControl c = (ToneControl) p.getControl("ToneControl");
		     c.setSequence(mySequence);
		     p.prefetch();   //预抓取
		     p.start();   //读取音频
		}catch(IOException e){
			System.out.println("IO异常:"+e.toString());
			e.printStackTrace();
		}catch(Exception ee){
			System.out.println("异常:"+ee.toString());
			ee.printStackTrace();
		}
	}
	
	protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {

		 display = null;
		 form = null;
	}

	protected void pauseApp() {
		// TODO Auto-generated method stub
		
	}

	protected void startApp() throws MIDletStateChangeException {
		// TODO Auto-generated method stub
		
		display.setCurrent(form);
		toneSequence();
	}
	
	

}



通过资源文件获取方式代码:
try{
			//获取音频文件  
			InputStream is = getClass().getResourceAsStream(soundFile);
			player = Manager.createPlayer(is,soundType);
			player.realize();
			player.prefetch();
			player.start();
			
		}catch(MediaException ex){
			ex.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}

你可能感兴趣的:(C++,c,C#,REST)