播放来自存储器的视频 [英] Playing a video from memory
问题描述
是否有可能直接从内存中播放视频,使用MemoryFile类?我想:
Is it possible to play a video directly from memory, using the MemoryFile class? I tried:
AssetFileDescriptor afd = getResources().openRawResourceFd(videoResId);
InputStream is = getResources().openRawResource(videoResId);
// BufferedReader br = new BufferedReader(new InputStreamReader(is));
try {
memoryFile = new MemoryFile("myvideo", (int) afd.getDeclaredLength());
byte[] buffer = new byte[8192];
int bytesRead, totalBytesRead = 0;
while ((bytesRead = is.read(buffer)) != -1) {
memoryFile.writeBytes(buffer, 0, 0, bytesRead);
totalBytesRead += bytesRead;
}
is.close();
} catch (IOException e) {
e.printStackTrace();
}
FileDescriptor mfd = MemoryFileUtil.getFileDescriptor(memoryFile);
videoPlayer = new MediaPlayer();
try {
// videoPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
videoPlayer.setDataSource(mfd, 0, afd.getLength());
videoPlayer.prepare();
} catch (Throwable e) {
e.printStackTrace();
}
videoPlayer.setLooping(true);
_setPlayerSurface(videoPlayer, holder);
其中 MemoryFileUtil
类是从<一个href=\"https://$c$c.google.com/p/sunnykwong/source/browse/trunk/One+More+Clock/src/com/sunnykwong/omc/MemoryFileUtil.java?spec=svn699&r=699\" rel=\"nofollow\">https://$c$c.google.com/p/sunnykwong/source/browse/trunk/One+More+Clock/src/com/sunnykwong/omc/MemoryFileUtil.java?spec=svn699&r=699
但表面仍然为黑色。然而,如果我称之为的setDataSource()
与 AFD
AssetFileDescriptor(上面的注释行),它完美的作品。
However the surface remains black. If however I call setDataSource()
with the afd
AssetFileDescriptor (the commented line above) it works perfectly.
我做得不对的MemoryFile?有一些指针,我需要它重置?
Am I doing something wrong with the MemoryFile? Is there some pointer I need to reset on it?
推荐答案
我怀疑MemoryFile可以进行工作。挖掘到的MediaPlayer#的setDataSource
code我终于找到了<一后href=\"http://androidxref.com/4.4.4_r1/xref/frameworks/av/media/libmediaplayerservice/MediaPlayerService.cpp#665\"相对=nofollow>本地源
I doubt MemoryFile can be made to work. After digging into the MediaPlayer#setDataSource
code I eventually found the native source
status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
{
ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
struct stat sb;
int ret = fstat(fd, &sb);
if (ret != 0) {
ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
return UNKNOWN_ERROR;
}
ALOGV("st_dev = %llu", sb.st_dev);
ALOGV("st_mode = %u", sb.st_mode);
ALOGV("st_uid = %lu", sb.st_uid);
ALOGV("st_gid = %lu", sb.st_gid);
ALOGV("st_size = %llu", sb.st_size);
if (offset >= sb.st_size) {
ALOGE("offset error");
::close(fd);
return UNKNOWN_ERROR;
}
if (offset + length > sb.st_size) {
length = sb.st_size - offset;
ALOGV("calculated length = %lld", length);
}
// ...
在内部它 FSTAT
的传递给它的文件描述符。我怀疑是这个调用要么彻底失败,或它的返回错误的大小,例如0。
Internally it fstat
's the file descriptor you pass it. My suspicion is that this call either fails outright or it's returning the wrong size, e.g. 0.
这篇关于播放来自存储器的视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!