通过蓝牙流音频 [英] Streaming audio through Bluetooth

查看:74
本文介绍了通过蓝牙流音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的蓝牙连接正常.连接后,我称之为写功能,该功能将流发送到另一台设备.它是这样的:

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);
}

这应该流传输10个字节的字节数组.然后在另一台设备上,我将此读取方法称为:

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);
}

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

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

方法如下:

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).

推荐答案

好吧,我的重点只是将流传递到MediaSource中,如下所示:

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);
}

并且您在MediaSource实施中将查找操作删除了,因为它是网络流.如果要进行流式传输,则必须将其复制到MemoryStream;

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. 
}

这篇关于通过蓝牙流音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆