×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Player error

  1. #1
    Regular Contributor
    Join Date
    Nov 2007
    Posts
    81

    Player error

    Hello, in addition to the Player not playing the music file see here, i am encountering another problem.


    i need to read the whole music file as a byte Array, do some decryption, and then pass it into the player. because File IO takes a really long time to complete, im using the following codes:

    Code:
    public void testAudio(){
    		try{
    			String path = "file:///E:/test.mp3";
    			FileConnection fileConnection = (FileConnection) Connector.open(path);
    			byte [] fileBytes = new byte[(int) fileConnection.fileSize()];
    			InputStream inputStream = new ByteArrayInputStream(fileBytes);
    			Player player = Manager.createPlayer(inputStream, "audio/mp3");
    			player.realize();
    				
    			VolumeControl vc = (VolumeControl) player.getControl("VolumeControl");
    			vc.setLevel(100);
    				
    			player.prefetch();
    			player.start();
    		}catch(Exception e){}
    }

    when playing on the phone, it gives an Exception javax.microedition.media.MediaException: Prefetch error: -5

    wat should i do?

  2. #2
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,073

    Re: Player error

    Is you midlet signed by some trusted certificate? is your code working on emulator ?

    thanks,
    ~Amitabh

  3. #3
    Regular Contributor
    Join Date
    Nov 2007
    Posts
    81

    Re: Player error

    Quote Originally Posted by im2amit View Post
    Is you midlet signed by some trusted certificate? is your code working on emulator ?

    thanks,
    ~Amitabh
    yeah, the code's running on the emulator (for the code in the other thread). this one... it's not tested coz of some stuff.

    anyway, by trusted cert... did u mean using Symbian Signed? is there a better way to get the cert and the app signed? coz the online signing using Symbian Signed takes quite a while.

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

    Re: Player error

    Quote Originally Posted by thechameleon View Post
    Code:
        FileConnection fileConnection = (FileConnection) Connector.open(path);
        byte [] fileBytes = new byte[(int) fileConnection.fileSize()];
    
        // where do you read the data from the file into the array????
    
        InputStream inputStream = new ByteArrayInputStream(fileBytes);
        Player player = Manager.createPlayer(inputStream, "audio/mp3");
    In this code, you are trying to fetch audio data from an array of zeros. Try adding something like:

    Code:
    InputStream fileIn = fileConnection.openInputStream();
    int bytesRead = 0;
    while (bytesRead < fileBytes.length) {
        bytesRead += fileIn.read(fileBytes, bytesRead, fileBytes.length - bytesRead);
    }
    Graham.

  5. #5
    Nokia Developer Champion
    Join Date
    Apr 2007
    Posts
    2,708

    Re: Player error

    wel for a J2ME app you can't use Symbian Signed... thats for like symbian (c++) apps...
    So some known certification companies are Verisign and Thawte...
    And well that costs money and it does take time to get a certificate, but thats only once (about 3 to 5 working days), so you wont have to wait all that time every time just to sign an app... You buy the certificate with which you sign your apps from them, but you will be doing the actual signing yourself...

  6. #6
    Regular Contributor
    Join Date
    Nov 2007
    Posts
    81

    Re: Player error

    Quote Originally Posted by grahamhughes View Post
    In this code, you are trying to fetch audio data from an array of zeros. Try adding something like:

    Code:
    InputStream fileIn = fileConnection.openInputStream();
    int bytesRead = 0;
    while (bytesRead < fileBytes.length) {
        bytesRead += fileIn.read(fileBytes, bytesRead, fileBytes.length - bytesRead);
    }
    Graham.
    uh.. sorry for that dumb error. i removed a few lines when copying and pasting the codes over some classes...

    please assume that the byte Array is filled up with the contents of the file.

    the song still does not play... i've tried

    InputStream inputStream = fileConnection.openInputStream();
    InputStream inputStream = getClass().getResourceAsStream("file:///test.mp3");
    InputStream inputStream = new ByteArrayInputStream(fileBytes);

  7. #7
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,073

    Re: Player error

    If your mp3 file is inside the jar file of your app; you can use:
    InputStream inputStream = getClass().getResourceAsStream("/test.mp3");
    no need to give the file name like: "file:///test.mp3"

    i.e. dont use file:// directly give the path.. of test.mp3 inside the JAR.

    And remember the size of MP3 file should not be very large as it may overflow your heap memory.

    thanks,
    ~Amitabhn

  8. #8
    Regular Contributor
    Join Date
    Nov 2007
    Posts
    81

    Re: Player error

    Quote Originally Posted by im2amit View Post
    If your mp3 file is inside the jar file of your app; you can use:


    no need to give the file name like: "file:///test.mp3"

    i.e. dont use file:// directly give the path.. of test.mp3 inside the JAR.

    thanks,
    ~Amitabh
    the mp3 file is outside the jar.

  9. #9
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,073

    Re: Player error

    if the file is outside the jar you cannot play it by
    InputStream inputStream = getClass().getResourceAsStream("file:///test.mp3");
    You need to access the file using JSR-75 FCA, read the bytes and try to play the music bytes using MMAPI.... see the MMAPI demos in SUN WKT.

    thanks,
    ~Amitabh

  10. #10
    Regular Contributor
    Join Date
    Nov 2007
    Posts
    81

    Re: Player error

    Quote Originally Posted by im2amit View Post
    if the file is outside the jar you cannot play it by

    You need to access the file using JSR-75 FCA, read the bytes and try to play the music bytes using MMAPI.... see the MMAPI demos in SUN WKT.

    thanks,
    ~Amitabh
    Code:
    public void testAudio(){
    		try{
    			String path = "file:///E:/test.mp3";
    			
    			FileConnection fileConnection = (FileConnection) Connector.open(path);
    			byte [] fileBytes = new byte[(int) fileConnection.fileSize()];
    			InputStream readStream = fileConnection.openInputStream();
    			readStream.read(fileBytes);
    			
    			ByteArrayInputStream inputStream = new ByteArrayInputStream(fileBytes);
    			
    			Player player = Manager.createPlayer(inputStream, "audio/mp3");
    			player.realize();
    			
    			VolumeControl vc = (VolumeControl) player.getControl("VolumeControl");
    			vc.setLevel(100);
    			
    			player.prefetch();
    			long duration = player.getDuration();
    			player.start();
    			Thread.sleep(duration/1000);
    
    			player.close();
    			inputStream.close();
    			readStream.close();			
    		}catch(Exception e){
    			String exceptionString = e.toString();
    		}
    		
    	}

    i've tried many different ways, all listed in one of the previous posts; they all either play nothing, or hang my phone. wat am i doing wrong? wat do u recommend i do?

    this latest code i posted hangs the phone; i can only exit the application and/or shut down my phone by taking out the battery.

  11. #11
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,073

    Re: Player error

    Which phone you are tring this on ? is your code working on emulator ?

    thanks,
    ~Amiatbh

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

    Re: Player error

    Quote Originally Posted by thechameleon View Post
    please assume that the byte Array is filled up with the contents of the file.
    I never assume anything.

    Quote Originally Posted by thechameleon View Post
    Code:
    byte [] fileBytes = new byte[(int) fileConnection.fileSize()];
    InputStream readStream = fileConnection.openInputStream();
    readStream.read(fileBytes);
    This code doesn't guarantee to fille the byte array with the contents of the file. This is a common mistake people make about the method InputStream.read(byte[]). It reads "some number of bytes" from the stream. You need to check the return value to see how many bytes were actually read.

    Quote Originally Posted by grahamhughes View Post
    Code:
    InputStream fileIn = fileConnection.openInputStream();
    int bytesRead = 0;
    while (bytesRead < fileBytes.length) {
        bytesRead += fileIn.read(fileBytes, bytesRead, fileBytes.length - bytesRead);
    }
    Also, I suggest you close the file InputStream immediately after you've read the data into the byte array. Don't worry about closing the ByteArrayInputStream, and don't worry (for now) about closing the Player. So, get rid of the code that sleeps. When you've got it playing, used a PlayerListener to detect the end of the sound and close it then.

    Graham.

  13. #13
    Regular Contributor
    Join Date
    Nov 2007
    Posts
    81

    Re: Player error

    Quote Originally Posted by grahamhughes View Post
    I never assume anything.



    This code doesn't guarantee to fille the byte array with the contents of the file. This is a common mistake people make about the method InputStream.read(byte[]). It reads "some number of bytes" from the stream. You need to check the return value to see how many bytes were actually read.



    Also, I suggest you close the file InputStream immediately after you've read the data into the byte array. Don't worry about closing the ByteArrayInputStream, and don't worry (for now) about closing the Player. So, get rid of the code that sleeps. When you've got it playing, used a PlayerListener to detect the end of the sound and close it then.

    Graham.
    ah, thanks for the tip about not all the bytes will be read. i didnt know that.

    in any case, this is the code:
    Code:
    public void testAudio(){
    		try{
    			String path = "file:///E:/test.mp3";
    			
    			FileConnection fileConnection = (FileConnection) Connector.open(path);
    			byte [] fileBytes = new byte[(int) fileConnection.fileSize()];
    			InputStream readStream = fileConnection.openInputStream();
    			String field1String = path;
    			
    			int bytesRead = readStream.read(fileBytes);
    			readStream.close();
    			
    			
    			field1String += "\nRead " + bytesRead +" out of " + fileConnection.fileSize() + " bytes";
    			if(bytesRead == fileConnection.fileSize()){
    				field1String += "\nAll Read";
    			}
    			
    
    			field1 = new TextField("test audio", field1String,1024, TextField.ANY);
    			ByteArrayInputStream inputStream = new ByteArrayInputStream(fileBytes);
    			
    			Player player = Manager.createPlayer(inputStream, "audio/mp3");
    			player.realize();
    			
    			VolumeControl vc = (VolumeControl) player.getControl("VolumeControl");
    			vc.setLevel(100);
    			
    			player.prefetch();
    			player.start();
    			
    		}catch(Exception e){
    			String exceptionString = e.toString();
    			e.printStackTrace();
    			field1 = new TextField("Exception", exceptionString,1024, TextField.ANY);
    		}
    	}

    it shows in the TextField:
    test audio
    file:///E:/test.mp3
    Read 4741833 out of 4741833 bytes
    All Read
    but there's no sound from the player.

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

    Re: Player error

    OK, but I suggest you change the code to read in a loop anyway. Different devices will behave differently here, depending on the underlying implementation of file access. Also, reading from external files, JAR files, network connections and so on, may behave differently.

    No exception? Or do you still get the prefetch error?

    Which device (or which emulator)?

    Graham.

  15. #15
    Regular Contributor
    Join Date
    Nov 2007
    Posts
    81

    Re: Player error

    Quote Originally Posted by grahamhughes View Post
    OK, but I suggest you change the code to read in a loop anyway. Different devices will behave differently here, depending on the underlying implementation of file access. Also, reading from external files, JAR files, network connections and so on, may behave differently.

    No exception? Or do you still get the prefetch error?

    Which device (or which emulator)?

    Graham.
    well.. im running the code on the N95 8GB. no message, no sounds.

Similar Threads

  1. error LNK2019: unresolved external symbol ???
    By wgarnett in forum Symbian
    Replies: 3
    Last Post: 2009-09-28, 06:53
  2. playing aduio file when call is rejected.
    By santosh_sangewar in forum Symbian
    Replies: 6
    Last Post: 2009-06-12, 11:35
  3. My BuilderX (with S60 SDK 2.1.NET MS version) cannot build ARMI UREL
    By danielvidonyi@hotmail.com in forum Symbian Tools & SDKs
    Replies: 1
    Last Post: 2006-05-04, 21:30
  4. error LNK2001: Please Help
    By sourish_khan in forum Symbian
    Replies: 9
    Last Post: 2006-04-19, 11:16

Posting Permissions

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