Streaming audio through Bluetooth

[亡魂溺海] 提交于 2020-01-16 08:35:11

问题


I have a Bluetooth connection that works ok. When connected, I call this write function, that sends a stream to another device. It goes like this:

public void Write(byte[] bytes)
{
    System.Threading.Tasks.Task.Run(() =>
    {
        int offset = 0;
        int count = 10;
        int len = bytes.Length;

        while (offset < len)
        {
            try
            {
                mmOutStream.Write(bytes, offset, Math.Min(count, len - offset));
                offset += count;
            }
            catch (IOException ex)
            {
                System.Diagnostics.Debug.WriteLine("Error occurred when sending data", ex);
            }
        }
    }).ConfigureAwait(false);
}

This should stream a byte array of 10 bytes. Then On onother device, I call this read method:

public void Read()
{
    System.Threading.Tasks.Task.Run(() =>
    {
        MediaPlayer player = new MediaPlayer();
        try
        {
            byte[] myReadBuffer = new byte[1024];
            int numberOfBytesRead = 0;
            do
            {
                numberOfBytesRead = mmInStream.Read(myReadBuffer, 0, myReadBuffer.Length);
                player.Prepared += (sender, e) =>
                {
                    player.Start();
                };
                player.SetDataSource(new StreamMediaDataSource(new System.IO.MemoryStream(myReadBuffer)));
                player.Prepare();
            }
            while (mmInStream.IsDataAvailable());
        }
        catch (IOException ex)
        {
            System.Diagnostics.Debug.WriteLine("Input stream was disconnected", ex);
        }
    }).ConfigureAwait(false);
}

The StreamMediaDataSource works fine, if I put the entire array in, but this return the Unable to resolve superclass of Lmd5c539bdc79f76d0c80e6cd44011eba829/StreamMediaDataSource; (388)

The method looks like this:

public class StreamMediaDataSource : MediaDataSource
{
    System.IO.Stream data;

    public StreamMediaDataSource(System.IO.Stream Data)
    {
        data = Data;
    } 

    public override long Size
    {
        get
        {
            return data.Length;
        }
    }

    public override int ReadAt(long position, byte[] buffer, int offset, int size)
    {
        data.Seek(position, System.IO.SeekOrigin.Begin);
        return data.Read(buffer, offset, size);
    }

    public override void Close()
    {
        if (data != null)
        {
            data.Dispose();
            data = null;
        }
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);

        if (data != null)
        {
            data.Dispose();
            data = null;
        }
    }
}

So how would I play the audio this way?


But, by using this answer, I get this error:

12-01 20:54:38.887 D/AbsListView(12444): Get MotionRecognitionManager
12-01 20:54:38.947 W/ResourceType(12444): Failure getting entry for 0x010802c9 (t=7 e=713) in package 0 (error -75)
12-01 20:54:45.073 V/BluetoothSocket.cpp(12444): initSocketNative
12-01 20:54:45.073 V/BluetoothSocket.cpp(12444): ...fd 53 created (RFCOMM, lm = 26)
12-01 20:54:45.073 V/BluetoothSocket.cpp(12444): initSocketFromFdNative
12-01 20:54:45.113 D/BluetoothUtils(12444): isSocketAllowedBySecurityPolicy start : device null
12-01 20:54:46.364 V/BluetoothSocket.cpp(12444): connectNative
12-01 20:54:46.424 V/BluetoothSocket.cpp(12444): ...connect(53, RFCOMM) = 0 (errno 115)
12-01 20:54:46.484 I/Choreographer(12444): Skipped 88 frames!  The application may be doing too much work on its main thread.
12-01 20:54:51.669 V/MediaPlayer(12444): constructor
12-01 20:54:51.679 V/MediaPlayer(12444): setListener
12-01 20:54:51.719 W/dalvikvm(12444): Unable to resolve superclass of Lmd5c539bdc79f76d0c80e6cd44011eba829/StreamMediaDataSource; (388)
12-01 20:54:51.719 W/dalvikvm(12444): Link of class 'Lmd5c539bdc79f76d0c80e6cd44011eba829/StreamMediaDataSource;' failed
12-01 20:54:55.693 V/MediaPlayer(12444): constructor
12-01 20:54:55.693 V/MediaPlayer(12444): setListener
12-01 20:54:55.703 W/dalvikvm(12444): Unable to resolve superclass of Lmd5c539bdc79f76d0c80e6cd44011eba829/StreamMediaDataSource; (388)
12-01 20:54:55.703 W/dalvikvm(12444): Link of class 'Lmd5c539bdc79f76d0c80e6cd44011eba829/StreamMediaDataSource;' failed
Thread finished: <Thread Pool> #5
The thread 0x5 has exited with code 0 (0x0).

回答1:


Well my main point is just passing the stream into MediaSource like below :

public void Read()
{
    System.Threading.Tasks.Task.Run(() =>
    {
        MediaPlayer player = new MediaPlayer();
        try
        {           
                player.Prepared += (sender, e) =>
                {
                    player.Start();
                };
                player.SetDataSource(new StreamMediaDataSource(mmInStream));
                player.Prepare();                   
        }
        catch (IOException ex)
        {
            System.Diagnostics.Debug.WriteLine("Input stream was disconnected", ex);
        }
    }).ConfigureAwait(false);
}

And your in your MediaSource implementation remove seeking operation as it is a network stream. And if you want streaming you have to copy it to a MemoryStream;

public class StreamMediaDataSource : MediaDataSource
{ 
...
    public override int ReadAt(long position, byte[] buffer, int offset, int size)
    {
        // data.Seek(position, System.IO.SeekOrigin.Begin); remove seeking
        return data.Read(buffer, offset, size);
    }  
... // rest of your code remains same. 
}


来源:https://stackoverflow.com/questions/59125914/streaming-audio-through-bluetooth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!