2
使用MemoryFile類可以直接從內存播放視頻嗎?我想:從內存中播放視頻
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
類是https://code.google.com/p/sunnykwong/source/browse/trunk/One+More+Clock/src/com/sunnykwong/omc/MemoryFileUtil.java?spec=svn699&r=699
但是表面保持黑色。但是,如果我使用afd
AssetFileDescriptor(上面的註釋行)致電setDataSource()
,它可以完美地工作。
我在做MemoryFile有問題嗎?有一些指針需要重置嗎?