我有一个应用程序通过蓝牙将音乐流到另一部手机。该应用程序将mp3解码为pcm数据,然后发送。问题是声音有时口吃,但其他时候玩得还好。播放方法如下所示:
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);
}过了一段时间,它停止并显示如下:
02-03 19:08:58.019 W/AudioTrack( 3986): releaseBuffer() track 0xc5e2de00 disabled due to previous underrun, restarting我该怎么解决这个问题?
但后来我发现,从我的三星A5到更好的三星J7,它的播放还不错,但它只是停了下来,这是日志:
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发布于 2020-02-04 01:46:33
您可以像下面这样改进代码。
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);https://stackoverflow.com/questions/60045170
复制相似问题