Volume Contro Null Pointer Exception
Please Login to Remove!
Hey all, i'm new to BB development and im trying to make an audio recording and playback app. I manage to record and stop the recording of the audio well, but when it comes to playing i get a null pointer exception when i call the play() method, specifically in the volume.setLevel() line. Can anyone help? My source code is below:
package Clases;
import java.io.*;
import java.lang.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import net.rim.device.api.ui.component.Dialog;
public class AudioRecorderThread extends Thread implements javax.microedition.media.PlayerListener{
private Player _player;
private RecordControl _recordControl;
private ByteArrayOutputStream output;
private ByteArrayInputStream input;
private byte[] _audioArray = null;
public byte[] get_audioArray() {
return _audioArray;
}
AudioRecorderThread()
{
output = new ByteArrayOutputStream();
}
public void playerUpdate(Player player, String event, Object eventData) {
// TODO Auto-generated method stub
}
public void run()
{
try
{
_player = Manager.createPlayer("capture://audio?encoding=audio/amr");
_player.addPlayerListener(this);
_player.realize();
_recordControl = (RecordControl) _player.getControl( "RecordControl" );
_recordControl.setRecordStream(output);
_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)
{
try
{
_recordControl.commit();
_audioArray = output.toByteArray();
}
catch (Exception e)
{
Dialog.alert(e.toString());
}
_recordControl = null;
}
}
public void play()
{
try {
input = new ByteArrayInputStream(_audioArray);
_player = Manager.createPlayer(input, "audio/x-wav");
_player.realize();
VolumeControl volume = (VolumeControl)_player.getControl("Volume Control");
volume.setLevel(50);
_player.prefetch();
_player.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
That's the thread and this is the screen where i call it from:
package Clases;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
public final class PrincipalScreen extends MainScreen implements FieldChangeListener
{
private ButtonField _boton1,_boton2, _boton3;
private LabelField _label;
private VerticalFieldManager _vertManager;
private HorizontalFieldManager _horiManager1;
private HorizontalFieldManager _horiManager2;
private AudioRecorderThread _recorderThread;
public PrincipalScreen()
{
_vertManager = new VerticalFieldManager();
_horiManager1 = new HorizontalFieldManager();
_horiManager2 = new HorizontalFieldManager();
_vertManager.add(_horiManager1);
_vertManager.add(_horiManager2);
_boton1 = new ButtonField("Record", ButtonField.CONSUME_CLICK);
_boton1.setChangeListener(this);
_boton2 = new ButtonField("Stop", ButtonField.CONSUME_CLICK);
_boton2.setChangeListener(this);
_boton3 = new ButtonField("Play", ButtonField.CONSUME_CLICK);
_boton3.setChangeListener(this);
_label = new LabelField("Choose");
_horiManager1.add(_label);
_horiManager2.add(_boton1);
_horiManager2.add(_boton2);
_horiManager2.add(_boton3);
this.add(_vertManager);
setTitle("Main Screen");
}
public void fieldChanged(Field field, int context)
{
if(field == _boton1)
{
AudioRecorderThread _audioThread = new AudioRecorderThread();
_audioThread.start();
_recorderThread = _audioThread;
// PopScreen popScreen = new PopScreen("Pop");
// UiApplication.getUiApplication().pushScreen(popScr een);
}
if(field == _boton2)
{
try
{
if (_recorderThread != null)
{
_recorderThread.stop();
}
}
catch (Exception e)
{
Dialog.alert(e.toString());
}
}
if(field == _boton3)
{
try
{
if (_recorderThread != null)
{
_recorderThread.play();
}
}
catch (Exception e)
{
Dialog.alert(e.toString());
}
}
}
}
|