0

I'm trying to play a recorded wave file. While playing, an exception is thrown at the following statement:

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

My entire code for playing the 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();
} 

What could be the problem?

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
iOSDev
  • 3,617
  • 10
  • 51
  • 91

2 Answers2

7

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
  • 11,398
  • 10
  • 46
  • 55
  • Hi,I tried with above code, No exception is thrown...but media player is not activated.Debugger console prints something as: MN: getPlayableStreams0(0)=1 MN: getLength0(0)=0 MN: getPlayableStreams0(0)=1 MN: isSeekable0(0)=1 AR: add source 10 AR: setAudioMode 32 MN: play0(0) is audio, NO check for active pause MN: play0(0)=0 MN: MEDIA_STOPPED received MN: handle=0 staticsHandle=7fffffff AUDIOMANAGER: IOException MN: unload0(0)=2 pauseHandle=7fffffff AR: remove source 10 AR: setAudioMode 32 – iOSDev Sep 04 '09 at 04:45
4

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.

Maksym Gontar
  • 22,765
  • 10
  • 78
  • 114
  • execution stopped at: InputStream is = clazz .getResourceAsStream( "file:///SDCard/BlackBerry/original.wav"); – iOSDev Sep 02 '09 at 09:46
  • 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. – iOSDev Sep 02 '09 at 11:04
  • updated according to Fostah answer. and don't forget to simulate SD Card ;) – Maksym Gontar Sep 02 '09 at 16:25