2

I am storaging mp3 files in google cloud stoarge and I want to get the durations of the files. I've tried to do this but it is not working somehow:

 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;
    }
}
davidmoshko
  • 223
  • 1
  • 4
  • 8
  • any stacktrace? what exactly is not working? – Igor Artamonov May 19 '15 at 08:47
  • I can't see the stacktrace but the problem is in line: audioInputStream = AudioSystem.getAudioInputStream(file); – davidmoshko May 19 '15 at 09:23
  • yeah, maybe. Your code doesn't look good anyway. `AppEngineFile` is outdated, deprecated, and not sure if even work this days. Creating `File` from this path is useless most likely (I didn't try though). Why you trying to do it this way? did you read official docs? https://cloud.google.com/appengine/docs/java/googlecloudstorageclient/getstarted – Igor Artamonov May 19 '15 at 09:35
  • oh...So do you know another way to get the file path or to do what I am trying to do? – davidmoshko May 19 '15 at 09:38
  • @davidmoshko I've just noticed you haven't accepted Igor's answer. If you ended up using his code in your app, then you should accept his answer (click on the empty checkmark by it), and upvote if you have the rep for it :) (same would apply to my own answer on your other question, if it is the answer you were looking for :) ) – Patrice May 22 '15 at 16:00

1 Answers1

1

This code could possible work:

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

But as we found in comments, the problem with AudioSystem class, whole package javax.sound is not supported by Appengine.

Also look at docs:

Igor Artamonov
  • 35,450
  • 10
  • 82
  • 113
  • Thanks but this is not working: try (InputStream in = Channels.newInputStream(readChannel)) { audioInputStream = AudioSystem.getAudioInputStream(in); } – davidmoshko May 19 '15 at 10:05
  • I have no idea. I can't see an error in the stack trace but this line is crashes my app so it doesn't do what she need to anymore. – davidmoshko May 19 '15 at 10:13
  • you need to look at stacktrace then, it's impossible to guess w/o such information. you can do it in your Google Cloud Console for example. this line is from official docs, btw – Igor Artamonov May 19 '15 at 10:16
  • This is how my stacktrace looks when I run the app. what canI infer from it? http://imageshack.com/a/img540/646/04vDoE.png – davidmoshko May 19 '15 at 10:21
  • that doesn't look like Appengine stacktrace at all, are you sure that you run it on Appengine Server? – Igor Artamonov May 19 '15 at 10:24
  • Yeah I'm sure. This is my project structure:http://imagizer.imageshack.us/a/img673/9195/JOhAzl.png And the server and app connect each other well (If I delete the part of the duration check the app works well without this part) – davidmoshko May 19 '15 at 10:30
  • stacktrace includes classes from Android. how do you run Android on a server? anyway, if it's on server, just open https://console.developers.google.com/project/%projectid%/logs and see your logs – Igor Artamonov May 19 '15 at 10:33
  • Ok. It says: "java.lang.NoClassDefFoundError: javax.sound.sampled.AudioSystem is a restricted class. Please see the Google App Engine developer's guide for more details.". Do you have any idea what it means? – davidmoshko May 19 '15 at 10:55
  • 1
    it means that you cannot use such class, see list of classes allowed on appengine: https://cloud.google.com/appengine/docs/java/jrewhitelist – Igor Artamonov May 19 '15 at 11:04
  • Do you know about a code that does it? All the options I find in web uses this class.. I can't see any library who takes care of audio file in the list you gave me...have any idea? – davidmoshko May 19 '15 at 11:12
  • @davidmoshko If you need to use that class, you'll need to look at [Managed VMs](https://cloud.google.com/appengine/docs/java/managed-vms/) which do not have the same restrictions as the regular Java runtime. – tx802 May 19 '15 at 16:10