我有一个应用程序,它将获取设备上的每个mp3文件,然后使用MP3Sharp库将其解码为PCm字节。然后将它们通过蓝牙发送到另一台设备。然后另一台设备组装AudioTrack类,并开始播放它,然后在蓝牙流可用时开始写入AudioTrack。问题是每隔几秒钟就会有1-2秒的白噪声,然后播放器就会开始播放歌曲的下一部分。我将如何删除它。与文件或播放器有关吗?
这是我将文件发送到依赖项服务的方式:
private void SendData(object sender, EventArgs e)
{
System.Threading.Tasks.Task.Run(() =>
{
// open the mp3 file.
MP3Stream stream = new MP3Stream(pathList[ShowSongFile.SelectedIndex]);
// Create the buffer.
byte[] buffer = new byte[4096];
// read the entire mp3 file.
int bytesReturned = 1;
while (bytesReturned > 0)
{
bytesReturned = stream.Read(buffer, 0, buffer.Length);
DependencyService.Get<BluetoothManager>().Write(buffer);
}
// close the stream after we're done with it.
stream.Close();
}).ConfigureAwait(false);
}
这是我在另一台设备上接收文件的方式:
public void Read()
{
System.Threading.Tasks.Task.Run(() =>
{
int _bufferSize;
AudioTrack _output;
_bufferSize = AudioTrack.GetMinBufferSize(44100, ChannelOut.Stereo, Android.Media.Encoding.Pcm16bit);
_output = new AudioTrack(Android.Media.Stream.Music, 44100, ChannelOut.Stereo, Android.Media.Encoding.Pcm16bit,
_bufferSize, AudioTrackMode.Stream);
_output.Play();
while (mmInStream.CanRead)
{
try
{
byte[] myReadBuffer = new byte[4096];
mmInStream.Read(myReadBuffer, 0, myReadBuffer.Length);
_output.Write(myReadBuffer, 0, myReadBuffer.Length);
}
catch (System.IO.IOException ex)
{
_output.Stop();
System.Diagnostics.Debug.WriteLine("Input stream was disconnected", ex);
}
}
_output.Stop();
}).ConfigureAwait(false);
}