J2SE phone
這次為了要趕工做出一個Voip 的實作
所以就特別寫了一個 Java 使用 UDP 的Voip 傳送資料

以下就先列出 Connect 的程式碼

ffyy99 發表在 痞客邦 留言(2) 人氣()

出處 http://lklkdawei.blog.163.com/blog/static/32574109200861031129157/
by 強大的大陸人
 
播放的是mp3格式音乐,需要

ffyy99 發表在 痞客邦 留言(0) 人氣()

package SST.com.tw;
import java.io.*;
import javax.sound.sampled.*;
public class AP_Main {
    static ByteArrayOutputStream bos = new ByteArrayOutputStream();
    public static void main(String args[]) {
        try {
            File file = new File ("D:\\aa.au");
            InputStream in = new FileInputStream(file);
            AudioInputStream AIS = AudioSystem.getAudioInputStream(file);
            AudioFormat format = AIS.getFormat();
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
            SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
            line.open(format);
            line.start();
            int bufferSize = (int) format.getSampleRate() * format.getFrameSize();
            byte[] buffer = new byte[bufferSize];
            try {
                int bytesRead = 0;
                while (bytesRead >= 0) {
                    bytesRead = AIS.read(buffer, 0, buffer.length);
                    if (bytesRead >= 0) {
                        line.write(buffer, 0, bytesRead);
                    }
                }
            } catch (IOException e) {
                System.out.println("" + e.toString());
            }
            line.close();
            System.out.println("program over");
       } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}

ffyy99 發表在 痞客邦 留言(0) 人氣()

package SST.com.tw;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.*;
public class RecorderAudio extends Thread {
    static TargetDataLine m_targetdataline;
    static AudioFileFormat.Type m_targetType;
    static AudioInputStream m_audioInputStream;
    static File m_outputFile;
    static ByteArrayOutputStream bos = new ByteArrayOutputStream();
    static byte[] buf;
    static boolean m_bRecording;
    public RecorderAudio(TargetDataLine line, AudioFileFormat.Type targetType,
                        File file) {
        m_targetdataline = line;
        m_audioInputStream = new AudioInputStream(line);
        m_targetType = targetType;
        m_outputFile = file;
    }
    public static void AudioRecorder() {
        String Filename = "d://aa.wav";
        File outputFile = new File(Filename);
        AudioFormat audioFormat = null;
        audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 8000.0F,
                                      8, 1, 1, 8000.0F, false);
        // telephone sample rate is 8KHz
        DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat);
        System.out.println(info.toString());
        TargetDataLine targetDataLine = null;
        
        try {
            targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
            targetDataLine.open(audioFormat);
        } catch (LineUnavailableException e) {
            System.out.println("audio record fail");
            System.exit(-1);
        }
        AudioFileFormat.Type targetType = AudioFileFormat.Type.AU;
        RecorderAudio recorder = null;
        recorder = new RecorderAudio(targetDataLine, targetType, outputFile);
        recorder.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException ex) {
            System.out.println(ex.toString());
        }
        recorder.stopRecording();
        try {
            FileOutputStream fos = new FileOutputStream(m_outputFile);
            fos.write(buf);
            System.out.println("file write");
            fos.close();
        } catch (Exception e) {
            System.out.println("file write");
        }
    }
    public void start() {
        m_targetdataline.start();
        super.start();
        System.out.println("recording...");
    }
    public static void stopRecording() {
        m_targetdataline.stop();
        m_targetdataline.close();
        m_bRecording = false;
        buf = bos.toByteArray();
        System.out.println("stopped");
    }
    public void run() {
        try {
            AudioSystem.write(m_audioInputStream, m_targetType, bos);
            System.out.println("after write()");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

ffyy99 發表在 痞客邦 留言(0) 人氣()





package SST.com.tw;
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.bluetooth.*;
import java.util.Vector;
import javax.microedition.io.*;
public class BTCV extends MIDlet implements DiscoveryListener, CommandListener, Runnable{
// Base ------------------------------------------
    Display dsp;
    Form frm;
    ChoiceGroup cg;
    Form saveFrm;
// Command -----------------------------------------
    Command OK;
    Command Exit;
    Command Send;
// BlueTooth ---------------------------------------
    LocalDevice local = null;       // Local Device Info
    DiscoveryAgent agent = null;    // Device Scanner
    Vector devicesFound = null;     // Remote device Info
    StreamConnection conn;
    DataInputStream dis = null;
    DataOutputStream dos = null;
    String url;
    Thread readThread = null;
    public BTCV () {
        dsp = Display.getDisplay(this);
        frm = new Form("BT SP");
        try {
            local = LocalDevice.getLocalDevice();
        } catch (BluetoothStateException ex) {
        }
        cg = new ChoiceGroup("Select Bluetooth Device,"+local.getFriendlyName().toString(), ChoiceGroup.EXCLUSIVE);
        OK = new Command("OK", Command.OK, 1);
        Exit = new Command("Exit", Command.EXIT, 1);
        Send = new Command("Send", Command.OK, 1);
        frm.append(cg);
        frm.addCommand(OK);
        frm.addCommand(Exit);
        frm.setCommandListener(this);
        readThread = new Thread(this);
    }
    
    public void startApp() {
        dsp.setCurrent(frm);
        scanOverFlag = false;
        runFlag = false;
        doDeviceDiscovery();
    }
    public void pauseApp() {
    }
    public void destroyApp(boolean unconditional) {
        this.notifyDestroyed();
    }
    boolean scanOverFlag;
    int cc;
    public void commandAction(Command cmnd, Displayable dsplbl) {
        if (cmnd == OK) {
            do {
                if (!scanOverFlag)      // waiting scan
                {
                    break;
                }   //cg.append("OK ", null);
                try {
                    RemoteDevice rd = (RemoteDevice) devicesFound.elementAt(cg.getSelectedIndex());
                    agent.searchServices(null, new UUID[]{SERIAL}, rd, this);
                } catch (BluetoothStateException ex) {
                    //ex.printStackTrace();
                    cg.append("searchService Error", null);
                }
                if (url == null)
                {
                    break;
                }
                try {
                    conn = (StreamConnection) Connector.open(url);
                    dos = conn.openDataOutputStream();
                    dis = conn.openDataInputStream();
                    runFlag = true;
                    frm.removeCommand(OK);
                    frm.addCommand(Send);
                    readThread.start();
                    readThreadFlag = false;
                } catch (IOException ex) {
                    frm.append("connect error");
                }
                
            } while (false);
        }else if (cmnd == Exit) {
            try {
                conn.close();
                readThreadFlag = true;
            } catch (IOException ex) {
                //ex.printStackTrace();
            }
            this.destroyApp(false);
        }else if (cmnd == Send) {
            try {
                if (dos != null) {
                    dos.writeChars(Integer.toString(cc));
                    cc++;
                    //dos.writeByte(0x30);
                    //dos.close();
                    
                }
            }catch (IOException ex) {
                frm.append("write error");
            }
        }
    }
    private void doDeviceDiscovery() {
        devicesFound = new Vector();
        try {
            agent = local.getDiscoveryAgent();
            agent.startInquiry(DiscoveryAgent.GIAC, this);      // start scan
        } catch (BluetoothStateException ex) {
            //ex.printStackTrace();
        }
    }
    private UUID SERIAL = new UUID(0x1101);
    private UUID RFCOMM = new UUID(0x0003);
    public void deviceDiscovered(RemoteDevice rd, DeviceClass dc) {
        try {
            devicesFound.addElement(rd);
            cg.append(rd.getFriendlyName(true), null);
        } catch (IOException ex) {
            //ex.printStackTrace();
        }
    }
    public void servicesDiscovered(int transID, ServiceRecord[] srs) {
        url = srs[0].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
    }
    public void serviceSearchCompleted(int transID, int respCode) {
        switch (respCode)
        {
            case DiscoveryListener.SERVICE_SEARCH_COMPLETED:
                frm.append("service complete");
                break;
            case DiscoveryListener.SERVICE_SEARCH_NO_RECORDS:
                cg.append("no record", null);
                break;
            case DiscoveryListener.SERVICE_SEARCH_ERROR:
                break;
        }
    }
    public void inquiryCompleted(int param) {
        switch (param)
        {
            case DiscoveryListener.INQUIRY_COMPLETED:
                scanOverFlag = true;        // device discovery over
                frm.append("device complete");
                break;
            case DiscoveryListener.INQUIRY_ERROR:
                break;
            case DiscoveryListener.INQUIRY_TERMINATED:
                break;
        }
    }
    private boolean runFlag;
    private boolean readThreadFlag;
    public void run() {
        while (!runFlag);
        while (!readThreadFlag) {
            byte buf = 0;
            try {
                buf = dis.readByte();
                //String.valueOf(buf)
                frm.append(new String(new byte[] {buf}));        // ascii
            } catch (IOException ex) {
                //ex.printStackTrace();
                frm.append("get error");
            }   
        }
    }
}

ffyy99 發表在 痞客邦 留言(2) 人氣()

當我們下載 jar 要使用時
直接把.jar 檔丟到 C:\Program Files\Java\jdk1.6.0_17\jre\lib\ext
就行了
不然就要一步一步的設定 classpath

ffyy99 發表在 痞客邦 留言(0) 人氣()

1
Blog Stats
⚠️

成人內容提醒

本部落格內容僅限年滿十八歲者瀏覽。
若您未滿十八歲,請立即離開。

已滿十八歲者,亦請勿將內容提供給未成年人士。