views:

1572

answers:

2

Hi,

Im trying to play recorded wave file.While playing,exception is thrown at foll. stmt.:

Player player = Manager.createPlayer(is, "audio/mpeg");

My entire code for playing wave file is as follows:

if (types[cnt].equals("audio/x-wav")) {
    Class clazz = Class.forName("RecordAudio");
    InputStream is = clazz.getResourceAsStream("file:///SDCard/BlackBerry/original.wav");
    //create an instance of the player from the InputStream
    Player player = Manager.createPlayer(is, "audio/mpeg");
    player.realize();
    player.prefetch();
    //start the player
    player.start();
}

Please help me out. Thanks in advance.

+3  A: 

Hi!

I believe it is because of wrong MIME type. Try this:

String fileName = "file:///SDCard/BlackBerry/original.wav";
String mimeType = "audio/x-wav";
String types[] = javax.microedition.media.Manager
     .getSupportedContentTypes(null);
for (int cnt = types.length - 1; cnt >= 0; --cnt) {
    if (types[cnt].equals(mimeType)) {
     InputStream is = null;
     FileConnection fconn = null;
     try {
      fconn = (FileConnection) Connector.open(
      fileName, Connector.READ);
     } catch (IOException e) {
      System.out.println("Error reading file");
     }
     try {
      is = fconn.openInputStream();
     } catch (IOException e) {
      System.out.println("Error opening stream");
     }
     Player player = null;
     try {
      player =      
      javax.microedition.media.Manager.createPlayer(
      is, mimeType);
     } catch (IOException e) {
      System.out.println("Error creating player");
     } catch (MediaException e) {
      System.out.println("Error media type");
     }
     try {
      player.realize();
     } catch (MediaException e) {
      System.out.println("Player cannot be released");
     }
     try {
      player.prefetch();
     } catch (MediaException e) {
      System.out.println("Player cannot be prefetched");
     }
     // start the player
     try {
      player.start();
     } catch (MediaException e) {
      System.out.println("Player cannot be started");
     }
    }
}

Also see in console what kind of exception was thrown.

Max Gontar
execution stopped at:InputStream is = clazz .getResourceAsStream( "file:///SDCard/BlackBerry/original.wav");
imMobile
On debugger console,FRIDG: could not find file:/SDCard/BlackBerry/original.wav is displayed.But the file exists at specified location.I also tried playing other files, none of them are being played...Please help.
imMobile
updated according to Fostah answer. and don't forget to simulate SD Card ;)
Max Gontar
+4  A: 

The function getResourceAsStream is for pulling resources from a JAR/COD file, not from the filesystem. Plus, this is simpler than you are making it. Just pass the file name and path to createPlayer, like so:

try {
    String filename = "file:///SDCard/BlackBerry/original.wav";
    Player player = javax.microedition.media.Manager.Manager.createPlayer( filename );
} catch (IOException e) {
    System.out.println("Error creating player");
} catch (MediaException e) {
    System.out.println("Error media type");
}
Fostah
+1 Fostah, that is correct.
Max Gontar
Hi,I tried with above code, No exception is thrown...but media player is not activated.Debugger console prints something as: MN: getPlayableStreams0(0)=1MN: getLength0(0)=0MN: getPlayableStreams0(0)=1MN: isSeekable0(0)=1AR: add source 10AR: setAudioMode 32MN: play0(0) is audio, NO check for active pauseMN: play0(0)=0MN: MEDIA_STOPPED receivedMN: handle=0 staticsHandle=7fffffffAUDIOMANAGER: IOExceptionMN: unload0(0)=2 pauseHandle=7fffffffAR: remove source 10AR: setAudioMode 32
imMobile