我的应用程序是两部手机之间的语音聊天。
当我把IP放到192.168.1.100时,应用程序崩溃,但是当我把192.168.1放到它时效果很好。
请给我一个正确的指导来解决我的错误。提前谢谢!
发件人活动:
package com.example.encryptechat;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import android.R.integer;
import android.app.Activity;
import android.content.DialogInterface;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText target,port;
private Button startButton,stopButton;
public byte[] buffer;
public static DatagramSocket socket;
private int port_num=1234;
AudioRecord recorder;
private int sampleRate = 44100;
private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
private boolean status = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(), "App created...!",Toast.LENGTH_LONG).show();
target = (EditText) findViewById (R.id.target_IP);
startButton = (Button) findViewById (R.id.start_button);
stopButton = (Button) findViewById (R.id.stop_button);
port=(EditText) findViewById(R.id.editText1);
target.setText("192.168.1.1");
startButton.setOnClickListener (startListener);
stopButton.setOnClickListener (stopListener);
}
private final OnClickListener stopListener = new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Recoder stop..",Toast.LENGTH_LONG).show();
status = false;
recorder.release();
Log.d("VS","Recorder released");
}
};
private final OnClickListener startListener = new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Clicked....!",Toast.LENGTH_LONG).show();
status = true;
startStreaming();
}
};
public void startStreaming() {
Thread streamThread = new Thread(new Runnable() {
@Override
public void run() {
try {
port_num=Integer.parseInt(port.getText().toString());
int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
DatagramSocket socket = new DatagramSocket();
Log.d("VS", "Socket Created");
byte[] buffer = new byte[minBufSize];
Log.d("VS","Buffer created of size " + minBufSize);
DatagramPacket packet;
final InetAddress destination = InetAddress.getByName(target.getText().toString());
Log.d("VS", "Address retrieved");
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize*10);
Log.d("VS", "Recorder initialized");
recorder.startRecording();
Toast.makeText(getApplicationContext(), "Start Talking....",Toast.LENGTH_LONG).show();;
while(status == true) {
//reading data from MIC into buffer
minBufSize = recorder.read(buffer, 0, buffer.length);
//putting buffer in the packet
packet = new DatagramPacket (buffer,buffer.length,destination,port_num);
socket.send(packet);
}
} catch(UnknownHostException e) {
Log.e("VS", "UnknownHostException");
} catch (IOException e) {
Log.e("VS", "IOException");
}
}
});
streamThread.start();
}
}
日志:
05-14 19:42:16.752: I/Process(956): Sending signal. PID: 956 SIG: 9
05-14 19:42:17.822: I/ActivityManager(294): Process com.example.encryptechat (pid 956) has died.
05-14 19:42:21.482: I/ActivityManager(294): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.encryptechat/.MainActivity} from pid 478
05-14 19:42:21.632: W/WindowManager(294): Failure taking screenshot for (246x410) to layer 21005
05-14 19:42:21.703: I/Choreographer(478): Skipped 62 frames! The application may be doing too much work on its main thread.
05-14 19:42:22.023: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
05-14 19:42:22.212: W/ActivityManager(294): Activity pause timeout for ActivityRecord{41689b70 u0 com.android.launcher/com.android.launcher2.Launcher}
05-14 19:42:22.252: D/dalvikvm(986): Not late-enabling CheckJNI (already on)
05-14 19:42:22.272: I/ActivityManager(294): Start proc com.example.encryptechat for activity com.example.encryptechat/.MainActivity: pid=986 uid=10051 gids={50051, 3003, 1015, 1028}
05-14 19:42:22.774: E/Trace(986): error opening trace file: No such file or directory (2)
05-14 19:42:23.942: D/dalvikvm(294): GREF has increased to 601
05-14 19:42:24.003: E/ActivityThread(755): Failed to find provider info for com.android.inputmethod.latin.dictionarypack
05-14 19:42:24.052: E/BinaryDictionaryGetter(755): Could not find a dictionary pack
05-14 19:42:24.232: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
05-14 19:42:24.322: I/Choreographer(478): Skipped 44 frames! The application may be doing too much work on its main thread.
05-14 19:42:24.392: D/dalvikvm(986): GC_CONCURRENT freed 69K, 7% free 2709K/2904K, paused 16ms+16ms, total 280ms
05-14 19:42:24.452: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
05-14 19:42:24.552: I/Choreographer(986): Skipped 67 frames! The application may be doing too much work on its main thread.
05-14 19:42:24.602: D/gralloc_goldfish(986): Emulator without GPU emulation detected.
05-14 19:42:24.712: I/ActivityManager(294): Displayed com.example.encryptechat/.MainActivity: +2s500ms
05-14 19:42:25.401: I/Choreographer(294): Skipped 30 frames! The application may be doing too much work on its main thread.
05-14 19:42:26.773: I/Choreographer(986): Skipped 32 frames! The application may be doing too much work on its main thread.
05-14 19:42:27.073: W/AudioHardwareInterface(39): getInputBufferSize bad sampling rate: 44100
05-14 19:42:27.073: E/AudioRecord(986): Unsupported configuration: sampleRate 44100, format 1, channelMask 0x10
05-14 19:42:27.091: D/VS(986): Socket Created
05-14 19:42:27.133: D/VS(986): Buffer created of size -2
05-14 19:42:27.141: D/VS(986): Address retrieved
05-14 19:42:27.141: W/dalvikvm(986): threadid=15: thread exiting with uncaught exception (group=0x40a71930)
05-14 19:42:27.161: E/AndroidRuntime(986): FATAL EXCEPTION: Thread-97
05-14 19:42:27.161: E/AndroidRuntime(986): java.lang.IllegalArgumentException: Invalid audio buffer size.
05-14 19:42:27.161: E/AndroidRuntime(986): at android.media.AudioRecord.audioBuffSizeCheck(AudioRecord.java:333)
05-14 19:42:27.161: E/AndroidRuntime(986): at android.media.AudioRecord.<init>(AudioRecord.java:230)
05-14 19:42:27.161: E/AndroidRuntime(986): at com.example.encryptechat.MainActivity$3.run(MainActivity.java:106)
05-14 19:42:27.161: E/AndroidRuntime(986): at java.lang.Thread.run(Thread.java:856)
05-14 19:42:27.181: W/ActivityManager(294): Force finishing activity com.example.encryptechat/.MainActivity
05-14 19:42:27.211: W/WindowManager(294): Failure taking screenshot for (246x410) to layer 21010
05-14 19:42:27.591: I/Choreographer(294): Skipped 40 frames! The application may be doing too much work on its main thread.
05-14 19:42:27.652: I/Choreographer(986): Skipped 89 frames! The application may be doing too much work on its main thread.
05-14 19:42:27.871: I/Choreographer(478): Skipped 88 frames! The application may be doing too much work on its main thread.
05-14 19:42:28.051: I/Choreographer(986): Skipped 32 frames! The application may be doing too much work on its main thread.
05-14 19:42:28.203: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
05-14 19:42:28.282: I/Choreographer(294): Skipped 39 frames! The application may be doing too much work on its main thread.
05-14 19:42:28.354: I/Choreographer(986): Skipped 34 frames! The application may be doing too much work on its main thread.
05-14 19:42:28.372: I/Choreographer(294): Skipped 140 frames! The application may be doing too much work on its main thread.
05-14 19:42:28.391: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
05-14 19:42:28.811: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
05-14 19:42:29.031: I/Choreographer(294): Skipped 35 frames! The application may be doing too much work on its main thread.
05-14 19:42:30.521: I/Process(986): Sending signal. PID: 986 SIG: 9
05-14 19:42:30.541: I/ActivityManager(294): Process com.example.encryptechat (pid 986) has died.
05-14 19:42:30.551: W/InputDispatcher(294): channel '410a59c8 com.example.encryptechat/com.example.encryptechat.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9
05-14 19:42:30.551: E/InputDispatcher(294): channel '410a59c8 com.example.encryptechat/com.example.encryptechat.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
05-14 19:42:30.551: W/InputDispatcher(294): channel '4172e7e0 Toast (server)' ~ Consumer closed input channel or an error occurred. events=0x9
05-14 19:42:30.551: E/InputDispatcher(294): channel '4172e7e0 Toast (server)' ~ Channel is unrecoverably broken and will be disposed!
05-14 19:42:30.662: I/WindowState(294): WIN DEATH: Window{410a59c8 u0 com.example.encryptechat/com.example.encryptechat.MainActivity EXITING}
05-14 19:42:30.662: W/InputDispatcher(294): Attempted to unregister already unregistered input channel '410a59c8 com.example.encryptechat/com.example.encryptechat.MainActivity (server)'
05-14 19:42:30.699: I/Choreographer(294): Skipped 35 frames! The application may be doing too much work on its main thread.
05-14 19:42:30.731: I/WindowState(294): WIN DEATH: Window{4172e7e0 u0 Toast}
05-14 19:42:30.742: W/InputDispatcher(294): Attempted to unregister already unregistered input channel '4172e7e0 Toast (server)'
05-14 19:42:30.983: W/NotificationService(294): Object died trying to hide notification android.app.ITransientNotification$Stub$Proxy@416e1188 in package com.example.encryptechat
05-14 19:42:31.141: I/Choreographer(294): Skipped 41 frames! The application may be doing too much work on its main thread.
05-14 19:42:31.191: W/InputMethodManagerService(294): Got RemoteException sending setActive(false) notification to pid 986 uid 10051
05-14 19:42:31.744: I/Choreographer(294): Skipped 34 frames! The application may be doing too much work on its main thread.
05-14 19:42:49.463: D/ExchangeService(716): Received deviceId from Email app: null
05-14 19:42:49.463: D/ExchangeService(716): !!! deviceId unknown; stopping self and retrying
05-14 19:42:54.521: D/ExchangeService(716): !!! EAS ExchangeService, onCreate
05-14 19:42:54.542: D/ExchangeService(716): !!! EAS ExchangeService, onStartCommand, startingUp = false, running = false
05-14 19:42:54.666: D/ExchangeService(716): !!! EAS ExchangeService, onStartCommand, startingUp = true, running = false
05-14 19:42:54.692: W/ActivityManager(294): Unable to start service Intent { act=com.android.email.ACCOUNT_INTENT } U=0: not found
05-14 19:42:54.692: D/ExchangeService(716): !!! Email application not found; stopping self
05-14 19:42:54.751: W/ActivityManager(294): Unable to start service Intent { act=com.android.email.ACCOUNT_INTENT } U=0: not found
05-14 19:42:54.791: E/ActivityThread(716): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40ceaab0 that was originally bound here
05-14 19:42:54.791: E/ActivityThread(716): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40ceaab0 that was originally bound here
05-14 19:42:54.791: E/ActivityThread(716): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
05-14 19:42:54.791: E/ActivityThread(716): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
05-14 19:42:54.791: E/ActivityThread(716): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
05-14 19:42:54.791: E/ActivityThread(716): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
05-14 19:42:54.791: E/ActivityThread(716): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
05-14 19:42:54.791: E/ActivityThread(716): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
05-14 19:42:54.791: E/ActivityThread(716): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
05-14 19:42:54.791: E/ActivityThread(716): at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
05-14 19:42:54.791: E/ActivityThread(716): at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
05-14 19:42:54.791: E/ActivityThread(716): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
05-14 19:42:54.791: E/ActivityThread(716): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
05-14 19:42:54.791: E/ActivityThread(716): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
05-14 19:42:54.791: E/ActivityThread(716): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-14 19:42:54.791: E/ActivityThread(716): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-14 19:42:54.791: E/ActivityThread(716): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-14 19:42:54.791: E/ActivityThread(716): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-14 19:42:54.791: E/ActivityThread(716): at java.lang.Thread.run(Thread.java:856)
05-14 19:42:54.873: E/StrictMode(716): null
05-14 19:42:54.873: E/StrictMode(716): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40ceaab0 that was originally bound here
05-14 19:42:54.873: E/StrictMode(716): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
05-14 19:42:54.873: E/StrictMode(716): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
05-14 19:42:54.873: E/StrictMode(716): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
05-14 19:42:54.873: E/StrictMode(716): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
05-14 19:42:54.873: E/StrictMode(716): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
05-14 19:42:54.873: E/StrictMode(716): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
05-14 19:42:54.873: E/StrictMode(716): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
05-14 19:42:54.873: E/StrictMode(716): at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
05-14 19:42:54.873: E/StrictMode(716): at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
05-14 19:42:54.873: E/StrictMode(716): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
05-14 19:42:54.873: E/StrictMode(716): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
05-14 19:42:54.873: E/StrictMode(716): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
05-14 19:42:54.873: E/StrictMode(716): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-14 19:42:54.873: E/StrictMode(716): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-14 19:42:54.873: E/StrictMode(716): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-14 19:42:54.873: E/StrictMode(716): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-14 19:42:54.873: E/StrictMode(716): at java.lang.Thread.run(Thread.java:856)
05-14 19:42:54.891: W/ActivityManager(294): Unbind failed: could not find connection for android.os.BinderProxy@40fb8608
05-14 19:42:55.047: E/ActivityThread(716): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d2efb0 that was originally bound here
05-14 19:42:55.047: E/ActivityThread(716): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d2efb0 that was originally bound here
05-14 19:42:55.047: E/ActivityThread(716): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
05-14 19:42:55.047: E/ActivityThread(716): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
05-14 19:42:55.047: E/ActivityThread(716): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
05-14 19:42:55.047: E/ActivityThread(716): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
05-14 19:42:55.047: E/ActivityThread(716): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
05-14 19:42:55.047: E/ActivityThread(716): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
05-14 19:42:55.047: E/ActivityThread(716): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
05-14 19:42:55.047: E/ActivityThread(716): at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
05-14 19:42:55.047: E/ActivityThread(716): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
05-14 19:42:55.047: E/ActivityThread(716): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
05-14 19:42:55.047: E/ActivityThread(716): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
05-14 19:42:55.047: E/ActivityThread(716): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-14 19:42:55.047: E/ActivityThread(716): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-14 19:42:55.047: E/ActivityThread(716): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-14 19:42:55.047: E/ActivityThread(716): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-14 19:42:55.047: E/ActivityThread(716): at java.lang.Thread.run(Thread.java:856)
05-14 19:42:55.081: E/StrictMode(716): null
05-14 19:42:55.081: E/StrictMode(716): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d2efb0 that was originally bound here
05-14 19:42:55.081: E/StrictMode(716): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
05-14 19:42:55.081: E/StrictMode(716): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
05-14 19:42:55.081: E/StrictMode(716): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
05-14 19:42:55.081: E/StrictMode(716): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
05-14 19:42:55.081: E/StrictMode(716): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
05-14 19:42:55.081: E/StrictMode(716): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
05-14 19:42:55.081: E/StrictMode(716): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
05-14 19:42:55.081: E/StrictMode(716): at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
05-14 19:42:55.081: E/StrictMode(716): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
05-14 19:42:55.081: E/StrictMode(716): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
05-14 19:42:55.081: E/StrictMode(716): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
05-14 19:42:55.081: E/StrictMode(716): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-14 19:42:55.081: E/StrictMode(716): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-14 19:42:55.081: E/StrictMode(716): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-14 19:42:55.081: E/StrictMode(716): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-14 19:42:55.081: E/StrictMode(716): at java.lang.Thread.run(Thread.java:856)
05-14 19:42:55.091: W/ActivityManager(294): Unbind failed: could not find connection for android.os.BinderProxy@40ffe678