Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

c# - 通过蓝牙流音频(Streaming audio through Bluetooth)

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.

(这应该流传输一个10字节的字节数组。)

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;

(如果我将整个数组放入,则StreamMediaDataSource可以正常工作,但这将返回无法解析Lmd5c539bdc79f76d0c80e6cd44011eba829 / StreamMediaDataSource的超类;)

(388)

((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?

(那么,我将如何以这种方式播放音频?)

  ask by MTS translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...