猿问

小弟想做一个类似微信的语音,以下代码请指教?

部分代码:

Handler han = new Handler() {
public void handleMessage(android.os.Message msg) {
Toast.makeText(MessageActivity.this, "jinlail", Toast.LENGTH_LONG).show();
LinearLayout client = new LinearLayout(MessageActivity.this);
TextView t = new TextView(MessageActivity.this);
t.setText(msg.getData().getString("username") + "说:");
Button button = new Button(MessageActivity.this);
button.setText("播放");
final String filepath = msg.getData().getString("filename");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MediaPlayer mediaPlayer = new MediaPlayer();
if (mediaPlayer.isPlaying()) {
mediaPlayer.reset();
}
try {
mediaPlayer.setDataSource("/sdcard/" + filepath);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e) {
// TODO: handle exception
}
}
});
client.setOrientation(LinearLayout.HORIZONTAL);
client.addView(t);
client.addView(button);
line.addView(client);
};
};

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.message);
new Thread() {
public void run() {
UDPServer.message=MessageActivity.this;
try {
UDPServer.openServer();
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
}

Message msg = new Message();
Bundle b3 = new Bundle();b3.putString("username", s.split(",")[0]);
b3.putString("filename", s.split(",")[1]);
msg.setData(b3); message.han.sendMessage(msg);

哆啦的时光机
浏览 124回答 1
1回答

Cats萌萌

光看这些只能判断如果 Handler里没有问题 UDPServer怎么调取的message Message里的han是否为空 你可以再好好看看, 主要看看 UDPServer.openServer();跟Message的初始化方法,看看message.han是否为空
随时随地看视频慕课网APP
我要回答