• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MIDlet Audio Doesn't Work

 
Ben Wilde
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I wrote a simple MIDlet that I want to show a picture and play a sound. Everything compiles without any trouble and things run fine on the emmulator. The problem is when I install the .jar onto my phone (Motorola Razr V3), the audio doesn't work. I made sure that the volume was on, and that the java volume wasn't muted. I'm wondering if anybody else has run into this problem, and if there's some software/hardware solution.

Here's a blip of my audio code:

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

Thanks a lot,
Ben
 
Tom Leylan
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll guess that it isn't the proper format. While it is a .wav file it must be an 8,000 Hz, 8-bit, Mono .Wav file. If you just grabbed a sample, check it's properties, it's probably sampled at a higher rate and stereo.

Tom
 
Danish Shaukat
Ranch Hand
Posts: 340
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From my experience MIDI is the most commonly supported audio format. Make sure WAV is supported by your phone. Otherwise try using MIDI or whatever format your phone supports.
 
Tom Leylan
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose I wasn't clear :-) I'm wasn't just "guessing" I have a RAZR so if it didn't support wav files I would have posted that and I got caught by the issue I reported.

The reason there are various formats is that each has it's advantages and conversely it's disadvantages. MIDI isn't the format of choice it is simply "a format."
 
Ben Wilde
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a bunch for your help. As soon as I downsampled my .wav's to 8khz, I was able to get play them on the RAZR. I'm now having another problem that I'm hoping you may be able to help with...

If I only want to play one wav, things seem to work fine. My application, however, needs to play several different wave files. Right now, I have a method 'playwavfile' that consists of a try loop, in which I make an input stream (using getClass().getResourceAsStream and pass a wav file name), then make a player (using Manager.createPlayer and passing my input stream). I then do a p.start (where p is the player). According to all of the examples I found, this is how to do it.

In my application, I call 'playwavfile' with different wave file names, and I would like to get them to play. When I try to put the file onto my phone, it works perfectly when playing the first wav file, but can't play any wav's after that. An exception gets caught telling me that it can't start the player for the other wavs. So, it looks like the player or input stream needs to be closed, but using in.close and p.close didn't seem to do it. Is there another way to do this?

Thanks,
Ben
 
Ramender Mall
Ranch Hand
Posts: 311
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

well, it shud work after you close the player and then create it again with different file name....there must be some logical issues in the code of your function.....
BTW, what exception is it?..and if possible put the code of playWav() function here....


Ramy...
 
Ben Wilde
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a blip of my code (keep in mind that after the user hits a button, the string 'currentString' is changed, then playwavfile() is called):

public void playwavfile()
{
try
{
InputStream in = getClass().getResourceAsStream("/" + currentString +".wav");
Player p = Manager.createPlayer(in, "audio/x-wav");
p.start();
}
catch (Exception e)
{
System.out.println("Error with wav file");
}
}

I also tried adding a bunch of other methods after the p.start() call (they were: in.close(); p.stop(); p.close(); p.deallocate() , but none of these helped. I also did a printout of the error that is caught when things don't work, and it just says: "start failed."

Thanks,
Ben
 
Ramender Mall
Ranch Hand
Posts: 311
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, if you tried all those close() etc. method after start(), then the media will stop as soon as you start it...

and the problem, according to me, is that you are trying to play one more sound while first one is still on and it has already acquired the exclusive resource like speaker....

my suggestion--> make 'p' the Player reference,a global variable by declaring it outside your method...
and in the beginning of your method perform a check whether 'p' is in CLOSED state or not, and if its not already closed, close it...
e.g


hope it helps,


Ramy.....
 
Ben Wilde
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramy,

Beautiful. It worked like a charm. Thank you VERY much. Your help put an end to many headaches.

-Ben
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic