×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 29
  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    14

    Question Error while trying to play mp3 file

    Hello.

    I am using NetBeans 6.7 Beta + S40_5th_Edition_SDK_Feature_Pack_1 and i am trying to play audio files.
    I am getting IllegalAccessException.
    Tried with mp3 and wav files.

    The code:
    Code:
    import java.io.*;
    import java.util.*;
    import javax.microedition.media.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.lcdui.Display;
    import javax.microedition.midlet.*;
    import javax.microedition.media.control.MIDIControl;
    import java.io.IOException;
    import javax.microedition.io.Connector;
    import javax.microedition.io.file.FileConnection;
    import javax.microedition.media.control.VolumeControl;
    import javax.microedition.media.Manager;
    import javax.microedition.media.control.ToneControl;
    
    
    /**
    The corresponding valid file URI in Windows is:
         file:///C:/Documents%20and%20Settings/davris/FileSchemeURIs.doc
    */
    
    public class MusicPlayer extends MIDlet implements CommandListener, PlayerListener {
       private MIDIControl mControl = null;
       private Display display;
    
       MusicPlayer() {
          //display = Display.getDisplay(this);
          //display.setCurrent(new Form("Music Player"));
          //playToneSequence();
       }
    
       public void startApp() {
          //initialize();
          //playToneSequence();
          startMIDlet();
       }
    
       private void initialize() {
           try {
          //Player p = Manager.createPlayer("file:///D:/My Documents/NetBeansProjects/MusicPlayer/src/ramm.mp3");
          //Player p = Manager.createRealizedPlayer(new File("test.mp3").toURI().toURL());
          //locator: file:///c:/predefgallery/predefmusic/test.mp3
          //Player p = Manager.createPlayer(getClass().getResourceAsStream(ramm.mp3), "audio/mpeg");
          //Player p = Manager.createPlayer(getClass().getResourceAsStream("ramm.mp3"), "audio/mpeg");
          //printSupportedContentTypes("null");
          //printSupportedProtocols("aufio/mpeg");
          InputStream is = getClass().getResourceAsStream("/ramm.mp3");
          //InputStream is = getClass().getResourceAsStream("file:///D:/My Documents/NetBeansProjects/MusicPlayer/src/ramm.mp3");
          Player p = Manager.createPlayer(is, "audio/mpeg");
          p.addPlayerListener(this);
          p.realize();
          VolumeControl vc = (VolumeControl) p.getControl("VolumeControl");
          if(vc != null) {
             vc.setLevel(100);
          }
          p.prefetch();
          //mControl = (MIDIControl)p.getControl("javax.microedition.media.control.MIDIControl");
          p.start();
          //System.out.println("Player state: " + p.getState());
          } catch (MediaException pe) {
             pe.printStackTrace();
          } catch (IOException ioe) {
             ioe.printStackTrace();
          }
       }
    
       public void startMIDlet() {
          initialize();
       }
    
       private void playToneSequence() {
          try {
             Player player = Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR);
             player.realize();
             ToneControl tc = (ToneControl)(player.getControl("ToneControl"));
             tc.setSequence(new byte[] { ToneControl.VERSION, 1, ToneControl.C4, 8, ToneControl.C4 + 2, 8 }); // D4
             player.start();
          } catch (MediaException pe) {
             pe.printStackTrace();
          } catch (IOException ioe) {
             ioe.printStackTrace();
          } catch (Exception e) {
             e.printStackTrace();
          }
       }
    
       public void pauseApp() {
       }
    
       public void destroyApp(boolean unconditional) {
       }
    
       public void commandAction(Command c, Displayable s) {
       }
    
       public static void printSupportedContentTypes(String protocol) {
          System.out.println("printSupportedContentTypes METHOD.");
          String[] protocols = javax.microedition.media.Manager.getSupportedContentTypes(protocol);
          System.out.println("**********SupportedContentTypes**********");
          for(int i=0; i<protocols.length; i++)
             System.out.println("Supported: " + protocols[i]);
          System.out.println("*****************************************");
       }
    
       public static void printSupportedProtocols(String protocol) {
          System.out.println("printSupportedProtocols METHOD.");
          String[] protocols = Manager.getSupportedProtocols(protocol);
          System.out.println("**********SupportedProtocols*************");
          for(int i=0; i<protocols.length; i++)
             System.out.println("Supported: " + protocols[i]);
          System.out.println("*****************************************");
       }
    public void playerUpdate(javax.microedition.media.Player player, String str, Object obj) {
        if(str == PlayerListener.END_OF_MEDIA){
            //player.close();
        }
    }
    
    }
    I have look the ForumNokia, Sun ME Forum, browsed web, read documentation, examined examples and still cant play.

    So i am obviously doing something wrong.

    I need help on this one, do you know where mistake is?

    Martin Berger

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Error while trying to play mp3 file

    The constructor of your MIDlet class is not public, so it is not accessible to the Application Manager.

    Change the constructor to public (or remove it, since you don't have any code in it anyway).

    Graham.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    14

    Question Re: Error while trying to play mp3 file

    Quote Originally Posted by grahamhughes View Post
    The constructor of your MIDlet class is not public, so it is not accessible to the Application Manager.

    Change the constructor to public (or remove it, since you don't have any code in it anyway).

    Graham.
    Wow, you answer fast!

    Yes, now i do not have IllegalAccessException but i do have OutOfMemmoryError when using mp3 file.
    With wav file it says: "Player Running Nothing to Display" but no sound.

    Also i tried using URI locator as an argument:
    "file:///D:/My Documents/NetBeansProjects/MusicPlayer/src/ramm.mp3"
    but error is
    IllegalArgumentException Param is null.
    I checked the path, it is correct, file is there.

    Can you help me with this?

  4. #4
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: Error while trying to play mp3 file

    also, even though it stil commentariated, this one :
    //printSupportedProtocols("aufio/mpeg");

    wont work, you have misspelled audio

  5. #5
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Error while trying to play mp3 file

    You get "nothing to display" because you never call Display.setCurrent(). Some VMs will shut an application down if it doesn't display something within a reasonable time.

    Avoid doing anything in the MIDlet constructor. Keep startApp() brief. Two issues with startApp(). First: it's an "event", so it needs to return quickly. Events are serialized, so no other event will be delivered until the current event has completed. Second: it may be called more that once. startApp() is a slight misnomer, it's also "resumeApp()", since it will get called after a pauseApp() event.

    To do anything potentially time consuming, you are better to create a Thread, and do it outside of the event mechanism.

    OutOfMemory... how big is the MP3 file? Depending on the implementation, getResourceAsStream() might cause the entire file to be loaded into the heap.

    You can't use a URL as a parameter to getResourceAsStream(). The parameter must be a path to a file in the JAR.

    For playing WAVs... if a file doesn't play, try changing it to 8kHz, 8bit, mono. A device that plays WAVs doesn't necessarily play all formats of WAV. Likewise for MIDIs and other formats, by the way.

    Graham.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    14

    Cool Re: Error while trying to play mp3 file

    Quote Originally Posted by Tiger79 View Post
    also, even though it stil commentariated, this one :
    //printSupportedProtocols("aufio/mpeg");

    wont work, you have misspelled audio
    Oops, thanks, i've overlooked it

  7. #7
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: Error while trying to play mp3 file

    Take into account that if you're testing on a S40 those have a limited memory Heap Size, mostly they max out at 2 megabytes... so any MP3 bigger than 2 megs minus the memory spcae needed by your app, is bound to fail...

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    14

    Post Re: Error while trying to play mp3 file

    OutOfMemory... how big is the MP3 file? Depending on the implementation, getResourceAsStream() might cause the entire file to be loaded into the heap.
    File is 3.66 Mb.

    You can't use a URL as a parameter to getResourceAsStream(). The parameter must be a path to a file in the JAR.
    Then it means: "/bark.wav" not "file:///D:/My Documents/NetBeansProjects/MusicPlayer/src/bark.wav".
    Ok.

    For playing WAVs... if a file doesn't play, try changing it to 8kHz, 8bit, mono. A device that plays WAVs doesn't necessarily play all formats of WAV. Likewise for MIDIs and other formats, by the way.
    I didnt know there are multiple WAV formats Going to try your advice.

    Hey, thanks Graham!

  9. #9
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Error while trying to play mp3 file

    Quote Originally Posted by Tiger79 View Post
    Take into account that if you're testing on a S40 those have a limited memory Heap Size, mostly they max out at 2 megabytes... so any MP3 bigger than 2 megs minus the memory spcae needed by your app, is bound to fail...
    Quote Originally Posted by Martin Berger View Post
    File is 3.66 Mb.
    I'd go with Tiger's assessment, and suggest that this file is just too big. You might be able to verify this by reducing the bits/sec rate on the file (if you have tools for doing this) to take the size down significantly. If it plays when it's smaller, then watch the memory monitor.

    Quote Originally Posted by Martin Berger View Post
    I didnt know there are multiple WAV formats Going to try your advice.
    You can change the format of a WAV using Windows Sound Recorder, I think there are options accessible from the Save As dialogue.

    Graham.

  10. #10
    Registered User
    Join Date
    Oct 2009
    Posts
    14

    Thumbs up Re: Error while trying to play mp3 file

    Quote Originally Posted by Tiger79 View Post
    Take into account that if you're testing on a S40 those have a limited memory Heap Size, mostly they max out at 2 megabytes... so any MP3 bigger than 2 megs minus the memory spcae needed by your app, is bound to fail...
    Now, this is interesting, no bigger than two Mb.
    Allright, that shouldnt be a problem, i need to make mp3 player which will play files from a list of files from phone card or memmory for Xpress Music devices. I hope...
    I wanted to see how Player works.

    Thanks for advice Tiger.

  11. #11
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Error while trying to play mp3 file

    The other issue with large files on Series 40s is that all Series 40s impose a limit on the size of JAR file that can be installed. For most higher-end S40s, the limit is 1Mb.

    Playing MP3s from the file system is slightly different. You'll need to use Connector.open() with a "file:" URL to access the file. This will trigger security prompts - Java apps can't access the file system without the user's permission every time they open a file. Don't quote me, but I think opening a file from the file system won't cause the whole file to be loaded into the heap.

    Graham.

  12. #12
    Registered User
    Join Date
    Oct 2009
    Posts
    14

    Smile Re: Error while trying to play mp3 file

    Quote Originally Posted by grahamhughes View Post
    The other issue with large files on Series 40s is that all Series 40s impose a limit on the size of JAR file that can be installed. For most higher-end S40s, the limit is 1Mb.

    Playing MP3s from the file system is slightly different. You'll need to use Connector.open() with a "file:" URL to access the file. This will trigger security prompts - Java apps can't access the file system without the user's permission every time they open a file. Don't quote me, but I think opening a file from the file system won't cause the whole file to be loaded into the heap.

    Graham.
    Graham, MAN, you are the real help!
    I though it would be easy to make mp3 player, like, use something like Java SE's JFileChooser, take a File path, create Player instance with File parameter, set listeners and adjust them, and few other things.
    I think i will have to read some Java ME book to get this done. I will check that accessibility stuff you said, about file system.

    Thanks again, i will need your help in future, it seems...

    Martin

  13. #13
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Error while trying to play mp3 file

    Don't thank me yet... wait for the pain...

    First thing you need to remember about MIDP is: a J2SE runtime weighs in at about 50Mb, while a MIDP runtime could be as small as 500k. Moving to MIDP from almost any other environment involves a painful culture-shock.

    As well as the big/small culture shock, you're also going to encounter the sandbox culture shock. The "sandbox" is what prevents a MIDP application from doing any harm to the user - from accessing the user's data, or spending the user's money (sending SMS, connecting to the network, etc.).

    Oh, and did I mention the "fragmentation" culture shock? This is what you hit once you get your app working on the emulator, or on one phone, and then you discover that it doesn't work on anything else...

    Yes... a lot of reading would be a good plan. There's a lot of stuff here in the discussion boards, and some great info in the wiki with loads of code samples. Time spent reading will probably save you ten times as much time head-banging later on. You'll find specific articles on subject like file access and playing media, as well as more general topics like portability, memory management and security domains.

    Good luck!

  14. #14
    Registered User
    Join Date
    Oct 2009
    Posts
    14

    Smile Re: Error while trying to play mp3 file

    Graham:

    Allright, waiting the pain
    I am gonna need that luck it seems...

  15. #15
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: Error while trying to play mp3 file

    Just out of curiosity, you were mentioning XpressMusic devices, have you already checked what platform those are ? I have a 5800XM and thats a S60 for example, but a 5310XM is S40 3rd edition whilst a 5610XM is a S40 5th edition...
    Just to specifically point out the device fragmentation Graham was mentioning

Similar Threads

  1. Error with makesis.exe and creating the .pkg file
    By gramya87 in forum Symbian Tools & SDKs
    Replies: 7
    Last Post: 2007-10-10, 13:53
  2. Help needed with a 3gp file...
    By joedoe_1981 in forum Streaming and Video
    Replies: 0
    Last Post: 2007-07-18, 18:58
  3. Contacts fields order??
    By timatima in forum Symbian
    Replies: 1
    Last Post: 2007-06-08, 13:51

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×