audioplayer.java

来自「<j2me 开发精解> 詹建光著 里所有的源码。对J2me的开发相当」· Java 代码 · 共 73 行

JAVA
73
字号
package com.j2medev.ch8.fc;

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;

public class AudioPlayer implements Runnable,PlayerListener,CommandListener {
    
    private FileController controller = null;
    private Player player = null;
    private InputStream is = null;
    private Form form = null;
    public static final Command backCommand = new Command("返回",Command.BACK, 1);

    public AudioPlayer(FileController controller) {
        this.controller = controller;
        
    }
    
    public void initVideo(InputStream is,String fileName){
        form = new Form(fileName);
        form.addCommand(backCommand);
        form.setCommandListener(this);
        this.is = is;
        new Thread(this).start();
    }
    
    public void run(){
        try{
            if(player == null)
                player = javax.microedition.media.Manager.createPlayer(is, "audio/midi");
            player.addPlayerListener(this);
            player.realize();
            player.prefetch();
            player.start();
            
        }catch(MediaException ex){
            ex.printStackTrace();
            closePlayer();
        }catch(IOException ex){
            ex.printStackTrace();
            closePlayer();
        }
    }
    
    public void closePlayer(){
        if (player != null) {
            player.close();
            player = null;
        }
    }
    
    public void playerUpdate(Player player,String event,Object obj){
        if(event.equals(PlayerListener.STARTED)){
            form.append("播放音乐");
            controller.setCurrent(form);
        }else if(event.equals(PlayerListener.END_OF_MEDIA)){
            form.deleteAll();
            form.append("音乐结束");
        }
    }
    
    public void commandAction(Command cmd,Displayable displayable){
        if(cmd == backCommand){
            if(player != null){
                closePlayer();
            }
            controller.showCurrentDir();
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?