Código de ejemplo: grabar audio en una aplicación del dispositivo BlackBerry
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import java.lang.*;
import javax.microedition.media.*;
import java.io.*;
import javax.microedition.media.control.*;
public class AudioRecordingDemo extends UiApplication
{
public static void main(String[] args)
{
AudioRecordingDemo app = new AudioRecordingDemo();
app.enterEventDispatcher();
}
public AudioRecordingDemo()
{
pushScreen(new AudioRecordingDemoScreen());
}
private class AudioRecordingDemoScreen extends MainScreen
{
private AudioRecorderThread _recorderThread;
public AudioRecordingDemoScreen()
{
setTitle("Audio recording demo");
addMenuItem(new StartRecording());
addMenuItem(new StopRecording());
}
private class StartRecording extends MenuItem
{
public StartRecording()
{
super("Start recording", 0, 100);
}
public void run()
{
try
{
AudioRecorderThread newRecorderThread = new AudioRecorderThread();
newRecorderThread.start();
_recorderThread = newRecorderThread;
}
catch (Exception e)
{
Dialog.alert(e.toString());
}
}
}
private class StopRecording extends MenuItem
{
public StopRecording()
{
super("Stop recording", 0, 100);
}
public void run()
{
try
{
if (_recorderThread != null)
{
_recorderThread.stop();
}
}
catch (Exception e)
{
Dialog.alert(e.toString());
}
}
}
private class AudioRecorderThread extends Thread implements javax.microedition.media.PlayerListener
{
private Player _player;
private RecordControl _recordControl;
AudioRecorderThread()
{
}
public void run()
{
try
{
_player = javax.microedition.media.Manager.createPlayer("capture://audio?encoding=audio/amr");
_player.addPlayerListener(this);
_player.realize();
_recordControl = (RecordControl) _player.getControl( "RecordControl" );
_recordControl.setRecordLocation("file:///store/home/user/AudioRecordingTest.amr");
_recordControl.startRecord();
_player.start();
}
catch( IOException e )
{
Dialog.alert(e.toString());
}
catch( MediaException e )
{
Dialog.alert(e.toString());
}
}
public void stop()
{
if (_player != null)
{
_player.close();
_player = null;
}
if (_recordControl != null)
{
_recordControl.stopRecord();
try
{
_recordControl.commit();
}
catch (Exception e)
{
Dialog.alert(e.toString());
}
_recordControl = null;
}
}
public void playerUpdate(Player player, String event, Object eventData)
{
Dialog.alert("Player " + player.hashCode() + " got event " + event + ": " + eventData);
}
}
}
}
Tema siguiente: Trabajo con vídeo en un dispositivo BlackBerry
Tema anterior: Grabar audio en una aplicación del dispositivo BlackBerry
¿Le ha resultado útil esta información? Envíenos sus comentarios.