Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 96
Registered: ‎06-11-2009
My Device: Not Specified
Accepted Solution

Audio Streaming and playback

Hi,

In my application I tried recording audio and then tried writting the bytes onto the file system in mp3 format. When i open the file through the browser the recorder opens and it does not play anything. Am i doing something wrong?

(FYI: JDE 4.6 simulator 9000)

 


import java.io.*;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import net.rim.blackberry.api.browser.Browser;
import net.rim.blackberry.api.browser.BrowserSession;
import net.rim.device.api.applicationcontrol.ApplicationPermissions;
import net.rim.device.api.applicationcontrol.ApplicationPermissionsManager;

public class RecordMIDlet extends MIDlet {

    public void startApp() {
        Display.getDisplay(this).setCurrent(new RecordForm());
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}

class RecordForm extends Form implements CommandListener {

    private StringItem messageItem;
    private StringItem errorItem;
    private final Command recordCommand,  playCommand,  cameraCommand;
    private Player p;
    private byte[] recordedSoundArray = null;

    public RecordForm() {
        super("Record Audio");
        messageItem = new StringItem("Record", "Click record to start recording.");
        this.append(messageItem);
        errorItem = new StringItem("", "");
        this.append(errorItem);

        recordCommand = new Command("Record", Command.SCREEN, 1);
        this.addCommand(recordCommand);
        playCommand = new Command("Play", Command.SCREEN, 2);
        this.addCommand(playCommand);
        cameraCommand = new Command("Camera", Command.SCREEN, 3);
        this.addCommand(cameraCommand);
        StringBuffer inhalt = new StringBuffer();
        this.setCommandListener(this);
    }

    public void commandAction(Command comm, Displayable disp) {
        if (comm == recordCommand) {
            checkPermissions();
            new Thread(new Runnable() {

                public void run() {
                    try {
                        p = Manager.createPlayer("capture://audio?encoding=pcm");
                        p.realize();
                        RecordControl rc = (RecordControl) p.getControl("RecordControl");
                        ByteArrayOutputStream output = new ByteArrayOutputStream();
                        rc.setRecordStream(output);
                        rc.startRecord();
                        p.start();
                        messageItem.setText("recording...");
                        Thread.sleep(5000);
                        messageItem.setText("done!");
                        rc.commit();
                        recordedSoundArray = output.toByteArray();
                        p.close();
                        saveMedia("recordTest", recordedSoundArray);
                    } catch (IOException ioe) {
                        errorItem.setLabel("Error");
                        errorItem.setText(ioe.toString());
                    } catch (MediaException me) {
                        errorItem.setLabel("Error");
                        errorItem.setText(me.toString());
                    } catch (InterruptedException ie) {
                        errorItem.setLabel("Error");
                        errorItem.setText(ie.toString());
                    }
                }
            }).start();
        } else if (comm == playCommand) {
            play();
//                ByteArrayInputStream recordedInputStream = new ByteArrayInputStream(recordedSoundArray);
//                Player p2 = Manager.createPlayer(recordedInputStream, "audio/basic");
//                p2.realize();
//                p2.prefetch();
//                p2.start();
        } else if (comm == cameraCommand) {
            Camera.getInstance().showCamera();
        }
    }

    public void play() {

        new Thread(new Runnable() {

            public void run() {
                BrowserSession browserSession = Browser.getDefaultSession();
                browserSession.displayPage("file:///SDCard/recordTest.mp3");

            }
        }).start();

    }

    private void saveMedia(String fileName, byte[] data) throws IOException, SecurityException {
        String imagePath = "SDCard/";
        FileConnection fileConnection = null;
        DataOutputStream dataOutputStream;

        String connection = "file:///" + imagePath + fileName + ".mp3";
        System.out.println("Conn Path:" + connection);

        fileConnection = (FileConnection) Connector.open(connection);
        if (!fileConnection.exists()) {
            fileConnection.create();
        }
        dataOutputStream = fileConnection.openDataOutputStream();
        dataOutputStream.write(data, 0, data.length);
        dataOutputStream.flush();
        dataOutputStream.close();
        if (fileConnection != null) {
            fileConnection.close();
        }
    }

    private void checkPermissions() {


        ApplicationPermissionsManager apm = ApplicationPermissionsManager.getInstance();
        ApplicationPermissions original = apm.getApplicationPermissions();
        if (original.getPermission(ApplicationPermissions.PERMISSION_RECORDING) == ApplicationPermissions.VALUE_ALLOW) {
            return;
        }
        ApplicationPermissions permRequest = new ApplicationPermissions();
        permRequest.addPermission(ApplicationPermissions.PERMISSION_RECORDING);
        boolean acceptance = ApplicationPermissionsManager.getInstance().invokePermissionsRequest(permRequest);
        if (acceptance) {

            // User has accepted all of the permissions.
            return;
        }
    }
}

Thanking you,

Regards,

S.A.Norton Stanley

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: Audio Streaming and playback

Looks like you're recording in PCM format (as opposed to MP3) and then trying to open this stream as MP3.

Developer
Posts: 96
Registered: ‎06-11-2009
My Device: Not Specified

Re: Audio Streaming and playback

Hi,

Thank you. What is the correct format in which i need to record and what format do i have to write the bytes onto the file system ? Kindly help me with this.

 

 

 

Thanking you,

Regards,

S.A.Norton Stanley

Developer
Posts: 96
Registered: ‎06-11-2009
My Device: Not Specified

Re: Audio Streaming and playback

Hi,

I tried recording using amr format and wrote the bytes onto the file system in mp3 format. The browser plays the file and in the end it throws an error message telling An error has occured attempting to play media and on the right corner i get  a message highlighted in red sayin Error Playing. What could be the problem?

 

 

Thanking you,

Regards,

S.A.Norton Stanley

Developer
Posts: 96
Registered: ‎06-11-2009
My Device: Not Specified

Re: Audio Streaming and playback

Hi,

The problem was that i was starting the browser in a thread which was causing the problem. It works fine now.

 

Thanking  you,

S.A.Norton Stanley

Highlighted
New Developer
Posts: 134
Registered: ‎01-11-2011
My Device: curve

Re: Audio Streaming and playback

 please share the code here.