Welcome to the Official BlackBerry® Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Developer
myraddin
Posts: 378
Registered: 09-12-2008
Accepted Solution

Not sure I it is a bug or a feature. We use two players: one to record, another to playback.

This is a continuance of a Thread by bmagown under 'BlackBerry 8300 Series Smartphone - BlackBerry Curve' forum section That can be found on:

 

Full duplex not possible ?

 



"Listen carefully, I shall say this only once"
Please use plain text.
New Developer
bmagown
Posts: 8
Registered: 11-26-2008

Not sure I it is a bug or a feature. We use two players: one to record, another to playback.

Not sure I it is a bug or a feature. We use two players: one to record,
another to playback. Every time 'record' player is started, 'playback'
player is stopped. Does java api allow to record and listen simultaneously?
Handheld itself is obviously capable. Code below:
 
//===================play back code snippet==========
//byte [] inb = some byte array filled with voice data
ByteArrayInputStream bais = new ByteArrayInputStream(inb);
Player pl = javax.microedition.media.Manager.createPlayer(bais,
"audio/mpeg");
pl.realize();
pl.prefetch();
pl.start();
 
//==================recorder code =======================
Player recPLayer =
javax.microedition.media.Manager.createPlayer("capture://audio");
recPLayer.realize();
RecordControl rc = (RecordControl)recPLayer.getControl("RecordControl");
recPLayer.start();                         
ByteArrayOutputStream output = new ByteArrayOutputStream();
rc.setRecordStream(output);
rc.startRecord();
                                     
Thread.sleep(2000);
rc.commit();
byte[] recData = output.toByteArray();
                                      
_out.write(recData);
output.close();
recPLayer.stop();
recPLayer.close();

 
Please use plain text.
New Developer
aaviad
Posts: 5
Registered: 02-18-2009

Re: Not sure I it is a bug or a feature. We use two players: one to record, another to playback.

[ Edited ]

Same problem here. Here is my attempt at demonstrating this and also logging all "playerUpdate" data. 

The program records into a byte-array and then plays it successfully.

 

When trying to record and play and the same time things do not work. The log clearly shows the interference  between the two playes.

 

Is this something that I should expect to work. Any interfaces I am missing to play and record audio in full-duplex?

 

Avner Aviad

 

 

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import javax.microedition.media.Manager;

import javax.microedition.media.Player;

import javax.microedition.media.PlayerListener;

import javax.microedition.media.control.RecordControl;

import net.rim.device.api.ui.Font;

import net.rim.device.api.ui.UiApplication;

import net.rim.device.api.ui.component.ButtonField;

import net.rim.device.api.ui.component.Dialog;

import net.rim.device.api.ui.component.RichTextField;

import net.rim.device.api.ui.container.MainScreen;

public class RecordAndPlay extends UiApplication{

private byte[] _bytesRecorded = null;

private byte[] _bytesToPlay = null;

private MainMenuScreen _screen = null;

 

public static void main(String argv[])

{

new RecordAndPlay();

}

 

public RecordAndPlay()

{

try

{

pushScreen(_screen = new MainMenuScreen());

enterEventDispatcher();

} catch (IOException e)

{

_screen.reportException(e);

}

}

 

class AudioRecorder extends Thread implements PlayerListener

{

public void run()

{

try

{

Player playerR = Manager.createPlayer("capture://audio?encoding=audio/amr");

playerR.addPlayerListener(this);

playerR.realize();

RecordControl recordControl = (RecordControl) playerR.getControl("RecordControl");

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

recordControl.setRecordStream(outputStream);

Manager.playTone(69, 500, 100);

Thread.sleep(500);

recordControl.startRecord();

playerR.start();

Thread.sleep(5000);

recordControl.commit();

_bytesRecorded = outputStream.toByteArray();

outputStream.close();

playerR.close();

synchronized (_screen)

{

_screen.notify();

}

} catch (Exception e)

{

_screen.reportException(e);

}

}

public void playerUpdate(Player player, final String event, Object eventData)

{

_screen.log("R-" + event + "[" + (eventData == null ? "" : eventData.toString()) + "]");

}

}

class AudioPlayer extends Thread implements PlayerListener

{

public void run()

{

try

{

_bytesToPlay = new byte[_bytesRecorded.length];

System.arraycopy(_bytesRecorded, 0, _bytesToPlay, 0, _bytesRecorded.length);

ByteArrayInputStream inputStream = new ByteArrayInputStream(_bytesToPlay);

Player playerP = Manager.createPlayer(inputStream, "audio/amr");

playerP.addPlayerListener(this);

playerP.realize();

playerP.prefetch();

playerP.start();

Thread.sleep(5000);

playerP.stop();

playerP.close();

synchronized (_screen)

{

_screen.notify();

}

} catch (Exception e)

{

_screen.reportException(e);

}

}

public void playerUpdate(Player player, String event, Object eventData)

{

_screen.log("P-" + event + "[" + (eventData == null ? "" : eventData.toString()) + "]");

}

}

class MainMenuScreen extends MainScreen

{

private ButtonField _buttonField = new ButtonField("Start");

private RichTextField _logField = new RichTextField();

private long _tStart;

 

public MainMenuScreen() throws IOException

{

super();

_tStart = System.currentTimeMillis();

add(_buttonField);

Font f = _logField.getFont();

_logField.setFont(f.derive(f.getStyle(), f.getHeight() * 3 / 5));

add(_logField);

}

 

public void log(final String sValue)

{

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

{

public void run()

{

long t = System.currentTimeMillis() - _tStart;

_logField.insert("[" + ((t / 1000) + "." + t % 1000) + "] " + sValue + "\n");

}

});

}

protected boolean navigationClick(int status, int time)

{

Thread r = new Thread()

{

public void run()

{

try

{

synchronized (_screen)

{

log("Recording");

(new AudioRecorder()).start();

_screen.wait();

log("Playing");

(new AudioPlayer()).start();

_screen.wait();

log("Recording & Playing");

(new AudioRecorder()).start();

(new AudioPlayer()).start();

_screen.wait();

_screen.wait();

// log("Playing again");

// (new AudioPlayer()).start();

// _screen.wait();

}

} catch (Exception e)

{

reportException(e);

}

}

};

r.start();

return true;

}

void reportException(final Exception e)

{

UiApplication.getUiApplication().invokeAndWait(new Runnable()

{

public void run()

{

log("Exception " + e.getMessage());

Dialog.inform(e.toString());

}

});

}

}

}



 

 

 

Message Edited by aaviad on 02-18-2009 03:56 PM
Message Edited by aaviad on 02-18-2009 03:59 PM
Message Edited by aaviad on 02-18-2009 04:03 PM
Message Edited by aaviad on 02-18-2009 04:14 PM
Message Edited by aaviad on 02-18-2009 04:14 PM
Please use plain text.
Developer
myraddin
Posts: 378
Registered: 09-12-2008

Re: Not sure I it is a bug or a feature. We use two players: one to record, another to playback.

Not a bug. 

 

MMAPI just does not facilitate full duplex.

 

You cannot record and play at the same time ;(

 

I have used a similar approach as you have and it works in the simulator but not on the device. (On the device the recorder would capture data only when the player was not playing.)

I guess simulator 'enables' this because it just forwards the recording/playing request to the OS.

 

I have researched the subject and have found many reports people trying to do this but all have failed. 

 

But I could never get to the bottom of this. I would like to know why, and exactly at what point does this break. 

 

The MMAPI Manager can handle only one player at a time ? Not true since you can play more than one audio at a time.

 

So why is there a limit that we cannot record and play at the same time - where exactly is the catch?

Anyone has a technical explanation of this ?



"Listen carefully, I shall say this only once"
Please use plain text.
Forums Veteran I
drizzt09
Posts: 2,087
Registered: 03-11-2009

Re: Not sure I it is a bug or a feature. We use two players: one to record, another to playback.

this looks like stuff that should be in the development section http://supportforums.blackberry.com/rim?category.id=BlackBerryDevelopment
Please use plain text.
Developer
myraddin
Posts: 378
Registered: 09-12-2008

Re: Not sure I it is a bug or a feature. We use two players: one to record, another to playback.

[ Edited ]

@drizzt09

 

You are absolutely right.

I missed the 'secton name' part of the thread :smileywink: (found it through generic search)

 

Please continue the discussion on: Full duplex - developers section

 

 

Message Edited by myraddin on 07-02-2009 02:27 PM


"Listen carefully, I shall say this only once"
Please use plain text.
Administrator
MSohm
Posts: 11,465
Registered: 07-09-2008
My Carrier: Bell

Re: Not sure I it is a bug or a feature. We use two players: one to record, another to playback.

The behaviour you see here can vary based on the BlackBerry smartphone model due to the fact that it is primarily based on the underlying hardware of the BlackBerry smartphone.  Simultaneous playback and recording is possible on some models, however only one codec can be used at the same time.  This means that you'd need to record and play back audio using the same format.
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Found a bug? Report it using the Issue Tracker
Please use plain text.
Developer
parlakisik
Posts: 18
Registered: 08-05-2009

Re: Not sure I it is a bug or a feature. We use two players: one to record, another to playback.

Hi mark ,

 

could you please give model's name that support playback and recording simultaneously ?

Please use plain text.
New Developer
mc123
Posts: 12
Registered: 07-23-2009

Re: Not sure I it is a bug or a feature. We use two players: one to record, another to playback.

I am working on a project that is trying to do something with playback and recording, so I would also like to know the models that can do simultaneous playback + record.
Please use plain text.
Developer
branchcut
Posts: 37
Registered: 10-17-2008

Re: Not sure I it is a bug or a feature. We use two players: one to record, another to playback.

I was able to get the code sample posted by aaviad to work -- but with a catch, it only seems to work when passing audio/amr as the codec. 

 

If I use the exact same code snippet, but instead pass audio/basic as the codec (to both the player and recorder), the simultaneous record/playback part of the test fails.  So it records OK, plays back OK, but then when it tries to playback while simultaneously recording, the playback does not work, and when it finally plays back the second recording nothing is heard either.  Eg, the crucial part of the test fails.

 

I get this same behavior on both the 4.6 emulator and a GSM Blackberry Bold device running OS 4.6.  

 

Has anyone reading this gotten simultaneous playback/record to work with the audio/basic (pcm) codec and willing to share their findings?  

 

 

Please use plain text.