BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 10-18-2011, 05:46 AM   #1
arsenk
New Member
 
Join Date: Jul 2011
Model: 9800T
PIN: N/A
Carrier: developer
Posts: 3
Default 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
Offline  
Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads for: Use custom DataSource to play audio!
Thread Thread Starter Forum Replies Last Post
Custom Ringtones Still Play When In Vibrate Mode?!?!?!?! uniek81 General 9700 Series Discussion - Bold 2 6 12-02-2009 09:35 PM
Can BB's play Flac files? nez Media Center 0 07-25-2008 01:45 AM
Play audio file attachments on your 8800 wav mp3 wma!!! ntegrt General Legacy Device Discussion 6 04-19-2007 11:19 PM
Audio formats supported by BB????? lalitsharma Developer Forum 2 05-18-2005 11:57 PM


Jelenko Commodore LS VPF with Pump  picture

Jelenko Commodore LS VPF with Pump

$875.00



Vintage Holt Commodore Commercial Floor Scrubber Polisher Buffer Burnisher picture

Vintage Holt Commodore Commercial Floor Scrubber Polisher Buffer Burnisher

$599.99



Mitsubishi Control Board DM00N649.  SM76A127G02 . Ductless unit 47-0910KR (C64) picture

Mitsubishi Control Board DM00N649. SM76A127G02 . Ductless unit 47-0910KR (C64)

$120.00



3D MULTIMEDIA VIDEO CARD C64/V2 1MB picture

3D MULTIMEDIA VIDEO CARD C64/V2 1MB

$94.05



POLAR PLASTICS C64 Construction Film,6x100,4Mil,Clear PK 4 picture

POLAR PLASTICS C64 Construction Film,6x100,4Mil,Clear PK 4

$178.04



Karcher Commodore Due Walk Behind Carpet Extractor #1.008-004.0 picture

Karcher Commodore Due Walk Behind Carpet Extractor #1.008-004.0

$9900.00







Copyright © 2004-2016 BlackBerryForums.com.
The names RIM © and BlackBerry © are registered Trademarks of BlackBerry Inc.