高分求一段J2ME源代码

今年大学选修课选是J2ME手机程序开发,一直没去听,作业不会写了,希望大家来帮帮忙啊,题目是实现一个简单的MP3播放器,平台是jdk+Eclipse+wtk2.5,请提供... 今年大学选修课选是J2ME手机程序开发,一直没去听,作业不会写了,希望大家来帮帮忙啊,题目是实现一个简单的MP3播放器,平台是jdk+Eclipse+wtk2.5,请提供可以运行成功的代码,谢谢啦
你写好后,我在本机运行通过的话,立刻给你分数
展开
 我来答
putian74
2008-12-06 · TA获得超过353个赞
知道小有建树答主
回答量:177
采纳率:0%
帮助的人:114万
展开全部
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import javax.microedition.midlet.*;
import java.io.*;
import javax.microedition.io.*;

public class PlayerMIDlet extends MIDlet implements CommandListener {
private Display display;
private List lst;
private Command start = new Command("Play",Command.SCREEN, 1);
private Command exit = new Command("Exit",Command.EXIT,2);

int currentPlaying = -1;

//Sound Player object.
static Player bbsounds[];
private Player tonePlayer;

public PlayerMIDlet(){
display = Display.getDisplay(this);
sound_init();
createTonePlayer();
}

protected void startApp() {
String[] elements = {"Play >> airhorn.wav","Play >> intro.midi","Play >> TuneSequence"};
lst = new List("Menu", List.IMPLICIT, elements, null);
lst.setCommandListener(this);
lst.addCommand(start);
display.setCurrent(lst);
}

private void reset(){
}

protected void pauseApp() {
try {
if(bbsounds[currentPlaying] != null && bbsounds[currentPlaying].getState() == bbsounds[currentPlaying].STARTED) {
bbsounds[currentPlaying].stop();
} else {
defplayer();
}
}catch(MediaException me) {
reset();
}
}

protected void destroyApp(boolean unconditional) {

lst = null;
try {
defplayer();
}catch(MediaException me) { }
}

public void commandAction(Command c , Displayable d){
int index = lst.getSelectedIndex();

System.out.println("Playing Sound .....");
if(c == start){
if (index == 0){
sound_play(0);
}
if(index == 1){
sound_play(1);
}
if(index == 2){
playTones();
}
}

if(c == exit){
this.destroyApp(true);
}
}

void defplayer() throws MediaException {
try{

if (bbsounds[currentPlaying] != null) {

if(bbsounds[currentPlaying].getState() == bbsounds[currentPlaying].STARTED) {
bbsounds[currentPlaying].stop();
}
if(bbsounds[currentPlaying].getState() == bbsounds[currentPlaying].PREFETCHED) {
bbsounds[currentPlaying].deallocate();
}
if(bbsounds[currentPlaying].getState() == bbsounds[currentPlaying].REALIZED ||
bbsounds[currentPlaying].getState() == bbsounds[currentPlaying].UNREALIZED) {
bbsounds[currentPlaying].close();
}
}
bbsounds = null;
}catch(Exception e){
}
}

public void sound_init()
{
try
{
bbsounds = new Player[ 3 ];
InputStream in = getClass().getResourceAsStream("airhorn.wav");

try
{
bbsounds[0] = Manager.createPlayer(in, "audio/X-wav");
}catch( Exception e ){
//System.out.println("Exception in Sound Creation ");
}
in = null;

InputStream is = getClass().getResourceAsStream("intro.midi");
try
{
bbsounds[1] = Manager.createPlayer(is, "audio/midi");
is = null;

}catch( Exception e ){
//System.out.println("Exception in Sound Creation ");
}
}catch( Exception e){}
}

public void sound_play(int id)
{
//System.out.println("Playing ID is >>"+id);

sound_stop( currentPlaying );
currentPlaying = id;
try
{
bbsounds[ id ].realize();

System.out.println("Playing Sound...");
bbsounds[ id ].start();

}catch(Exception e){
//System.out.println("Playing Exception");
}
return;
}

public void sound_stop( int id)
{
try{
if(id!=-1){
bbsounds[ id ].deallocate();
bbsounds[ id ].stop();
tonePlayer.stop();
}
}catch(Exception ex){
//System.out.println("Stop Sound Exception ");
}
return;
}

private void createTonePlayer() {
/**
* "Mary Had A Little Lamb" has "ABAC" structure.
* Use block to repeat "A" section.
*/
byte tempo = 30; // set tempo to 120 bpm
byte d = 8; // eighth-note

byte C4 = ToneControl.C4;;
byte D4 = (byte)(C4 + 2); // a whole step
byte E4 = (byte)(C4 + 4); // a major third
byte G4 = (byte)(C4 + 7); // a fifth
byte rest = ToneControl.SILENCE; // rest

byte[] mySequence = {
ToneControl.VERSION, 1, // version 1
ToneControl.TEMPO, tempo, // set tempo
ToneControl.BLOCK_START, 0, // start define "A" section
E4,d, D4,d, C4,d, E4,d, // content of "A" section
E4,d, E4,d, E4,d, rest,d,
ToneControl.BLOCK_END, 0, // end define "A" section
ToneControl.PLAY_BLOCK, 0, // play "A" section
D4,d, D4,d, D4,d, rest,d, // play "B" section
E4,d, G4,d, G4,d, rest,d,
ToneControl.PLAY_BLOCK, 0, // repeat "A" section
D4,d, D4,d, E4,d, D4,d, C4,d // play "C" section
};

try{
tonePlayer = Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR);
tonePlayer.realize();
ToneControl c = (ToneControl)tonePlayer.getControl("ToneControl");
c.setSequence(mySequence);

} catch (IOException ioe) {System.err.println("Initializing Tone");}
catch (MediaException me) { }

}
public void playTones() {

if (tonePlayer != null){
try {
System.out.println("Playing Sound...");
tonePlayer.prefetch();
tonePlayer.start();

} catch (MediaException me) {

//System.err.println("Problem starting player");
} // end catch
} // end if
} // end playTone
}

import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
public class AudioMidlet extends MIDlet
implements CommandListener, Runnable {
private Display mDisplay;
private List main;
Player player=null;
VolumeControl vc;
public void startApp() {
mDisplay = Display.getDisplay(this);
main = new List("AudioMIDlet", List.IMPLICIT);
main.append("music1", null);
main.append("music2", null);
main.append("music3", null);
main.addCommand(new Command("Exit", Command.EXIT, 0));
main.addCommand(new Command("Play", Command.SCREEN, 0));
main.setCommandListener(this);
mDisplay.setCurrent(main);
}

public void pauseApp() {}

public void destroyApp(boolean unconditional) {}

public void commandAction(Command c, Displayable s) {
if (c.getCommandType() == Command.EXIT)
notifyDestroyed();
else {
Form waitForm = new Form("Loading...");
mDisplay.setCurrent(waitForm);
Thread t = new Thread(this);
t.start();
}
}

public void run() {
playFromResource();
}

private void playFromResource() {
try {
int i=main.getSelectedIndex();
InputStream in=null;
switch(i)
{
case 0:
in = getClass().getResourceAsStream("/chimes.wav");
player = Manager.createPlayer(in, "audio/x-wav");
player.start();
break;
case 1:
in = getClass().getResourceAsStream("/chord.wav");
player = Manager.createPlayer(in, "audio/x-wav");
player.start();
break;
case 2:
in = getClass().getResourceAsStream("/music.wav");
player = Manager.createPlayer(in,"audio/x-wav");
player.start();
break;
}
}
catch (Exception e) {
showException(e);
return;
}
mDisplay.setCurrent(main);
}

private void showException(Exception e) {
Alert a = new Alert("Exception", e.toString(), null, null);
a.setTimeout(Alert.FOREVER);
mDisplay.setCurrent(a, main);
}
}

//把上面两个java文件放在一个包里,我的是在Netbeans里编的,还要属性里改一下,就是在MIDlet里增加一下
//另外你要播放什么文件自己加在包里,程序里你也可以适当改一下参数
AiPPT
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图... 点击进入详情页
本回答由AiPPT提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式