首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AudioTrack口吃

AudioTrack口吃
EN

Stack Overflow用户
提问于 2020-02-03 18:35:05
回答 1查看 389关注 0票数 0

我有一个应用程序通过蓝牙将音乐流到另一部手机。该应用程序将mp3解码为pcm数据,然后发送。问题是声音有时口吃,但其他时候玩得还好。播放方法如下所示:

代码语言:javascript
复制
public void Read()
{
    System.Threading.Tasks.Task.Run(() =>
    {
        int _bufferSize;
        AudioTrack _output;

        _output = new AudioTrack(Android.Media.Stream.Music, 44100, ChannelOut.Stereo, Android.Media.Encoding.Pcm16bit,
            10000, AudioTrackMode.Stream);
        _output.Play();

        byte[] myReadBuffer = new byte[1000];
        //byte[] check = new byte[1];

        System.Threading.Tasks.Task.Run(() =>
        {
            while (true)
            {
                try
                {
                    mmInStream.Read(myReadBuffer, 0, myReadBuffer.Length);
                    _output.Write(myReadBuffer, 0, myReadBuffer.Length);
                    //mmOutStream.Write(check);
                }
                catch (System.IO.IOException ex)
                {
                    System.Diagnostics.Debug.WriteLine("Input stream was disconnected", ex);
                }
            }
        }).ConfigureAwait(false);
    }).ConfigureAwait(false);
}

过了一段时间,它停止并显示如下:

代码语言:javascript
复制
02-03 19:08:58.019 W/AudioTrack( 3986): releaseBuffer() track 0xc5e2de00 disabled due to previous underrun, restarting

我该怎么解决这个问题?

但后来我发现,从我的三星A5到更好的三星J7,它的播放还不错,但它只是停了下来,这是日志:

代码语言:javascript
复制
 02-04 15:44:12.816 W/AudioTrack( 2299): releaseBuffer() track 0xc6fa9380 disabled due to previous underrun, restarting
 Thread finished: <Thread Pool> #7
 The thread 0x7 has exited with code 0 (0x0).
 02-04 15:44:36.239 V/InputMethodManager( 2299): Starting input: tba=android.view.inputmethod.EditorInfo@37c869a nm : com.companyname.sharethemusic ic=null
 02-04 15:44:36.239 D/InputMethodManager( 2299): startInputInner - Id : 0
 02-04 15:44:36.240 I/InputMethodManager( 2299): startInputInner - mService.startInputOrWindowGainedFocus
 02-04 15:44:36.248 D/InputTransport( 2299): Input channel constructed: fd=78
 02-04 15:44:36.248 D/InputTransport( 2299): Input channel destroyed: fd=72
 02-04 15:44:36.659 D/InputMethodManager( 2299): HSIFW - flag : 0 Pid : 2299
 02-04 15:44:36.712 D/BluetoothSocket( 2299): close() this: android.bluetooth.BluetoothSocket@6b2de90, channel: 7, mSocketIS: android.net.LocalSocketImpl$SocketInputStream@c5befa8, mSocketOS: android.net.LocalSocketImpl$SocketOutputStream@a1a6ac1mSocket: android.net.LocalSocket@c0c7266 impl:android.net.LocalSocketImpl@d0bf9a7 fd:java.io.FileDescriptor@2545e54, mSocketState: CONNECTED
 02-04 15:44:36.723 D/BluetoothAdapter( 2299): disable()
 02-04 15:44:38.062 D/BluetoothAdapter( 2299): onBluetoothStateChange: up=false
 02-04 15:44:38.062 D/BluetoothAdapter( 2299): Bluetooth is turned off, stop adv
 02-04 15:44:38.063 D/BluetoothAdapter( 2299): There are no active google scan apps, stop scan
 02-04 15:44:38.090 D/BluetoothAdapter( 2299): ondisableBLE
 02-04 15:44:38.090 D/BluetoothAdapter( 2299): There are no active google scan apps, stop scan
EN

回答 1

Stack Overflow用户

发布于 2020-02-04 01:46:33

您可以像下面这样改进代码。

代码语言:javascript
复制
int buffsize = AudioTrack.GetMinBufferSize(8000, ChannelOut.Mono, Encoding.Pcm16bit);
_output = new AudioTrack(Android.Media.Stream.Music, 8000, ChannelOut.Mono, Android.Media.Encoding.Pcm16bit,
            buffsize*4, AudioTrackMode.Stream);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60045170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档