i am trying to establish call but it is not working. the project is similar to Streaming voice between Android Phones over WiFi i will post the code and i will be greatful for any help
the sender
public class MainActivity extends ActionBarActivity {
private String outputFile = null;
private Button startBtn;
private Button stopBtn;
private Button playBtn;
private Button stopPlayBtn;
private TextView text;
AudioRecord recorder;
public byte[] buffer;
private boolean status = true;
//Audio Configuration.
private int sampleRate = 8000; //How much will be ideal?
private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
public int minBufSize;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text1);
// store it to sd card
outputFile = Environment.getExternalStorageDirectory().
getAbsolutePath() + "/javacodegeeksRecording.mp3";
startBtn = (Button)findViewById(R.id.start);
startBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
start(v);
}
});
stopBtn = (Button)findViewById(R.id.stop);
stopBtn.setEnabled(true);
stopBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stop(v);
}
});
playBtn = (Button)findViewById(R.id.play);
playBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
play(v);
}
});
stopPlayBtn = (Button)findViewById(R.id.stopPlay);
stopPlayBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopPlay(v);
}
});
}
public void start(View view){
try {stopBtn.setEnabled(true);
minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
byte[] buffer = new byte[4096];
Log.d("VS","Buffer created of size " + minBufSize);
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize);
Log.d("VS","Recorder starts");
OutputStream os = new FileOutputStream(outputFile);
Log.d("VS","Outputstream starts");
BufferedOutputStream bos = new BufferedOutputStream(os);
Log.d("VS","Buffer starts");
DataOutputStream dos = new DataOutputStream(bos);
Log.d("VS","DataOutputstream starts");
recorder.startRecording();
//reading data from MIC into buffer
minBufSize = recorder.read(buffer, 0, buffer.length);
dos.write(buffer);
} catch (IllegalStateException e) {
// start:it is called before prepare()
// prepare: it is called after start() or before setOutputFormat()
e.printStackTrace();
} catch (IOException e) {
// prepare() fails
e.printStackTrace();
}
text.setText("Recording Point: Recording");
startBtn.setEnabled(false);
stopBtn.setEnabled(true);
Toast.makeText(getApplicationContext(), "Start recording...",
Toast.LENGTH_SHORT).show();
}
public void stop(View view){
try {
recorder.stop();
status=false;
stopBtn.setEnabled(false);
playBtn.setEnabled(true);
text.setText("Recording Point: Stop recording");
Toast.makeText(getApplicationContext(), "Stop recording...",
Toast.LENGTH_SHORT).show();
} catch (IllegalStateException e) {
// it is called before start()
e.printStackTrace();
} catch (RuntimeException e) {
// no valid audio/video data has been received
e.printStackTrace();
}
}
public void play(View view) {
try{
minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
Log.d("VS","MinBufSize starts");
FileInputStream in=new FileInputStream( outputFile );
Log.d("VS","fileinputstream starts");
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, 4096, AudioTrack.MODE_STREAM);
byte[] byteData = null;
int bytesread = 0, ret = 0;
int size = (int) outputFile.length();
audioTrack.play();
Log.d("VS","Audio track playing starts");
int count = 512 * 1024;
while (bytesread < size) {
// Write the byte array to the track
Log.d("VS","Reading starts");
ret = in.read( byteData,0, count);
//ret =size in bytes
Log.d("VS","ret="+ ret);
Log.d("VS","continue starts");
if (ret != -1) {
audioTrack.write(byteData,0, ret);
Log.d("VS","write to audio track starts");
bytesread += ret; } //ret
else break; } //while
in.close(); audioTrack.stop(); audioTrack.release();
playBtn.setEnabled(false);
stopPlayBtn.setEnabled(true);
text.setText("Recording Point: Playing");
Toast.makeText(getApplicationContext(), "Start play the recording...",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void stopPlay(View view) {
try {
playBtn.setEnabled(true);
stopPlayBtn.setEnabled(false);
text.setText("Recording Point: Stop playing");
Toast.makeText(getApplicationContext(), "Stop playing the recording...",
Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
// TODO Auto-generated catch block
Log.d("VS",e.getMessage());
e.printStackTrace();
}
}
}
the receiver :
public class MainActivity extends ActionBarActivity {
private EditText target;
private TextView streamingLabel;
private Button startButton,stopButton;
public byte[] buffer;
public static DatagramSocket socket;
private int port=50005; //which port??
AudioRecord recorder;
//Audio Configuration.
private int sampleRate = 8000; //How much will be ideal?
private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
int minBufsize=4096;
private boolean status = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
target = (EditText) findViewById (R.id.target_IP);
streamingLabel = (TextView) findViewById(R.id.streaming_label);
startButton = (Button) findViewById (R.id.start);
stopButton = (Button) findViewById (R.id.stop);
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
status = false;
recorder.release();
Log.d("VS","Recorder released");
}
});
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
status = true;
startStreaming();
}
});
}
public void startStreaming() {
Thread streamThread = new Thread(new Runnable() {
@Override
public void run() {
try {
final InetAddress destination = InetAddress.getByName(target.getText().toString());
Log.d("VS", "Address retrieved");
int minBufSize = 4096;//AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
DatagramSocket socket = new DatagramSocket(port,destination);
Log.d("VS", "Socket Created");
byte[] buffer = new byte[minBufSize];
Log.d("VS","Buffer created of size " + minBufSize);
DatagramPacket packet;
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize);
Log.d("VS", "Recorder initialized");
recorder.startRecording();
Log.d("VS", "Start recording");
while(status == true) {
Log.d("VS", "entering loop");
//reading data from MIC into buffer
minBufSize = recorder.read(buffer, 0, buffer.length);
Log.d("VS", "putting data in buffer");
//putting buffer in the packet
packet = new DatagramPacket (buffer,buffer.length,destination,port);
Log.d("VS", "putting data in packet");
socket.send(packet);
Log.d("VS", "sending data");
}
} catch(UnknownHostException e) {
Log.e("VS", "UnknownHostException");
} catch (IOException e) {
Log.e("VS", "IOException");
}
}
});
streamThread.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Aucun commentaire:
Enregistrer un commentaire