Use custom DataSource to play audio!
Please Login to Remove!
Hi All,I've just try to play some raw audio bytes from some file.The problem is that if I set some ContentLength for SourceStream and after it reach that value no audio is playing furter:
package mypackage;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.media.Control;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.protocol.ContentDescripto r;
import javax.microedition.media.protocol.DataSource;
import javax.microedition.media.protocol.SourceStream;
import net.rim.device.api.io.IOUtilities;
import net.rim.device.api.media.control.AudioPathControl;
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.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldMan ager;
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 MyScreen extends MainScreen implements FieldChangeListener,
PlayerListener {
private ButtonField playAudio;
private ButtonField stopAudio;
private LabelField status;
private byte[] dencoded = null;
private Player player = null;
private int len = 0;
private int curPos = 0;
/**
* Creates a new MyScreen object
*/
private SourceStream gogiStream = new SourceStream() {
ContentDescriptor mContentDescriptor = new ContentDescriptor(
"audio/x-pcm");
private static final int defSize = 320;
public int read(byte[] b, int offset, int length) throws IOException {
if(length <= defSize || offset >= (getContentLength() - defSize))
{
offset = 0;
length = (int) getContentLength();
// try {
// player = null;
// start();
// } catch (MediaException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// return (-1)*(offset);
}
int cur = 0;
while(cur<defSize)
{
b[offset+cur] = dencoded[curPos++];
++cur;
}
return defSize;// curPos < length ? curPos : length;
}
public ContentDescriptor getContentDescriptor() {
return mContentDescriptor;
}
public long getContentLength() {
return 64000;
}
public int getSeekType() {
return SourceStream.SEEKABLE_TO_START;
}
public int getTransferSize() {
return 320;
}
public long seek(long where) throws IOException {
return where;
// throw new IOException("not seekable");
}
public long tell() {
// if (mStartTime == 0)
return 0;
// return (System.currentTimeMillis() - mStartTime);
}
public Control getControl(String controlType) {
return null;
}
public Control[] getControls() {
return null;
}
};
public MyScreen() {
// Set the displayed title of the screen
setTitle("AudioTest");
playAudio = new ButtonField("Play Audio");
playAudio.setChangeListener(this);
stopAudio = new ButtonField("Stop Audio");
stopAudio.setChangeListener(this);
HorizontalFieldManager hm = new HorizontalFieldManager();
hm.add(playAudio);
hm.add(stopAudio);
add(hm);
status = new LabelField();
add(status);
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
encoded = readTextFile("file:///SDCard/BlackBerry/documents/soundTest.txt");
len = dencoded.length;
} else {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.alert("check SDCard!");
}
});
}
}
});
}
private byte[] readTextFile(String fName) {
FileConnection fconn = null;
DataInputStream is = null;
byte[] data = null;
try {
fconn = (FileConnection) Connector.open(fName, Connector.READ);
is = fconn.openDataInputStream();
data = IOUtilities.streamToBytes(is);
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
try {
if (null != is)
is.close();
if (null != fconn)
fconn.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
return data;
}
public void fieldChanged(Field field, int context) {
if (field == playAudio) {
try {
start();
} catch (Exception e) {
Dialog.alert(e.getMessage());
}
} else if (field == stopAudio) {
stop();
}
}
private void start() throws IOException, MediaException {
if (null == player) {
// //////////////////////// DataSource /////////////
player = Manager.createPlayer(new DataSource(null) {
SourceStream[] mStream = { gogiStream };
public void connect() throws IOException {
}
public void disconnect() {
}
public String getContentType() {
return gogiStream.getContentDescriptor().getContentType() ;
}
public SourceStream[] getStreams() {
return mStream;
}
public void start() throws IOException {
}
public void stop() throws IOException {
}
public Control getControl(String controlType) {
return null;
}
public Control[] getControls() {
return null;
}
});
// ///////////////////// DataSource ///////////////
// ///////////////ByteArrayInputStream/////////////////////////////
// ByteArrayInputStream theInput = new ByteArrayInputStream(dencoded);
// player = Manager.createPlayer(theInput, "audio/x-pcm");
// ///////////////ByteArrayInputStream/////////////////////////////
player.addPlayerListener(this);
player.realize();
AudioPathControl lPathCtr = (AudioPathControl) player
.getControl("net.rim.device.api.media.control.Audi oPathControl");
lPathCtr.setAudioPath(AudioPathControl.AUDIO_PATH_ HANDSET);
player.stop();
player.prefetch();
curPos = 0;
player.start();
status.setText("Playing...");
invalidate();
}
}
private void stop() {
if (player == null)
return;// nothing to stop
curPos = 0;
try {
if (player.getState() == Player.STARTED) {
player.stop();
}
if (player.getState() != Player.CLOSED) {
player.close();
}
player = null;
status.setText("Stopped!");
invalidate();
} catch (MediaException e) {
}
}
public void playerUpdate(Player player, String event, Object eventData) {
if (event == PlayerListener.BUFFERING_STARTED) {
// mBuffering = true;
} else if (event == PlayerListener.DEVICE_UNAVAILABLE) {
// pausing both player and recorder
try {
// already stooped player.stop();
// mSendStream.getPlayer().stop();
} catch (Throwable e) {
}
} else if (event == PlayerListener.DEVICE_AVAILABLE) {
// starting both player and recorder
try {
// mSendStream.getPlayer().start();
stop();
start();
} catch (Throwable e) {
}
}
}
}
It even doesn't work when I write -1 as return value in getContentSize().
Is there any idea,how to play the whole audio without grouing the value of 64000,becuse I needn't that.If it will success I'll use it for real time audio playing.
Regards,
Arsen
__________________
Regards,
Arsen
|