获取文件持续时间Google App云存储服务器

时间:2015-05-19 07:14:21

标签: java google-app-engine servlets mp3 google-cloud-storage

我在google cloud stoarge中存储mp3文件,我想获取文件的持续时间。 我试图这样做,但它不能以某种方式工作:

 ListResult lr = gcsService.list(mybucketname, ListOptions.DEFAULT);
    while (lr.hasNext() && playlistLength > 0) {
        ListItem li = lr.next();
        String filename = "/gs/mybucketname/" + li.getName();
        AppEngineFile readableFile = new AppEngineFile(filename);
        String st = readableFile.getFullPath();
        File file = new File(st);
        AudioInputStream audioInputStream = null;
        try {
           audioInputStream = AudioSystem.getAudioInputStream(file);
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        }
        AudioFormat format = audioInputStream.getFormat();
        long frames = audioInputStream.getFrameLength();
        double durationInSeconds = (frames+0.0) / format.getFrameRate();
        playlistLength-=(int)(durationInSeconds)/60;
    }
}

1 个答案:

答案 0 :(得分:1)

此代码可能有效:

String filename = li.getName();
GcsService gcsService =
GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
GcsInputChannel readChannel = gcsService.openPrefetchingReadChannel(new GcsFilename(mybucketName, fileName), 0, 1024 * 1024);
AudioInputStream audioInputStream;
try (InputStream in = Channels.newInputStream(readChannel)) {
   audioInputStream = AudioSystem.getAudioInputStream(in);
}

但正如我们在评论中发现的那样,Appengine不支持AudioSystem类,整个包javax.sound的问题。

另请参阅文档: