Android的 - MediaPlayer的缓冲区大小的ICS 4.0 [英] Android - MediaPlayer Buffer Size in ICS 4.0

查看:1698
本文介绍了Android的 - MediaPlayer的缓冲区大小的ICS 4.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个插座作为对MediaPlayer的代理这样我就可以下载并将其写入套接字之前解密MP3音频。这是类似的,但是我正在使用本作所有的Andr​​oid 2.1版在全国公共广播电台的新闻应用程序所示的例子 - 4个大气压。

I'm using a socket as a proxy to the MediaPlayer so I can download and decrypt mp3 audio before writing it to the socket. This is similar to the example shown in the NPR news app however I'm using this for all Android version 2.1 - 4 atm.

NPR StreamProxy code - <一个href="http://$c$c.google.com/p/npr-android-app/source/browse/Npr/src/org/npr/android/news/StreamProxy.java">http://$c$c.google.com/p/npr-android-app/source/browse/Npr/src/org/npr/android/news/StreamProxy.java

NPR StreamProxy code - http://code.google.com/p/npr-android-app/source/browse/Npr/src/org/npr/android/news/StreamProxy.java

我的问题是,播放快2.1 - 2.3,但在射击上的prepared听众之前的Andr​​oid 4.0 ICS的MediaPlayer的缓存数据太多。

My issue is that playback is fast for 2.1 - 2.3, but in Android 4.0 ICS the MediaPlayer buffers too much data before firing the onPrepared listener.

数据写入套接字的OutputStream之前的prepared一个例子量():

An example amount of data written to the Socket OutputStream before onPrepared():

在SGS2 2.3.4 - 在prepared()后〜133920字节

On SGS2 with 2.3.4 - onPrepared() after ~ 133920 bytes

在的Nexus S采用4.0.4 - 在prepared()后〜961930字节

On Nexus S with 4.0.4 - onPrepared() after ~ 961930 bytes

这也发生在Galaxy Nexus的。

This also occurs on the Galaxy Nexus.

古怪的4.0模拟器不缓冲的数据是4.0的设备。任何人对ICS体验与MediaPlayer的一个类似的问题?

Weirdly the 4.0 emulator doesn't buffer as much data as 4.0 devices. Anyone experience a similar issue with the MediaPlayer on ICS?

修改

下面是如何将代理服务器写入套接字。在这个例子中它是从从文件加载一个CipherInputStream,但是当它从型Htt presponse加载同一发生

Here's how the proxy is writing to the socket. In this example it's from a CipherInputStream loaded from a file, but the same occurs when it's loaded from the HttpResponse.

final Socket client = (setup above)

// encrypted file input stream
final CipherInputStream inputStream = getInputStream(file);

// setup the socket output stream
final OutputStream output =  client.getOutputStream();

// Writing the header
final String httpHeader = buildHttpHeader(file.length());
final byte[] buffer = httpHeader.getBytes("UTF-8");
output.write(buffer, 0, buffer.length);

int writtenBytes = 0;
int readBytes;
final byte[] buff = new byte[1024 * 12]; // 12 KB

while (mIsRunning && (readBytes = inputStream.read(buff)) != -1) {
    output.write(buff, 0, readBytes);
    writtenBytes += readBytes;
}

output.flush();
output.close();

这是写入MediaPlayer的音频之前的HTTP标头。

The HTTP Headers that are written to the MediaPlayer before the audio..

private String buildHttpHeader(final int contentLength) {
    final StringBuilder sb = new StringBuilder();

    sb.append("HTTP/1.1 200 OK\r\n");
    sb.append("Content-Length: ").append(contentLength).append("\r\n");
    sb.append("Accept-Ranges: bytes\r\n" );
    sb.append("Content-Type: audio/mpeg\r\n");
    sb.append("Connection: close\r\n" );
    sb.append("\r\n");

    return sb.toString();
}

我环顾四周,替代实现,但正如我刚才加密的音频和MediaPlayer的不支持InputStreams作为数据源,我唯一的选择(我觉得..)是使用代理本等。

I've looked around for alternate implementations but as I have encrypted audio and the MediaPlayer does not support InputStreams as a data source my only option (I think..) is to use a proxy such as this.

再次,这是工作的相当不错的Andr​​oid 2.1 - 2.3,但在ICS中的MediaPlayer播放前缓冲了大量的这种数据。

Again, this is working fairly well Android 2.1 - 2.3 but in ICS the MediaPlayer is buffering a huge amount of this data before playing.

编辑2:

进一步的测试显示,这也是对SGS2一个问题,一旦升级到Android 4.0.3。因此,它似乎是MediaPlayer的缓冲实现已在4.0显著改变。这是令人沮丧的API提供了没有办法改变的行为。

Further testing is showing that this is also an issue on the SGS2 once upgraded to Android 4.0.3. So it seems like the MediaPlayer's buffering implementation has changed significantly in 4.0. This is frustrating as the API provides no way to alter the behaviour.

编辑3:

Android的错误产生。请添加注释和明星有作为 <一href="http://$c$c.google.com/p/android/issues/detail?id=29870">http://$c$c.google.com/p/android/issues/detail?id=29870

Android bug created. Please add comments and star there as well http://code.google.com/p/android/issues/detail?id=29870

编辑4:

我的播放code是比较规范的。我对MediaPlayer的开始()调用在我的prepared()方法。

My playback code is fairly standard.. I have the start() call on the MediaPlayer in my onPrepared() method.

mCurrentPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mCurrentPlayer.setDataSource(url);
mCurrentPlayer.prepareAsync();

尝试过只用prepare(),也ajacian81推荐的方式,但都无济于事。

Have tried it using just prepare() and also ajacian81's recommended way but to no avail.

我要补充一点,最近谷歌的员工回来给我,我的问题,并确认该缓冲区的大小是在ICS有意增加(高清内容)。它已被请求的API开发者添加到上的MediaPlayer设置一个缓冲器大小的能力。

I should add that recently a Google employee got back to me about my question and confirmed that the buffer size was intentionally increased in ICS (for HD content). It has been requested to the API developers to add the ability to set a buffer size on MediaPlayer.

虽然我觉得这个API变更请求已经被周围之前,我来了,所以我不会建议任何人都屏住呼吸。

Though I think this API change request had been around before I came along so I wouldn't advise anyone to hold their breath.

推荐答案

有没有可能看到code其中你开始()荷兰国际集团在MediaPlayer?

Would it be possible to see the code where you're start()ing the MediaPlayer?

您使用 STREAM_MUSIC 音频流的类型?

Are you using the STREAM_MUSIC audio stream type?

player.setAudioStreamType(AudioManager.STREAM_MUSIC);

你还播放器prepareAsync的实验(); 。和球员prepare();

Have you also experimented between player.prepareAsync(); and player.prepare();?

也有类似的问题,去年我记得,这里的解决方案是:开始,暂停,然后在prepared开始():

There was a similar issue last year I remember, where the solution was to: start, pause and then onPrepared to start():

player.setAudioStreamType(AudioManager.STREAM_MUSIC); 
player.setDataSource(src); 
player.prepare(); 
player.start(); 
player.pause(); 
player.setOnPreparedListener(new OnPreparedListener() {     
@Override
                public void onPrepared(MediaPlayer mp) {
                    player.start();                
                }
          });

,不可能在这种情况下,修复,但是当你纺车轮,这可能是值得一试。

Unlikely to be the fix in this case, but while you're spinning your wheels this might be worth a shot.

这篇关于Android的 - MediaPlayer的缓冲区大小的ICS 4.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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