BlackBerry Support Forum - Powered by vBulletin
Results 1 to 5 of 5
  1. #1
    arsenk01 is offline Member
    BlackBerry Device: any
     
    Device Firmware: 6.0
     
    Email Configuration: No Data Plan - Phone Only
     
    Mobile Carrier & location: developer
     
    Join Date
    Jun 2011
    Posts
    37

    Use custom DataSource to play audio!

    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) {
    }
    }

    }
    }

    If instead of giving 64000 fixed size I write -1 ,which means I don't know content size then I recieve 42 in read function.
    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

  2. #2
    GaryCutri's Avatar
    GaryCutri is offline Moderator
    BlackBerry Device: z10
     
    Device Firmware: 10.1.0.264
     
    Email Configuration: BlackBerry 10 - Standard Data Plan
     
    Mobile Carrier & location: Telstra - Australia
     
    Join Date
    Sep 2006
    Location
    Melbourne
    Posts
    17,993
    Images
    376
    Blog Entries
    68

  3. #3
    arsenk01 is offline Member
    BlackBerry Device: any
     
    Device Firmware: 6.0
     
    Email Configuration: No Data Plan - Phone Only
     
    Mobile Carrier & location: developer
     
    Join Date
    Jun 2011
    Posts
    37

    Re: Use custom DataSource to play audio!

    Hi Gary,
    I'm familiar with that topics.Unfortunately they all about how to play fixed size audio,that is very simple,however I would like to play real time audio,which means I'll receive some encoded data(bytes) d encode them and play as raw data.The problem is that when player reaches the end of that data it stops playing anything.

  4. #4
    GaryCutri's Avatar
    GaryCutri is offline Moderator
    BlackBerry Device: z10
     
    Device Firmware: 10.1.0.264
     
    Email Configuration: BlackBerry 10 - Standard Data Plan
     
    Mobile Carrier & location: Telstra - Australia
     
    Join Date
    Sep 2006
    Location
    Melbourne
    Posts
    17,993
    Images
    376
    Blog Entries
    68
    Quote Originally Posted by arsenk01 View Post
    Hi Gary,
    I'm familiar with that topics.Unfortunately they all about how to play fixed size audio,that is very simple,however I would like to play real time audio,which means I'll receive some encoded data(bytes) d encode them and play as raw data.The problem is that when player reaches the end of that data it stops playing anything.
    As I am not aware of how to resolve your issue it would be best that I refer you to the Official BlackBerry Developer Forum = Application Platforms - BlackBerry Support Community Forums

  5. #5
    arsenk01 is offline Member
    BlackBerry Device: any
     
    Device Firmware: 6.0
     
    Email Configuration: No Data Plan - Phone Only
     
    Mobile Carrier & location: developer
     
    Join Date
    Jun 2011
    Posts
    37
    Thanks Gary,I already posted there too,but got no explanation or suggestion.

Similar Threads

  1. Audio buffering
    By arsenk01 in forum BlackBerry Java Development Environment
    Replies: 1
    Last Post: 24-10-2011, 12:22 PM
  2. Custom ringtone for SMS
    By Poppy in forum BlackBerry Legacy Device Discussion
    Replies: 4
    Last Post: 19-10-2010, 05:08 PM
  3. Gapless Audio
    By EpharGy in forum BlackBerry Legacy Device Discussion
    Replies: 1
    Last Post: 13-05-2010, 06:10 PM
  4. BB audio problems
    By bigredinmo in forum BlackBerry Legacy Device Discussion
    Replies: 3
    Last Post: 09-03-2010, 11:23 AM
  5. audio transfer from PC
    By rolly clemente in forum BlackBerry Pearl Smartphone Discussion
    Replies: 2
    Last Post: 31-07-2007, 12:00 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Search Engine Friendly URLs by vBSEO 3.6.1