Skip to content Skip to sidebar Skip to footer

Playing A Video From Memory

Is it possible to play a video directly from memory, using the MemoryFile class? I tried: AssetFileDescriptor afd = getResources().openRawResourceFd(videoResId); InputStream is = g

Solution 1:

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);
    }
    // ...

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.

Post a Comment for "Playing A Video From Memory"