⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jphone.java

📁 java开发的netbeans插件中的application原型
💻 JAVA
字号:
/*
 * JPhone.java
 *
 * Created on 2006年11月22日, 下午12:08
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package cn.edu.tsinghua.thss.talkie.audio;

import cn.edu.tsinghua.thss.talkie.audio.AMAudioFormat;
import java.io.IOException;
import java.io.InputStream;
import javax.sound.sampled.AudioFormat;

/**
 *
 * @author promenade
 */
public class JPhone implements Runnable {
    
    private AudioCapture PhoneMIC = null;
    private AudioPlayStream PhoneSPK = null;
    private CallLink curCallLink = null;
    
    protected static int bufSize = 1024;//?
    boolean complete = false;
    byte[] gsmdata = new byte[bufSize];
    int numBytesRead = 0;
    
    InputStream playbackInputStream = null;
    String IP;
    VoiceSender sender = null;
    Thread thread1 = null;
    /** Creates a new instance of JPhone */
    public JPhone( String ip ) {
        this.IP = ip;
    }
    
    /**
     *当按下呼叫按钮的时候执行startPhone()方法,
     *startPhone()方法又将子类VoiceSender作为secondThread线程启动
     */
    public void startPhone() {
        initAudioHardware();
        
        //紧接着在建立CallLink子类的实例curCallLink来与具有目标IP地址的计算机进行scoket连接后
        curCallLink = new CallLink( IP );
        
        //启动第二个thread,用于连接别的服务器,并发送本机的声音。
        sender = new VoiceSender( curCallLink, PhoneMIC );
        
        //启动第一个thread,用于监听来的声音。
        //(new Thread(this)).start();//not easy for debug
        thread1 = new Thread( this, " Receiving Thread " );
        thread1.start();
        
    }
    
    public void stopPhone(){
        PhoneMIC.close();
        try {
            PhoneSPK.close(true);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        sender.stop(); // close thread2
        thread1.interrupt(); // close thread1
        try {
            curCallLink.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
    }
    /**
     * 建立AudioCapture对象和AudioPlayStream对象的实例PhoneMIC和PhoneSPK
     */
    public void initAudioHardware() {
        PhoneMIC = new AudioCapture(AMAudioFormat.FORMAT_CODE_GSM);
        AudioFormat format = AMAudioFormat.getLineAudioFormat(AMAudioFormat.FORMAT_CODE_GSM);
        PhoneSPK = new AudioPlayStream(format);
        
        try {
            PhoneMIC.open();
            PhoneSPK.open();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    /**
     *run()方法通过已经建立的CallLink子类的实例curCallLink监听网络上的数据(也就是等待别人的呼叫),
     * 一旦有音频数据到来 curCallLink 实例就为AudioPlayStream 对象PhoneSPK 提供网络传来的音频数据,
     * 而PhoneSPK在一个循环中不断的将音频数据转换为音频信号,完成类似电话听筒的功能。
     */
    public synchronized void run() {
        try {
            //curCallLink.open();
            curCallLink.listen();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        System.out.println("SomeBody Connected!!");
        
        try {
            playbackInputStream = curCallLink.getInputStream();
            System.out.println("input stream ready...");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        try {
            PhoneSPK.start();
            System.out.println("phone speaker ready... ");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        while( !Thread.currentThread().interrupted() ) {
            try {
                numBytesRead = playbackInputStream.read(gsmdata);
                System.out.println( numBytesRead + " Bytes of data readed!");
                if(numBytesRead == -1) {
                    complete=true;
                    break;
                }
                PhoneSPK.write(gsmdata, 0, numBytesRead);
            } catch (IOException e) {
//                System.out.println("IO exception!!");
                //e.printStackTrace();
                //System.exit(1);
            }
        }
        
        try {
            curCallLink.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

⌨️ 快捷键说明

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