3

質問: 受信したデータ (Bluetooth チャット サンプル コード経由で送信) を mp3 ファイルにパイプするにはどうすればよいですか?

目的: シンプルな Bluetooth ファイル転送方法を使用して、ある Android フォンから別の Android フォンに mp3 ファイルを転送します。

方法: Android SDK で提供されている Bluetooth チャット サンプルを使用して、MP3 ファイルをバイト配列にパイプし、そのデータを 2 番目のデバイスに送信しました。

現在のステータス: データは 2 番目のデバイスによって受信されます (ストリームを画面に出力して確認しました) が、ファイルを開いてデータをパイプし、「.mp3」拡張子を付けて再生することができません。 .

BluetoothChat.java

public byte[] lilfilebuffer = null;


private void sendMessage(String message) {


    lilfilebuffer = read(new File("/sdcard/media/07_The-organ.mp3"));
    mChatService.write(lilfilebuffer);


}


private byte[] read(final File file) {
    Throwable pending = null;
    FileInputStream in = null;
    final byte buffer[] = new byte[(int) file.length()];
    try {
        in = new FileInputStream(file);
        in.read(buffer);
    } catch (Exception e) {
        pending = new RuntimeException("Exception occured on reading file "
                        + file.getAbsolutePath(), e);
    } finally {
        if (in != null) {
                try {
                        in.close();
                } catch (Exception e) {
                        if (pending == null) {
                                pending = new RuntimeException(
                                        "Exception occured on closing file" 
                             + file.getAbsolutePath(), e);
                        }
                }
        }
        if (pending != null) {
                throw new RuntimeException(pending);
        }
    }
    return buffer;
}




// The Handler that gets information back from the BluetoothChatService
private final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case MESSAGE_STATE_CHANGE:
            if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
            switch (msg.arg1) {
            case BluetoothChatService.STATE_CONNECTED:
                setStatus(getString(R.string.title_connected_to, mConnectedDeviceName));
                mConversationArrayAdapter.clear();
                break;
            case BluetoothChatService.STATE_CONNECTING:
                setStatus(R.string.title_connecting);
                break;
            case BluetoothChatService.STATE_LISTEN:
            case BluetoothChatService.STATE_NONE:
                setStatus(R.string.title_not_connected);
                break;
            }
            break;
        case MESSAGE_WRITE:
            byte[] writeBuf = (byte[]) msg.obj;

            try {
                FileOutputStream out = new FileOutputStream("/sdcard/media/testsong.mp3");
                out.write(writeBuf);
                out.close();

                } 
                catch (IOException e) 
                { 
                //System.out.println("Exception ");

                }
            break;
        case MESSAGE_READ:
            byte[] readBuf = (byte[]) msg.obj;

            try {
                FileOutputStream out = new FileOutputStream("/sdcard/media/testsong.mp3");

                out.write(readBuf);
                out.close();



                } 
                catch (IOException e) 
                { 
                //System.out.println("Exception ");

                }



            break;
        case MESSAGE_DEVICE_NAME:
            // save the connected device's name
            mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
            Toast.makeText(getApplicationContext(), "Connected to "
                           + mConnectedDeviceName, Toast.LENGTH_SHORT).show();
            break;
        case MESSAGE_TOAST:
            Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
                           Toast.LENGTH_SHORT).show();
            break;
        }
    }
};

BluetoothChatService.java

public void write(byte[] out) {

    ConnectedThread r;

    synchronized (this) {
        if (mState != STATE_CONNECTED) return;
        r = mConnectedThread;
    }

    r.write(out);
}

private class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    public ConnectedThread(BluetoothSocket socket, String socketType) {
        Log.d(TAG, "create ConnectedThread: " + socketType);
        mmSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;


        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) {
            Log.e(TAG, "temp sockets not created", e);
        }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {
        Log.i(TAG, "BEGIN mConnectedThread");
        byte[] buffer = new byte[1024];
        int bytes;


        while (true) {
            try {

                bytes = mmInStream.read(buffer);


                mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
                        .sendToTarget();
            } catch (IOException e) {
                Log.e(TAG, "disconnected", e);
                connectionLost();
                // Start the service over to restart listening mode
                BluetoothChatService.this.start();
                break;
            }
        }
    }

    public void write(byte[] buffer) {
        try {
            mmOutStream.write(buffer);


            mHandler.obtainMessage(BluetoothChat.MESSAGE_WRITE, -1, -1, buffer)
                    .sendToTarget();
        } catch (IOException e) {
            Log.e(TAG, "Exception during write", e);
        }
    }


}}
4

1 に答える 1

0

データや MP3 ファイルについて特別なことは何もありません。再生する前にディスクに書き込む必要があります。

私はこれを正確に行うアプリ ( BlueMuze ) を持っていますが、Bluetooth 接続を確立して維持することと、ペアリングのタイミングを正しくすることがより困難であることがわかりました。

ただし、コードの問題は、1024 バイトごとに新しいファイルを作成して書き込むように見えることです。

FileOutputStream「chat」ハンドラーがサービスから送信されたメッセージを読み取るたびに、新しい を作成します。むしろ、FileOutputStream一度作成して、ファイルの最後に到達するまでバッファを書き込んでから、FileOutputStream.

それが役立つことを願っています。

于 2012-09-11T10:09:02.877 に答える