libvlc_media_get_duration始终返回0

时间:2011-04-30 15:12:41

标签: c libvlc

我正在用纯C写一个媒体播放器而我正在使用libvlc。目前我正在开发媒体库,我正在编写目录漫游器和媒体文件解析器。它可以很好地处理各种元数据,如艺术家或专辑等,但libvlc_media_get_duration总是返回0.我尝试了所有内容并在各处搜索,但我无法使其工作。有人能帮助我吗?

以下是代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <vlc/vlc.h>
#include <stdarg.h>
#include <stdbool.h>
#include <dirent.h>
#include <sys/stat.h>

void strcopy(char **dst, const char *src) {
  unsigned int size = strlen(src);
  *dst = (char *) realloc(*dst, sizeof(char) * (size + 1));
  strncpy(*dst, src, size);
  *(*dst+size) = 0;
}

void strconcat(char **dst, int n, ...) {

  va_list args;
  unsigned int count = 0;

  // Count
  va_start(args, n);
  for (unsigned short i = 0; i < n; i++)
    count += strlen(va_arg(args, char*));
  va_end(args);

  // Allocate
  *dst = (char *) realloc(*dst, sizeof(char) * (count+1));
  unsigned int cursor = 0;
  va_start(args, n);
  for(unsigned short i = 0; i < n; i++) {
    char *src = va_arg(args, char*);
    strncpy((*dst+cursor), src, strlen(src));
    cursor += strlen(src);
    *(*dst+cursor) = 0;
  }
  va_end(args);

}

void /* Process tags and add file to database */
__db_add_file(libvlc_instance_t *inst, const char *url, bool compute_hash) {

  // Create new media
  libvlc_media_t *media = libvlc_media_new_path(inst, url);
  libvlc_media_parse(media);

  if (libvlc_media_is_parsed(media)) {

    printf("%s\n", url);
    printf("%llu\n", libvlc_media_get_duration(media));
  }

  libvlc_media_release(media);

}

void /* Walker over directory */
__db_dir_walker(libvlc_instance_t *inst, const char *dir_url, bool compute_hash) {

  // Build base path
  char *base_url = NULL;
  if (dir_url[strlen(dir_url)-1] != '/')
    strconcat(&base_url, 2, dir_url, "/");
  else
    strcopy(&base_url, dir_url);

  // Create necessary variables
  struct dirent *entry;
  DIR *dir;
  struct stat fs;

  // Try to open dir
  if (!(dir = opendir(dir_url))) return;

  while (entry = readdir(dir)) {

    // Strip parent entries
    if ((strcmp(".", entry->d_name) == 0) ||
        (strcmp("..", entry->d_name) == 0)) continue;

    char *dir_full_path = NULL;
    strconcat(&dir_full_path, 2, base_url, entry->d_name);

    if (stat(dir_full_path, &fs) < 0) return;

    if (S_ISDIR(fs.st_mode)) { // Process directory

      __db_dir_walker(inst, dir_full_path, compute_hash);

    } else { // Process media file

      __db_add_file(inst, dir_full_path, compute_hash);
    }

  }

  // Free memory
  closedir(dir);
}

void
db_scan_directory(const char *dir_url, bool compute_hash) {

  // Try to open target dir
  if (!opendir(dir_url)) return;

  // Preload vlc instance for tag data retrieving
  libvlc_instance_t *inst = libvlc_new(0, NULL);

  // Walk over directory
  __db_dir_walker(inst, dir_url, compute_hash);

  // Free resources
  libvlc_release(inst);
}

int main () {

  db_scan_directory("/media/storage/music/Blur/", false);

  return 0;
}

谢谢!

2 个答案:

答案 0 :(得分:7)

如果有人想知道这个问题的回答,请点击这里:

你需要玩才能获得持续时间。

感谢Videolan论坛的Jean-Baptiste Kempf。

答案 1 :(得分:3)

最好的方法可能是调用libvlc_media_parse()或其异步反对部分libvlc_media_parse_async()

调用libvlc_media_parse()后,您的元数据(包括持续时间)将被提交。