I want to play music from one Android device to another. When I press the PLAY button on one device, the song starts playing on the other device. There is no music file in the receiver. I need to transfer the song from one device to another and start playing the song as soon as the data begins to receive to it. How can I achieve this mechanism? I tried the code below:
1.After establishing a connection between the two devices, I start sending a WAV file when I press the PLAY button:
private void sendMusicFile() {
try {
InputStream inStream = getResources().openRawResource(R.raw.jingle);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buff = new byte[20480];
int i;
while ((i = inStream.read(buff, 0, buff.length)) > 0) {
baos.write(buff, 0, i);
}
mFileToSend = baos.toByteArray();
mIsFileSent = true;
Log.v("log_tag", "Length of File to Transfer : " + mFileToSend.length);
mChatService.write(mFileToSend);
} catch (IOException e) {
e.printStackTrace();
}
}
2. When the message arrives on the connected device, I save the received byte [] to the file for the moment. Instead, I want to play music using this received byte [] one by one, while maintaining continuity.
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
Log.v("log_tag", "ReadBuff Length : " + readBuf.length);
try {
if (fos == null) {
fos = new FileOutputStream("/sdcard/Recorded.wav");
}
fos.write(readBuf);
for (int i = 0; i < readBuf.length; i++) {
counter = counter + 1;
if (counter >= mFileLength) {
fos.close();
playSound();
}
}
} catch (FileNotFoundException e) {
Log.d("CAMERA", e.getMessage());
} catch (IOException e) {
Log.d("CAMERA", e.getMessage());
}
}
break;
3. , [], . [] . AudioTrack , , . [], AudioTrack.
Bluetooth SPP .