android 4.4音频策略,Phono android SDK,音频在android 4.4.2中可用,但在其他版本上使用dosent...

/**

* 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);

*/

+0

以何种方式没有按它工作吗?或者在回答问题之前,我们是否应该为自己找到答案? –

+0

问题是什么? –

+0

问题是,它在Android 4.4.2的作品,我测试了其他版本的Android,它不工作,我不明白为什么 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值