iOSDev
iOSDev

Reputation: 3617

Audio files not Playing in Blackberry

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?

Upvotes: 0

Views: 3551

Answers (2)

Maksym Gontar
Maksym Gontar

Reputation: 22775

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.

Upvotes: 4

Fostah
Fostah

Reputation: 11806

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");
}

Upvotes: 7

Related Questions