Phono android SDK,音频适用于android 4.4.2,但不适用于其他版本

时间:2014-02-27 16:58:12

标签: android audio phono

/**
 * Minimalist Phono native client for Android.
 * Illustrates most of the features of Phono Native in the
 * simplest possible way.
 * 
 * @author tim
 *
 */
public class Audio {

/* pretty much any phono native usage will need these */
PhonoNative _pn;
private Context _ctx;
public PhonoMessaging _mess;
public PhonoPhone _phone;
public PhonoCall _call;
protected String _sessionId;

/* app specific */
protected AudioTrack _audioTrack;
private Data _data;

private static Audio audio;
// Note that the constructor is private

public static Audio getSingletonObject(Context ctx, Data data) {
    if (audio == null) {
        audio = new Audio(ctx, data);
    }
    return audio;
}

/*
 * Standard Android stuff... 
 */
Audio(Context ctx, Data data) {
    _ctx = ctx;
    _data = data;

    /* we set some audio UX behaviour here - adjust as needed */
    AudioManager as = (AudioManager) (_ctx
            .getSystemService(Context.AUDIO_SERVICE));
    if (as.isWiredHeadsetOn()) {
        as.setSpeakerphoneOn(false);
    } else {
        as.setSpeakerphoneOn(true);
    }
    startPhono();
}


/*
 * Phono native config and setup.
 */
private void startPhono() {

    // we will need an implementation of the (Abstract) PhonoPhone class
    // Our needs are simple enough to do that inline in an Anon class.
    _phone = new PhonoPhone() {

        @Override
        // invoked when a new call is created 
        public PhonoCall newCall() {
            // implement the abstract PhonoCall class with our behaviours
            // again simple enough to do inline.
            PhonoCall acall = new PhonoCall(_phone) {

                @Override
                public void onAnswer() {
                    android.util.Log.d("newCall", "Answered");
                }

                @Override
                public void onError() {
                    android.util.Log.d("newCall", "Call Error");
                }

                @Override
                public void onHangup() {
                    android.util.Log.d("newCall", "Hung up");
                    _call = null;
                }

                @Override
                public void onRing() {
                    android.util.Log.d("newCall", "Ringing");
                }

            };
            // set other initialization of the call - default volume etc.
            acall.setGain(100);
            acall.setVolume(100);
            return acall;
        }

        @Override
        public void onError() {
            android.util.Log.d("PhonoPhone", "Phone Error");
        }

        @Override
        public void onIncommingCall(PhonoCall arg0) {
            android.util.Log.d("PhonoPhone", "Incomming call");
            _call = arg0;
            _call.answer();
        }

    };
    // and we need an implementation of the (Abstract) PhonoMessaging class 
    _mess = new PhonoMessaging() {

        @Override
        public void onMessage(PhonoMessage arg0) {
            android.util.Log.d("message", arg0.getBody());
        }

    };
    // Likewise PhonoNative - optionally set the address of the phono server
    _pn = new PhonoNative() {

        // 3 boilerplate android methods - we return platform specific implementations of the AudioFace and PlayFace and DeviceInfoFace
        // interfaces.

        @Override
        public AudioFace newAudio() {
            DroidPhonoAudioShim das = new DroidPhonoAudioShim();
            _audioTrack = das.getAudioTrack(); // in theory you might want to manipulate the audio track.
            return das;
        }

        @Override
        public PlayFace newPlayer(String arg0) {
            PlayFace f = null;
            try {
                f = new Play(arg0, _ctx);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return f;
        }
        @Override
                    public DeviceInfoFace newDeviceInfo(){
            return new DeviceInfo();
        }

        // What to do when an error occurs
        @Override
        public void onError() {
            android.util.Log.d("PhonoNative", "Error");
        }

        // we have connected to the Phono server so we now set the UI into motion.
        @Override
        public void onReady() {
            // once we are connected, apply the messaging and phone instances we built.
            _pn.setApiKey("******************************************");
            _pn.setMessaging(_mess);
            _pn.setPhone(_phone);
            // This is where your Id mapping code would go
            _sessionId = this.getSessionID();

            if(audio._sessionId != null) {
                JSONObject obj2 = new JSONObject();

                try {
                    obj2.putOpt("action", "phone");
                    obj2.putOpt("id", _data.getUuid());
                    obj2.putOpt("name", _data.getMyName());
                    obj2.putOpt("roomId", _data.getRoomId());
                    obj2.putOpt("userId", _data.getUuid());
                    obj2.putOpt("ip", audio._sessionId);
                    obj2.putOpt("sip", "sip:"+audio._sessionId);

                    _data.broadcast(obj2);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }//if

            Message msg = Message.obtain();
            msg.what = 0;

            JSONObject obj = new JSONObject();
            try {
                obj.putOpt("action", "dialog");
                obj.putOpt("message", "My call info: "+audio._sessionId+", sip:"+audio._sessionId);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            msg.obj = obj;

            _data.getMyViewUpdateHandler().sendMessage(msg);

            android.util.Log.d("SESSION ID", _sessionId);
        }

        // we got disconnected from the phono server - 
        // retry logic goes here.
        @Override
        public void onUnready() {
            android.util.Log.d("PhonoNative", "Disconnected");
        }
    };

    // set the ringtones (ideally these are local resources - not remote URLS.)
    Uri path = Uri.parse("android.resource://com.mypackage/"+R.raw.ring);
    Uri path2 = Uri.parse("android.resource://com.mypackage/"+R.raw.ringback);

    _phone.setRingTone(path.getPath());
    _phone.setRingbackTone(path2.getPath());

    // and request a connection.
    // phono native will ensure that this (and all other network activity) 
    // occurs on a non UI thread.
    _pn.connect();
}
}



/*Now the audio call 
audio = new Audio(this, data);

audio._phone.dial(_callsip,null);
*/

1 个答案:

答案 0 :(得分:1)

您是否在清单文件上放置了正确的权限? 由于您使用的是唱机,因此您可能需要使用:

`<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />`

编辑: 还要确保您在清单文件上也使用正确的API目标  例如:  <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" />