Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > re-playing sound in J2ME

Reply
Thread Tools

re-playing sound in J2ME

 
 
marko
Guest
Posts: n/a
 
      12-14-2004
Hi,
I'm working on a game in MIDP 2 J2ME and have problems with playing sound.
I used this method to start playing sound:

public void playsnd() {
try {
InputStream is = getClass().getResourceAsStream("/res/test.wav");
Player p = Manager.createPlayer(is, "audio/x-wav");
p.prefetch();
p.start();
}
catch (IOException ex) {}
catch (MediaException ex) {}

}

This method works fine except it has a long delay before sound actually
starts playing.
Is it possible to initialize and create player in class constructor and
then call start() method on player when needed?
I tried this but it gives me null pointer exception when I call
startplaying() method:


import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.Random;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.io.*;

public class ExampleGameCanvas extends GameCanvas implements Runnable {

private Player p;

public ExampleGameCanvas() throws Exception {

InputStream is = getClass().getResourceAsStream("/res/test.wav");
Player p = Manager.createPlayer(is, "audio/x-wav");
p.prefetch();

}

public void startplaying() {

try {
p.start();
}
catch (MediaException ex) {}

}

}


Thank You!
 
Reply With Quote
 
 
 
 
Darryl L. Pierce
Guest
Posts: n/a
 
      12-15-2004
marko wrote:
> Hi,
> I'm working on a game in MIDP 2 J2ME and have problems with playing sound.
> I used this method to start playing sound:
>
> public void playsnd() {
> try {
> InputStream is = getClass().getResourceAsStream("/res/test.wav");
> Player p = Manager.createPlayer(is, "audio/x-wav");
> p.prefetch();
> p.start();


You don't need to prefetch the sound file. When you call start() it
fetches it then and it takes just as long as if you called prefetch().

> }
> catch (IOException ex) {}
> catch (MediaException ex) {}
>
> }
>
> This method works fine except it has a long delay before sound actually
> starts playing.


That's the time the system has to spend actually loading the data from
the input stream. Believe it or not, it can be quite slow reading data
from the JAR.

> Is it possible to initialize and create player in class constructor and
> then call start() method on player when needed?


Yes, you can instantiate the player without calling start().

> I tried this but it gives me null pointer exception when I call
> startplaying() method:


That's because you've declared an instance variable:

> private Player p;


But then in your constructor you created a local variable with the same
name:

> public ExampleGameCanvas() throws Exception {
>
> InputStream is = getClass().getResourceAsStream("/res/test.wav");
> Player p = Manager.createPlayer(is, "audio/x-wav");


Here you should instead be doing:

p = Manager.createPlayer(is,"audio/x-wav");

And that will fix *this* problem.

--
Darryl L. Pierce <>
Visit my webpage: <http://mcpierce.multiply.com>
"By doubting we come to inquiry, through inquiry truth."
- Peter Abelard
 
Reply With Quote
 
 
 
 
marko
Guest
Posts: n/a
 
      12-15-2004
> That's because you've declared an instance variable:
>
>> private Player p;

>
>
> But then in your constructor you created a local variable with the same
> name:
>
>> public ExampleGameCanvas() throws Exception {
>> InputStream is = getClass().getResourceAsStream("/res/test.wav");
>> Player p = Manager.createPlayer(is, "audio/x-wav");

>
>
> Here you should instead be doing:
>
> p = Manager.createPlayer(is,"audio/x-wav");
>
> And that will fix *this* problem.
>


Yes, that solved the problem and now it works without any delay! THANK
YOU very much
 
Reply With Quote
 
Darryl L. Pierce
Guest
Posts: n/a
 
      12-15-2004
marko wrote:

>> Here you should instead be doing:
>>
>> p = Manager.createPlayer(is,"audio/x-wav");
>>
>> And that will fix *this* problem.

>
> Yes, that solved the problem and now it works without any delay! THANK
> YOU very much


No problem, mate. Glad to be of service.

--
Darryl L. Pierce <>
Visit my webpage: <http://mcpierce.multiply.com>
"By doubting we come to inquiry, through inquiry truth."
- Peter Abelard
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
trouble with sound in J2ME Jeff Java 1 04-10-2006 04:33 PM
Will application J2ME MIDP 2.0 based of one device run another J2ME MIDP 2.0 device? nishadixit Java 5 06-01-2005 05:40 AM
J2ME/ktoolbar: Send an sms to an emulated J2ME-App Markus Java 4 02-12-2005 01:20 PM
J2ME Lime error 10061 (Re: Help with J2ME Wireless Toolkit) Boldra Java 0 12-03-2003 11:30 AM
Can i using j2me program to download and run other j2me programs in emulator? robin Java 0 07-20-2003 12:59 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57