BlackBerry Forums Support Community

BlackBerry Forums Support Community (http://www.blackberryforums.com/index.php)
-   Media Center (http://www.blackberryforums.com/forumdisplay.php?f=54)
-   -   Voice-recording in BB bold (http://www.blackberryforums.com/showthread.php?t=154710)

dolan 10-14-2008 12:12 AM

Voice-recording in BB bold
 
Hi All,

I am working on voice recording. can anybody give suggestion or any sample code aboubt that?
Thanks in advance,

MM.

dolan 10-17-2008 05:41 AM

Hi All,

for Voice recording i am not getting the output. by debugging the program , i understood that its not checking the conditions. that means, its always showing null value for VideoControl. I am posting my code. Please reply me as soon as possible. I am in hurry.

Thanks in advance.




package com.rim.player;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Canvas;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.RecordControl;
import javax.microedition.media.control.VideoControl;
import javax.microedition.media.control.VolumeControl;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.HorizontalFieldMan ager;
import net.rim.device.api.ui.container.MainScreen;

public class Voice extends UiApplication{

public static void main(String[] args){
Voice voice = new Voice();
voice.enterEventDispatcher();
}
public Voice(){
Recorder screen = new Recorder();
pushScreen(screen);
}

// A UI screen to display our media player.
class Recorder extends MainScreen implements FieldChangeListener , PlayerListener{
private Player p;
private VolumeControl _volumeControl;
private RichTextField _statusField;
private ButtonField _controlButton;
private Field _recordField;
private Recorder _mainScreen;

private LabelField _currentTime;
private LabelField _duration;
private LabelField _volumeDisplay;

private TimerUpdateThread _timerUpdateThread;
private BasicEditField _videoField;

Recorder(){
_mainScreen = this;
setTitle("Voice Recording Demo");
_statusField = new RichTextField("Loading recorder, please wait...");
add(_statusField);

UiApplication.getUiApplication().invokeLater(new Runnable() {


private Player recordPlayer;
private RecordControl rc;
private String encoding_type;
private ByteArrayOutputStream output;
private Canvas canvas;
/**/ private VideoControl _videoControl;

//private VideoControl vc = (VideoControl)p.getControl( "javax.microedition.media.control.VideoControl ");
//private VideoControl vc = ;


public void run() {


try {
Player p = Manager.createPlayer("capture://audio?encoding=amr");
p.realize();
VideoControl vc = (VideoControl)p.getControl("Voice Dialing");


// start the Player and record for 7 seceonds.
rc = (RecordControl)p.getControl("RecordControl");
output = new ByteArrayOutputStream();
rc.setRecordStream(output);
rc.setRecordSizeLimit(20);
rc.startRecord();
p.start();
Thread.sleep(7000);
rc.commit();
p.close();
if (vc != null) {
vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, null);//USE_DIRECT_VIDEO, canvas);
vc.setDisplayFullScreen(true);
vc.setVisible(true);

addFields();
}
else
{
_statusField.setText("Error: Could not load media");
}



} catch (IOException ioe) {
} catch (MediaException me) {
} catch (InterruptedException ie) {}


// if(_videoField != null)
// {
// addFields();
// }
// else
// {
// _statusField.setText("Error: Could not load media");
// }
}
});
}

private void addFields()
{
delete(_statusField);
add(_videoField);
HorizontalFieldManager hfm1 = new HorizontalFieldManager(Field.FIELD_HCENTER);
_controlButton = new ButtonField("Start");
_controlButton.setChangeListener(this);
hfm1.add(_controlButton);
add(hfm1);

HorizontalFieldManager hfm2 = new HorizontalFieldManager(Field.FIELD_HCENTER);
_currentTime = new LabelField("-");
_duration = new LabelField("- s");

hfm2.add(_currentTime);
hfm2.add(new LabelField(" / "));
hfm2.add(_duration);
hfm2.add(new LabelField("\t\t"));

add(hfm2);
}




public void fieldChanged(Field field, int context)
{
if (_controlButton.getLabel().equals("Start"))
{
try
{

p.start();

_timerUpdateThread = new TimerUpdateThread();
_timerUpdateThread.start();
}
catch(MediaException pe)
{
System.out.println(pe.toString());
}
}
else
{
try
{

p.stop();

_timerUpdateThread.stop();
}
catch(MediaException pe)
{
System.out.println(pe.toString());
}
}
}
public void playerUpdate(Player player, final String event, Object eventData)
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{

public void run()
{


Dialog.alert(p.toString());
if (event.equals(VOLUME_CHANGED))
{
_volumeDisplay.setText("Volume : " + _volumeControl.getLevel());
}
else if (event.equals(STARTED ))
{
_currentTime.setText(" ");
_controlButton.setLabel("Pause");
}
else if (event.equals(STOPPED))
{
_currentTime.setText(p.getMediaTime()/1000000 + "");
_controlButton.setLabel("Start");
}
else if (event.equals(DURATION_UPDATED))
{
_duration.setText(p.getDuration()/1000000 + " s");
}
else if (event.equals(END_OF_MEDIA))
{
_controlButton.setLabel("Start");
}
}
});
}
protected boolean invokeAction(int action)
{
boolean handled = super.invokeAction(action);

if(!handled)
{
switch(action)
{
case ACTION_INVOKE: // Trackball click.
{
return true;
}
}
}
return handled;
}


protected boolean onSavePrompt()
{
return true;
}

private class TimerUpdateThread extends Thread
{
private boolean _threadCanRun;

public void run()
{
_threadCanRun = true;
while( _threadCanRun ) {
UiApplication.getUiApplication().invokeLater( new Runnable() {
public void run()
{
_currentTime.setText( p.getDuration() / 1000000 + "" );
}
} );

try {
Thread.sleep( 500L );
} catch( InterruptedException e ) {
}
}
}

public void stop()
{
_threadCanRun = false;
}
}
}
}


All times are GMT -5. The time now is 08:15 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.