If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

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
New Contributor
UnknownMister
Posts: 2
Registered: ‎11-30-2012
My Device: bold 9700
My Carrier: Claro

Simultaneous recording/playback

[ Edited ]

First of all sorry if it´s a very repeated post.

 

I'm making an application that uses simultaneous recording and playback, some days ago I read this post:

 

http://supportforums.blackberry.com/t5/Java-Development/Everything-You-Need-To-Know-About-Simultaneo...

 

OK here is the test code (JDE API 6.0.0)

 

Screen class

package audiotest;

import javax.microedition.media.Manager;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class AudioTestScreen extends MainScreen
{
	protected ButtonField record1 = new ButtonField("Record 1");
	protected ButtonField playback1 = new ButtonField("Playback 1");
	protected ButtonField stop1 = new ButtonField("Stop 1");
	
	protected ButtonField record2 = new ButtonField("Record 2");
	protected ButtonField playback2 = new ButtonField("Playback 2");
	protected ButtonField stop2 = new ButtonField("Stop 2");
	
	private Recorder thread1;
	private Recorder thread2;
    /**
     * Creates a new AudioTestScreen object
     */
    public AudioTestScreen()
    {        
        // Set the displayed title of the screen 
    	try
    	{
    		thread1 = new Recorder(Manager.createPlayer("capture://audio?encoding=audio/amr&voipmode=true&rate=4750"));
    		thread2 = new Recorder(Manager.createPlayer("capture://audio?encoding=audio/amr&voipmode=true&rate=4750"));
    	}
    	catch(Exception e)
    	{
    		Dialog.alert(e.toString());
    	}
        setTitle("AudioTest");
        
        record1.setChangeListener(new FieldChangeListener(){
			public void fieldChanged(Field field, int context) {
				// TODO Auto-generated method stub
				thread1.start();
			}	
        });
        
        stop1.setChangeListener(new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				// TODO Auto-generated method stub
				thread1.stop();
			}
		});
        
        playback1.setChangeListener(new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				// TODO Auto-generated method stub
				try
				{
					thread1.playback(Manager.createPlayer(thread1.getInputStream(), "audio/amr"));
				}
				catch(Exception e)
				{
					Dialog.alert(e.toString());
				}
			}
		});
        
        record2.setChangeListener(new FieldChangeListener(){
			public void fieldChanged(Field field, int context) {
				// TODO Auto-generated method stub
				thread2.start();
			}	
        });
        
        stop2.setChangeListener(new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				// TODO Auto-generated method stub
				thread2.stop();
			}
		});
        
        playback2.setChangeListener(new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				// TODO Auto-generated method stub
				try
				{
					thread2.playback(Manager.createPlayer(thread2.getInputStream(), "audio/amr"));
				}
				catch(Exception e)
				{
					Dialog.alert(e.toString());
				}
			}
		});
        
        add(record1);
        add(stop1);
        add(playback1);
        add(record2);
        add(stop2);
        add(playback2);
    }
}

 

Recorder class

package audiotest;

import java.lang.Thread;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.microedition.media.Player;
import javax.microedition.media.control.RecordControl;

public class Recorder extends Thread {
	
	public Player player;
	public ByteArrayOutputStream output;
	public byte[] data;
	public RecordControl rcontrol;
	
	public Player newPlayer;
	
	public Recorder(Player recorderObj)
	{
		player = recorderObj;
	}
	
	public void run()
	{
		try
		{
			output = new ByteArrayOutputStream();
			player.realize();
			rcontrol = (RecordControl)player.getControl("RecordControl");
			rcontrol.setRecordStream(output);
			rcontrol.startRecord();
			player.start();
		}
		catch(Exception e)
		{
		}
	}
	
	public void stop()
	{
		try{
			
			rcontrol.commit();
			data = output.toByteArray();
			output.close();
			player.close();
		} catch (Exception e2){
			//Perform actions
		}
	}
	
	public String getData(){
		return "" + data.length;	
		
	}
	
	public InputStream getInputStream()
	{
		InputStream input = new ByteArrayInputStream(data);
		return input;
	}
	
	public void playback(Player playbackObj) throws MediaException
	{
		newPlayer = playbackObj;
		newPlayer.realize();
		newPlayer.prefetch();
		newPlayer.start();
	}
	
}

 

thread1 variable is used to record some audio for after use (as a playback for the test), thread2 is used to record audio when thread1 is playbacking the audio captured before.

 

1- I press the record1 button to capture some testing audio

2- I press the stop1 button to stop recording audio from thread1

3- I press the record2 button to start other audio recording(the post said that the recorder must be started before the playback)

4- I press playback1 button to start playback

5- I press stop2 button to stop recording

6- I press playback2 button to listen the recorded audio

 

In the sixth step, when I listen to the second recorded audio, I listen the same audio in the first recorded audio (yes in the second recording I pronounce some words as a test) or simply records audio after the playback completely played the audio. What is my problem in the code?, I tested the app on a BlackBerry Curve 9230.

 

Thanks.

Please use plain text.
BlackBerry Development Advisor (Retired)
gsandhu
Posts: 41
Registered: ‎07-14-2008
My Device: Not Specified

Re: Simultaneous recording/playback

Hi, I ran your sample application on my BlackBerry device and on step 6 I was able to hear the voice overlap of the first and second recording.  I was able to hear both my second and first recording on step 6.  This works fine.  Can you confirm if you are using BlackBerry® OS 6.0.0.524 (bundle 2333)+.  Can you try using 7.0+ handheld code.

Please use plain text.