我正在开发一个应用程序 Android 到 流式广播并录制流 ,我使用< strong> 媒体播放器 以流式传输和 URLConnection 进行录制。
问题在于 URLConnection 比 媒体播放器 更快,并且记录过多。
如果有人有任何解决方案,我们将不胜感激。
功能流
private void stream(String urlStream)
{
MediaPlayer player = new MediaPlayer();
try
{
player.setDataSource(urlStream);
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener()
{
public void onPrepared(MediaPlayer mp)
{
player.start();
}
});
} catch (IllegalArgumentException e)
{
e.printStackTrace();
} catch (IllegalStateException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
记录功能
public static void record(final String urlString,final File file)
{
new Thread(new Runnable()
{
public void run()
{
try
{
URLConnection conn = new URL(urlString).openConnection();
InputStream is = conn.getInputStream();
OutputStream outstream = new FileOutputStream(file);
byte[] buffer = new byte[4096];
int len;
while ((len = is.read(buffer)) > 0)
{
outstream.write(buffer, 0, len);
}
outstream.close();
} catch (Exception e)
{
Log.e("error record", e.toString());
}
}
}).start();
}