4

I want to play music from one Android device to another.When I press PLAY button in one device, song starts playing in another device.There is no music file available in receiving device.I have to transfer song from one device to another and start playing song as soon as data starts receive to it.How can I achieve this mechanism?I have tried some code which is given below:

1.After connection established between two devices I start sending .wav file on click of 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 message receive to connected device I save received byte[] into file for now.Instead I want to play music using this received byte[] one after another maintaining continuity.

case MESSAGE_READ:
               //Received bytes from sending device
                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.The music file I am writing using received byte[] is appropriate.But I want to play music as file transfer initiated or when I got first byte[] from sending device.I have used AudioTrack for streaming but it only plays music when whole music file written.It does not play music on received byte[], doing so create a unusual noise from AudioTrack.

I only want to implement Bluetooth SPP for this achievement.

Community
  • 1
  • 1
AndiM
  • 2,196
  • 2
  • 21
  • 38

0 Answers0