6 Replies Latest reply on Mar 3, 2009 4:50 PM by paus akid

    playing local sound file

    kamckng Level 1
      I need to play an mp3 file in my AIR app. But I won't necessarily know the path to the file because I need to use things like the storage directory. So, would I have to do something like this to get that done:

      var file:File = File.applicationStorageDirectory.resolvePath("path/to/mp3");
      var urlRequest:URLRequest = new URLRequest(file.nativePath);

      and then load the sound from that? Or is there some way that AIR has provided to skip the first step?

      Thanks!
      Kyle
        • 1. Re: playing local sound file
          Jeff Swartz Level 3
          Re. "I won't necessarily know the path to the file because I need to use things like the storage directory":

          - You do not *need* to use the application storage directory. But it is a good place to store application-specific data. AIR can load files from other locations.
          - If you do want to use the application storage directory, your code looks good.
          • 2. playing local sound file
            paus akid Level 1
            I'm trying to do the same thing as kamckng. On WINXP it's working, but on OSX I get an 'app:' in front of my mp3s file.nativePath. here's my example code

            var file:File = new File();
            file = file.resolvePath(currentRandomSong);
            var req:URLRequest = new URLRequest(file.nativePath);
            songCurrent = new Sound();
            songCurrent.addEventListener(IOErrorEvent.IO_ERROR, onSoundError);
            songCurrent.addEventListener(Event.COMPLETE, onSoundLoaded);
            songCurrent.load(req);

            which throws an IOErrorEvent:
            Error #2032: Stream Error. URL: app:/Users/starpause/mp3s/song.mp3

            /Users/starpause/mp3s/song.mp3 does exist on the filesystem

            any ideas?
            • 3. Re: playing local sound file
              kamckng Level 1
              I don't know much about OSX at all. Could you just check for an instance of app: in the nativePath and just filter it out if it's there?
              • 4. Re: playing local sound file
                paus akid Level 1
                @kamckng unfortunately no, 'app:' isn't on file.nativePath, it's added by the URLRequest and URLRequests can't be manipulated by strings.
                • 5. Re: playing local sound file
                  Joe ... Ward Level 4
                  @ paus akid,

                  There are two things wrong here.
                  One, you should never use a nativePath in place of a URL. This happens to work on Windows because the nativePath is interpreted as an absolute path. However, on Mac and Linux, a nativePath is indistinguishable from a relative URL. Since relative URLs are resolved against the application directory (in general), you get the app:/ URL scheme. So change:
                  var req:URLRequest = new URLRequest(file.nativePath);
                  to:
                  var req:URLRequest = new URLRequest(file.url);

                  The second issue is that you don't initialize your file object before using it to resolve a second path. Instead of:
                  var file:File = new File();
                  file = file.resolvePath(currentRandomSong);

                  You should do something like this (using the appropriate directory):
                  var file:File = File.usersDirectory("path/to/mp3");

                  See
                  Developing cross-platform AIR applications for more information.
                  • 6. playing local sound file
                    paus akid Level 1
                    @Joe thanks very much!

                    .url vs .nativePath was exactly my problem... so i'm now storing file handles using nativePath but using a file handle to extract and pass a url to the loaders in my application.

                    thanks for the article as well, interesting read.