BlackBerry - "Media unloaded while initializing" error - blackberry

I am trying to play a video based on the videorecordingdemo sample from JDE7.0 (code below).
I am building for OS5.0 - which is why I cannot just run the whole demo app which is written for OS7.0.
I am using the emulator (9300) and have set up a folder to be used as my sd card. I have placed a .AVI video in there. I have also tried placing the .AVI into the resources, and streaming it from there.
In both cases, when I call Player.start(), the following exception is thrown:
net.rim.device.internal.media.RimMediaException: Media unloaded while initializing
I would like to know:
What does this error mean?
How can I fix it?
Alternatively are the any really special, stringent requirements for .AVI files to play?
Update: Tried the app on a real Torch device, and it gave
net.rim.device.internal.media.UnloadedMediaException
FWIW here is the sample code - as copied directly from the videorecordingdemo sample app.
package mypackage;
/*
* VideoPlaybackScreen.java
*
* Copyright © 1998-2011 Research In Motion Ltd.
*
* Note: For the sake of simplicity, this sample application may not leverage
* resource bundles and resource strings. However, it is STRONGLY recommended
* that application developers make use of the localization features available
* within the BlackBerry development platform to ensure a seamless application
* experience across a variety of languages and geographies. For more information
* on localizing your application, please refer to the BlackBerry Java Development
* Environment Development Guide associated with this release.
*/
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VideoControl;
import javax.microedition.media.control.VolumeControl;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
/**
* A screen for playing a video
*/
public class VideoPlaybackScreen extends MainScreen
{
private Player _videoPlayer;
private VideoControl _videoControl;
/**
* Constructs a screen to playback the video from a specified input stream
*
* #param inputStream The InputStream of the video to display
*
* #throws NullPointerException Thrown if <code>inputStream</code> is null
*/
public VideoPlaybackScreen(InputStream inputStream)
{
if (inputStream == null)
{
throw new NullPointerException("'inputStream' cannot be null");
}
try
{
_videoPlayer = javax.microedition.media.Manager.createPlayer(inputStream, "video/sbv");
initScreen();
}
catch( Exception e )
{
Dialog.alert("Exception while initializing the playback video player\n\n" + e);
}
}
/**
* Constructs the screen to playback the video from a file
*
* #param file A locator string in URI syntax that points to the video file
*/
public VideoPlaybackScreen(String file)
{
boolean notEmpty;
try
{
FileConnection fconn = (FileConnection) Connector.open(file);
notEmpty = fconn.exists() && fconn.fileSize() > 0;
fconn.close();
}
catch( IOException e )
{
Dialog.alert("Exception while accessing the video filesize:\n\n" + e);
notEmpty = false;
}
// Initialize the player if the video is not empty
if( notEmpty )
{
try
{
_videoPlayer = javax.microedition.media.Manager.createPlayer(file);
initScreen();
}
catch( Exception e )
{
Dialog.alert("Exception while initializing the playback video player\n\n" + e);
}
}
else
{
add(new LabelField("The video file you are trying to play is empty"));
}
}
/**
* Initializes the screen after the player has been created
*
* #throws Exception Thrown if an error occurs when initializing the video player, video display or volume control
*/
private void initScreen() throws Exception
{
_videoPlayer.realize();
_videoPlayer.addPlayerListener(new PlayerListener()
{
/**
* #see javax.microedition.media.PlayerListener#playerUpdate(Player, String, Object)
*/
public void playerUpdate(Player player, String event, Object eventData)
{
// Alert the user and close the screen after the video has
// finished playing.
if( event == PlayerListener.END_OF_MEDIA )
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert("Finished playing");
close();
}
});
}
}
});
// Set up the playback
_videoControl = (VideoControl) _videoPlayer.getControl("VideoControl");
Field vField = (Field) _videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE,
"net.rim.device.api.ui.Field");
add(vField);
VolumeControl vol = (VolumeControl) _videoPlayer.getControl("VolumeControl");
vol.setLevel(30);
}
/**
* #see net.rim.device.api.ui.Field#onVisibilityChange(boolean)
*/
protected void onVisibilityChange(boolean visible)
{
// If the screen becomes visible and the video player was created, then
// start the playback.
if( visible && _videoPlayer != null )
{
try
{
_videoPlayer.start();
}
catch( final Exception e )
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
// If starting the video fails, terminate the playback
Dialog.alert("Exception while starting the video\n\n" + e);
close();
}
});
}
}
}
/**
* #see net.rim.device.api.ui.Screen#onClose()
*/
public void close()
{
// Close the video player if it was created
if( _videoPlayer != null )
{
_videoPlayer.close();
}
super.close();
}
}

Related

proper video streaming with rxjava

To handle a video stream from a webcam (delivered by opencv) i am considering to use RxJava.
I am hoping to achieve the following:
being able to control the frames per second to be delivered
to be able to handle different inputs - e.g. a life webcam, a video or even a still picture
being able to switch to a picture-by-picture handling under the control of a gui
I have been experimenting a bit with RxJava but i am confused about the debounce, throttleFirst and async operators
Examples like https://stackoverflow.com/a/48723331/1497139 show some code but I am missing more detailed explanation.
Where could I find a decent example for video processing or something similar along the needs mentioned above?
The code below does some non async logic at this time - please let me know if i could build on it:
ImageFetcher
import org.opencv.core.Mat;
import org.opencv.videoio.VideoCapture;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func0;
import rx.functions.Func1;
/**
* fetcher for Images
*
*/
public class ImageFetcher {
// OpenCV video capture
private VideoCapture capture = new VideoCapture();
private String source;
protected int frameIndex;
public int getFrameIndex() {
return frameIndex;
}
/**
* fetch from the given source
*
* #param source
* - the source to fetch from
*/
public ImageFetcher(String source) {
this.source = source;
}
/**
* try opening my source
*
* #return true if successful
*/
public boolean open() {
boolean ret = this.capture.open(source);
frameIndex=0;
return ret;
}
/**
* fetch an image Matrix
*
* #return - the image fetched
*/
public Mat fetch() {
if (!this.capture.isOpened()) {
boolean ret = this.open();
if (!ret) {
String msg = String.format(
"Trying to fetch image from unopened VideoCapture and open %s failed",
source);
throw new IllegalStateException(msg);
}
}
final Mat frame = new Mat();
this.capture.read(frame);
frameIndex++;
return !frame.empty() ? frame : null;
}
#Override
protected void finalize() throws Throwable {
super.finalize();
}
/**
* convert me to an observable
* #return a Mat emitting Observable
*/
public Observable<Mat> toObservable() {
// Resource creation.
Func0<VideoCapture> resourceFactory = () -> {
VideoCapture capture = new VideoCapture();
capture.open(source);
return capture;
};
// Convert to observable.
Func1<VideoCapture, Observable<Mat>> observableFactory = capture -> Observable
.<Mat> create(subscriber -> {
boolean hasNext = true;
while (hasNext) {
final Mat frame = this.fetch();
hasNext = frame!=null && frame.rows()>0 && frame.cols()>0;
if (hasNext) {
String msg = String.format("->%6d:%4dx%d", frameIndex, frame.cols(), frame.rows());
System.out.println(msg);
subscriber.onNext(frame);
}
}
subscriber.onCompleted();
});
// Disposal function.
Action1<VideoCapture> dispose = VideoCapture::release;
return Observable.using(resourceFactory, observableFactory, dispose);
}
}
ImageSubscriber
import org.opencv.core.Mat;
import rx.Subscriber;
public class ImageSubscriber extends Subscriber<Mat> {
public Throwable error;
public int cols = 0;
public int rows=0;
public int frameIndex=0;
public boolean completed = false;
public boolean debug = false;
#Override
public void onCompleted() {
completed = true;
}
#Override
public void onError(Throwable e) {
error = e;
}
#Override
public void onNext(Mat mat) {
cols = mat.cols();
rows = mat.rows();
frameIndex++;
if (cols==0 || rows==0)
System.err.println("invalid frame "+frameIndex);
if (debug) {
String msg = String.format("%6d:%4dx%d", frameIndex, cols, rows);
System.out.println(msg);
}
}
};

QR code screen popup issue with BlackBerry os 6

I am trying to scan QR code with my code. My code is running fine with 5.0(Bold) and 7.1(Torch) OS phones. It is running fine with 7.1 and 5.0. but giving problem while running with 6.0 OS(Bold 9700). The problem is - "While trying to scan QR code, app scans the QR code but camera screen doesn't pop and it remains at the front. Event it is not able to hide by using Esc key". please help me to resolve the issue with os6.
Edit:
Code while opening camera screen for QR code scan:
Hashtable hints = new Hashtable();
// The first thing going in is a list of formats. We could look for
// more than one at a time, but it's much slower.
Vector formats = new Vector();
formats.addElement(BarcodeFormat.QR_CODE);
hints.put(DecodeHintType.POSSIBLE_FORMATS, formats);
// We will also use the "TRY_HARDER" flag to make sure we get an
// accurate scan
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
// We create a new decoder using those hints
BarcodeDecoder decoder = new BarcodeDecoder(hints);
// Finally we can create the actual scanner with a decoder and a
// listener that will handle the data stored in the QR code. We put
// that in our view screen to handle the display.
try {
_scanner = new BarcodeScanner(decoder, new LeadQRcodeDecoderListener());
_QRcodeScreen = new LeadQRcodeScannerViewScreen(_scanner);
// If we get here, all the QR code scanning infrastructure should be set
// up, so all we have to do is start the scan and display the viewfinder
_scanner.startScan();
UiApplication.getUiApplication().pushScreen(_QRcodeScreen);
}
catch (Exception e) {
e.printStackTrace();
return;
}
code for closing screen is:
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
UiApplication.getUiApplication().popScreen(_QRcodeScreen);
}
});
I am calling this code after scanning of QR code.
This is a problem with OS6 in some devices that has been asked before on this site. Last one was two days ago:
Blackberry OS6 camera wont shut down after capture
AFAIK there's no API to close the camera app, so it has to be done with key injection hacks, that are tricky because they need accurate timing and as CPUs are different in some models, and also because the camera app has a different design in some OSes.
So either you use JSR135 and use a renamed Zxing package to provide a camera view contained in your app, or just follow your approach but instead of closing the camera app you just bring to foreground your own app.
I have solved my same issue for os 6. After scanning of QR code, close all player and scanner connection.
You can use-
if (_scanner != null && _scanner.getPlayer() != null) {
_scanner.getPlayer().close();
}
It is helpful to me.
This will definitely help you.
here is my code , it's working perfectly in OS 6.0 device 9830
/**
* First Invoke the QR Scanner
*/
ViewFinderScreen _viewFinderScreen =
new ViewFinderScreen(ShoopingCartScreen.this); // ShoopingCartScreen.this Current Screen Object
UiApplication.getUiApplication().pushScreen(_viewFinderScreen);
package com.application.qrScanner;
import java.util.Hashtable;
import java.util.Vector;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;
import net.rim.device.api.barcodelib.BarcodeDecoder;
import net.rim.device.api.barcodelib.BarcodeDecoderListener;
import net.rim.device.api.barcodelib.BarcodeScanner;
import net.rim.device.api.io.Base64InputStream;
import net.rim.device.api.io.http.HttpDateParser;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.MainScreen;
import com.application.data.ShoopingCartObj;
import com.application.global.Global;
import com.application.log.Log;
import com.application.main.MessageScreen;
import com.application.main.orderDetail.orderSection.InputPopUpScreen;
import com.application.main.shoopingSection.ShoopingCartScreen;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.DecodeHintType;
public class ViewFinderScreen extends MainScreen
{
private BarcodeScanner _scanner;
private short _frequency = 1046;
private short _duration = 200;
private int _volume = 100;
private VideoControl vc;
private ButtonField _btnCancel;
private ShoopingCartScreen _shoopingCartScreen;
/**
* Creates a new ViewFinderScreen object
*/
public ViewFinderScreen(ShoopingCartScreen _shoopingCartScreen)
{
this._shoopingCartScreen = _shoopingCartScreen;
_btnCancel = new ButtonField("Cancel" , ButtonField.USE_ALL_WIDTH)
{
protected boolean navigationClick(int status, int time)
{
fieldChangeNotify(1);
return true;
}
};
_btnCancel.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
stopScan();
UiApplication.getUiApplication().popScreen(ViewFinderScreen.this);
}
});
// Initialize Hashtable used to inform the scanner how to
// recognize the QR code format.
Hashtable hints = new Hashtable();
Vector formats = new Vector(1);
formats.addElement(BarcodeFormat.QR_CODE);
hints.put(DecodeHintType.POSSIBLE_FORMATS, formats);
// Initialize the BarcodeDecoder
BarcodeDecoder decoder = new BarcodeDecoder(hints);
// Create a custom instance of a BarcodeDecoderListener to pop the
// screen and display results when a QR code is recognized.
BarcodeDecoderListener decoderListener = new BarcodeDecoderListener()
{
/**
* #see BarcodeDecoderListener#barcodeDecoded(String)
*/
public void barcodeDecoded(String rawText)
{
try {
String encoded = rawText;
byte[] decoded = Base64InputStream.decode( encoded );
rawText = new String(decoded);
System.out.println( new String( decoded ) );
}
catch (Throwable t) {
System.out.println( "Unable to decode string: " + t.getMessage() );
}
displayMessage(rawText);
ViewFinderScreen.this. _shoopingCartScreen.beep();
}
};
try
{
// Initialize the BarcodeScanner object and add the associated
// view finder.
_scanner = new BarcodeScanner(decoder, decoderListener);
vc = _scanner.getVideoControl();
vc.setDisplayFullScreen(true);
add(_scanner.getViewfinder());
setStatus(_btnCancel);
}
catch(Exception e)
{
displayMessage("Initilize Scanner: " + e.getMessage());
}
startScan();
}
/**
* Informs the BarcodeScanner that it should begin scanning for QR Codes
*/
public void startScan()
{
try
{
_scanner.startScan();
}
catch(MediaException me)
{
displayMessage(" Start Scan Error: " + me.getMessage());
}
}
public void stopScan()
{
try
{
Player p = _scanner.getPlayer() ;
if(p != null)
{
p.stop();
p.deallocate();
p.close();
}
}
catch (Exception e)
{
MessageScreen.msgDialog("Exception in Stop Scanning "+e.toString());
}
}
/**
* Pops the ViewFinderScreen and displays text on the main screen
*
* #param text Text to display on the screen
*/
private void displayMessage(final String text)
{
Log.d("QR Code String ", text);
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
stopScan();
}
});
}
protected boolean keyDown(int keycode, int time)
{
if (Keypad.key(keycode) == Keypad.KEY_ESCAPE)
{
stopScan();
return true;
}
return super.keyDown(keycode, time);
}
}

App Error 104 uncaught : runtime exception while running blackberry application

I have created one application to get My location coordinates using GPS after deploying my code in simulator i am getting above error with explanation -
uncaught exception:pushmodalscreen called by a non-event thread
I am not able to figure out whats going wrong.
/**
* GPSDemo.java
*
* Copyright © 1998-2011 Research In Motion Ltd.
*
* Note: For the sake of simplicity, this sample application may not leverage
* resource bundles and resource strings. However, it is STRONGLY recommended
* that application developers make use of the localization features available
* within the BlackBerry development platform to ensure a seamless application
* experience across a variety of languages and geographies. For more information
* on localizing your application, please refer to the BlackBerry Java Development
* Environment Development Guide associated with this release.
*/
package com.gps;
import java.util.*;
import javax.microedition.location.*;
import net.rim.device.api.command.*;
import net.rim.device.api.gps.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.util.*;
import net.rim.blackberry.api.invoke.*;
import net.rim.blackberry.api.maps.*;
import net.rim.blackberry.api.menuitem.*;
/**
* This application acts as a simple travel computer, recording route
* coordinates, speed and altitude. Recording begins as soon as the
* application is invoked.
*/
public class GPSScreen extends UiApplication
{
// Represents the number of updates over which altitude is calculated, in seconds
private static final int GRADE_INTERVAL = 5;
private static final long ID = 0x5d459971bb15ae7aL;
// Represents period of the position query, in seconds
private static int _interval = 1;
private double _latitude;
private double _longitude;
private LocationProvider _locationProvider;
private GPSDemoScreen _screen;
private MapView _mapview = new MapView();
/**
* Entry point for application
*
* #param args Command line arguments (not used)
*/
public static void main(String[] args)
{
// Create a new instance of the application and make the currently
// running thread the application's event dispatch thread.
new GPSScreen().enterEventDispatcher();
}
/**
* Create a new GPSDemo object
*/
public GPSScreen()
{
_screen = new GPSDemoScreen();
_screen.setTitle("GPS Demo");
// Attempt to start the location listening thread
if(startLocationUpdate())
{
_screen.setState(_locationProvider.getState());
}
// Render the screen
pushScreen(_screen);
}
/**
* Invokes the Location API with Standalone criteria
*
* #return True if the <code>LocationProvider</code> was successfully started, false otherwise
*/
private boolean startLocationUpdate()
{
boolean returnValue = false;
if(!(GPSInfo.getDefaultGPSMode() == GPSInfo.GPS_MODE_NONE))
{
try
{
Criteria criteria = new Criteria();
criteria.setCostAllowed(false);
_locationProvider = LocationProvider.getInstance(criteria);
if(_locationProvider != null)
{
/*
* Only a single listener can be associated with a provider,
* and unsetting it involves the same call but with null.
* Therefore, there is no need to cache the listener
* instance request an update every second.
*/
_locationProvider.setLocationListener(new LocationListenerImpl(), _interval, -1, -1);
returnValue = true;
}
else
{
invokeLater(new Runnable()
{
public void run()
{
Dialog.alert("Failed to obtain a location provider, exiting...");
System.exit(0);
}
});
}
}
catch(final LocationException le)
{
invokeLater(new Runnable()
{
public void run()
{
Dialog.alert("Failed to instantiate LocationProvider object, exiting..." + le.toString());
System.exit(0);
}
});
}
}
else
{
invokeLater(new Runnable()
{
public void run()
{
Dialog.alert("GPS is not supported on this device, exiting...");
System.exit(0);
}
});
}
return true;
}
/**
* Implementation of the LocationListener interface. Listens for updates to
* the device location and displays the results.
*/
private class LocationListenerImpl implements LocationListener
{
/**
* #see javax.microedition.location.LocationListener#locationUpdated(LocationProvider,Location)
*/
public void locationUpdated(LocationProvider provider, Location location)
{
if(location.isValid())
{
_longitude = location.getQualifiedCoordinates().getLongitude();
_latitude = location.getQualifiedCoordinates().getLatitude();
_mapview.setZoom(Integer.parseInt("0.1"));
try
{
int latitude = (int) (100000 * _latitude);
int longitude = (int) (100000 * _longitude);
if (latitude > 9000000 || latitude < -9000000 || longitude >= 18000000 || longitude < -18000000)
{
throw new IllegalArgumentException ();
}
_mapview.setLatitude(latitude);
_mapview.setLongitude(longitude);
// Invoke BlackBerry Maps application with provided MapView object.
Invoke.invokeApplication(Invoke.APP_TYPE_MAPS, new MapsArguments(_mapview));
}
catch(RuntimeException re)
{
// An exception is thrown when any of the following occur :
// Latitude is invalid : Valid range: [-90, 90]
// Longitude is invalid : Valid range: [-180, 180)
// Minus sign between 2 numbers.
Dialog.alert("Temporary Unavailable Service");
}
}
}
/**
* #see javax.microedition.location.LocationListener#providerStateChanged(LocationProvider, int)
*/
public void providerStateChanged(LocationProvider provider, int newState)
{
if(newState == LocationProvider.TEMPORARILY_UNAVAILABLE)
{
provider.reset();
}
_screen.setState(newState);
}
}
/**
* The main screen to display the current GPS information
*/
private final class GPSDemoScreen extends MainScreen
{
TextField _statusTextField;
/**
* Create a new GPSDemoScreen object
*/
GPSDemoScreen()
{
// Initialize UI
_statusTextField = new TextField(Field.NON_FOCUSABLE);
}
/**
* Display the state of the GPS service
*
* #param newState The state to display
*/
public void setState(final int newState)
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
/**
* #see java.lang.Runnable#run()
*/
public void run()
{
switch(newState)
{
case LocationProvider.AVAILABLE:
_statusTextField.setText("Available");
break;
case LocationProvider.OUT_OF_SERVICE:
_statusTextField.setText("Out of Service");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
_statusTextField.setText("Temporarily Unavailable");
break;
}
}
});
}
/**
* #see net.rim.device.api.ui.Screen#close()
*/
public void close()
{
if(_locationProvider != null)
{
_locationProvider.reset();
_locationProvider.setLocationListener(null, -1, -1, -1);
}
super.close();
}
}
}
I've seen that error when Dialog.alert() is used outside of the event thread. Looking at your code, I see LocationListenerImpl.locationUpdated assumes it is running on the event thread. If it is not, the UI update code would throw an exception, and then your exception handler will try to display a dialog, and that will fail as well.

How to play an audio file one after another in Blackberry?

I managed to create an application in Blackberry that plays audio file, but it will only plays one file. I try to use a for loop to play a few audio files.
I managed to play it, but it did not play the whole sound of the audio files, it just play the first audio files and second for a few seconds, and stop playing after that. The files that played also play the sound overlap each other which should not be happening.
How to play the full sound of the audio files one after another in Blackberry without stopping?
Here is my code for the application that I created with the for loop:
package mypackage;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import java.lang.Class;
import javax.microedition.rms.RecordStore;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import net.rim.device.api.media.protocol.ByteArrayInputStreamDataSource;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.extension.container.*;
import net.rim.device.api.ui.UiApplication;
import java.io.IOException;
public class PlayMedia extends UiApplication{
/**
* Entry point for application
* #param args Command line arguments (not used)
*/
public static void main(String[] args){
PlayMedia theApp = new PlayMedia();
theApp.enterEventDispatcher();
}
public PlayMedia()
{
pushScreen(new PlayMediaScreen());
}
/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
final class PlayMediaScreen extends MainScreen
{
/**
* Creates a new PlayMediaScreen object
*/
PlayMediaScreen()
{
String test1 = "Test(2seconds).mp3";
String test2 = "Test(2seconds)2.mp3";
String test3 = "Test(2seconds)3.mp3";
String test4 = "Test(2seconds)4.mp3";
String test5 = "blind_willie.mp3";
String mp3 = null;
for(int i=0;i<5;i++){
if(i == 0){
mp3 = test1;
}
else if(i == 1){
mp3 = test2;
}
else if(i == 2){
mp3 = test3;
}
else if(i == 3){
mp3 = test4;
}
else if(i == 4){
mp3 = test5;
}
play(mp3);
}
}
private void play(String mp3){
Player p = null;
InputStream stream = (InputStream)this.getClass().getResourceAsStream("/" + mp3);
try {
//p = Manager.createPlayer(source);
p = Manager.createPlayer(stream, "audio/mpeg");
p.realize();
p.prefetch();
//testing
System.out.println("Creating Players!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
System.out.println("The mp3 is " + mp3 + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//testing
System.out.println("IO Exeception!!!!!!!!!!!!!!!!1 " + e);
//testing
System.out.println(p);
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//testing
System.out.println("Media Exeception!!!!!!!!!!!!!!!!1" + e);
//testing
System.out.println(p);
}
/*
* Best practice is to invoke realize(), then prefetch(), then start().
* Following this sequence reduces delays in starting media playback.
*
* Invoking start() as shown below will cause start() to invoke prefetch(0),
* which invokes realize() before media playback is started.
*/
try {
p.start();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//testing
System.out.println("Media Exeception for starting!!!!!!!!!!!!!!!!1" + e);
//testing
System.out.println(p);
}
/*try {
p.stop();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
//p.deallocate();
//p.close();
}
}
}
I managed to play it, but it did not play the whole sound of the audio files, it just play the first audio files and second for a few seconds, and stop playing after that. The files that played also play the sound overlap each other which should not be happening.
Please reread the BB API docs for Player carefully:
Simple Playback
A Player can be created from one of the Manager's createPlayer methods. After the Player is created, calling start will start the playback as soon as possible. The method will return when the playback is started. The playback will continue in the background and will stop automatically when the end of media is reached.
Simple playback example illustrates this:
try {
Player p = Manager.createPlayer("http://abc.wav");
p.start();
} catch (MediaException pe) {
} catch (IOException ioe) {
}
Note the docs say The method will return when the playback is started. The playback will continue in the background ... This is the reason of you get "sound overlap".
To overcome this you need to attach a listener to the player Player.addPlayerListener(PlayerListener playerListener). The listener will be notified from the background "playback" thread when the media file has been played to the end. And this will be the right moment to start a new playback for the next media file. Please don't expect the code from me, I'm just giving you an idea.
Finally I get it, this is my code. :D
package mypackage;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import java.lang.Class;
import javax.microedition.rms.RecordStore;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import net.rim.device.api.media.protocol.ByteArrayInputStreamDataSource;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.extension.container.*;
import net.rim.device.api.ui.UiApplication;
import java.io.IOException;
public class PlayMedia extends UiApplication{
/**
* Entry point for application
* #param args Command line arguments (not used)
*/
public static void main(String[] args){
PlayMedia theApp = new PlayMedia();
theApp.enterEventDispatcher();
}
public PlayMedia()
{
pushScreen(new PlayMediaScreen());
}
/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
final class PlayMediaScreen extends MainScreen implements PlayerListener
{
/**
* Creates a new PlayMediaScreen object
*/
Player p = null;
String mp3 = "";
String test1 = "Test2seconds.mp3";
String test5 = "Test2seconds2.mp3";
//String test6 = "Test2seconds3.mp3";
String test4 = "Test2seconds4.mp3";
String test2 = "blind_willie.mp3";
String test3 = "blind_willie2.mp3";
PlayMediaScreen()
{
mp3 = test1;
play(mp3);
}
private void play(String mp3){
InputStream stream = (InputStream)this.getClass().getResourceAsStream("/" + mp3);
try {
//p = Manager.createPlayer(source);
p = Manager.createPlayer(stream,"audio/mpeg");
p.addPlayerListener(this);
p.realize();
p.prefetch();
//testing
System.out.println("Creating Players!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
System.out.println("The mp3 is " + mp3 + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//testing
System.out.println("IO Exeception!!!!!!!!!!!!!!!!1 " + e);
//testing
System.out.println(p);
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//testing
System.out.println("Media Exeception!!!!!!!!!!!!!!!!1" + e);
//testing
System.out.println(p);
}
/*
* Best practice is to invoke realize(), then prefetch(), then start().
* Following this sequence reduces delays in starting media playback.
*
* Invoking start() as shown below will cause start() to invoke prefetch(0),
* which invokes realize() before media playback is started.
*/
try {
p.start();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//testing
System.out.println("Media Exeception for starting!!!!!!!!!!!!!!!!1" + e);
//testing
System.out.println(p);
}
/*
try {
p.stop();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
p.deallocate();
p.close();
*/
}
public void playerUpdate(Player player, String event, Object eventData) {
// TODO Auto-generated method stub
if (event.equals(PlayerListener.END_OF_MEDIA)) {
//String mp3 = "";
if( mp3.equals(test1) ) {
mp3 = test2;
//testing
System.out.println("The MP3 is " + mp3 + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
play(mp3);
//testing
System.out.println("The MP3 FINISH PLAYING is " + mp3 + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
else if( mp3.equals(test2) ){
mp3 = test3;
//testing
System.out.println("The MP3 is " + mp3 + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
play(mp3);
//testing
System.out.println("The MP3 FINISH PLAYING is " + mp3 + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
else if( mp3.equals(test3) ) {
mp3 = test4;
//testing
System.out.println("The MP3 is " + mp3 + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
play(mp3);
//testing
System.out.println("The MP3 FINISH PLAYING is " + mp3 + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
else if( mp3.equals(test4) ) {
mp3 = test5;
//testing
System.out.println("The MP3 is " + mp3 + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
play(mp3);
//testing
System.out.println("The MP3 FINISH PLAYING is " + mp3 + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
else if( mp3.equals(test5) ){
mp3 = null;
try {
player.stop();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
player.deallocate();
player.close();
}
}
}
}
}

How to continuous play audio files in Blackberry?

I wanted to create a Blackberry application that plays a few audio files continuous one after another. But the application I created can open all the audio files continuously but did not managed to play the whole audio files. Can anyone know a solution for this? Below is my codes:
package mypackage;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import java.lang.Class;
import javax.microedition.rms.RecordStore;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import net.rim.device.api.media.protocol.ByteArrayInputStreamDataSource;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.extension.container.*;
import net.rim.device.api.ui.UiApplication;
import java.io.IOException;
public class PlayMedia extends UiApplication{
/**
* Entry point for application
* #param args Command line arguments (not used)
*/
public static void main(String[] args){
PlayMedia theApp = new PlayMedia();
theApp.enterEventDispatcher();
}
public PlayMedia()
{
pushScreen(new PlayMediaScreen());
}
/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
final class PlayMediaScreen extends MainScreen
{
/**
* Creates a new PlayMediaScreen object
*/
PlayMediaScreen()
{
String test1 = "Test(2seconds).mp3";
String test2 = "Test(2seconds)2.mp3";
String test3 = "Test(2seconds)3.mp3";
String test4 = "Test(2seconds)4.mp3";
String test5 = "blind_willie.mp3";
String mp3 = null;
for(int i=0;i<5;i++){
if(i == 0){
mp3 = test1;
}
else if(i == 1){
mp3 = test2;
}
else if(i == 2){
mp3 = test3;
}
else if(i == 3){
mp3 = test4;
}
else if(i == 4){
mp3 = test5;
}
play(mp3);
}
}
private void play(String mp3){
Player p = null;
InputStream stream = (InputStream)this.getClass().getResourceAsStream("/" + mp3);
try {
//p = Manager.createPlayer(source);
p = Manager.createPlayer(stream, "audio/mpeg");
p.realize();
p.prefetch();
//testing
System.out.println("Creating Players!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
System.out.println("The mp3 is " + mp3 + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//testing
System.out.println("IO Exeception!!!!!!!!!!!!!!!!1 " + e);
//testing
System.out.println(p);
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//testing
System.out.println("Media Exeception!!!!!!!!!!!!!!!!1" + e);
//testing
System.out.println(p);
}
/*
* Best practice is to invoke realize(), then prefetch(), then start().
* Following this sequence reduces delays in starting media playback.
*
* Invoking start() as shown below will cause start() to invoke prefetch(0),
* which invokes realize() before media playback is started.
*/
try {
p.start();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//testing
System.out.println("Media Exeception for starting!!!!!!!!!!!!!!!!1" + e);
//testing
System.out.println(p);
}
try {
p.stop();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
p.deallocate();
p.close();
}
}
}
You can save the files that you want to play in a Vector for example, and make a queue that will start by playing the first one and when it finishes it will start by the second.
So in ur vector you will have the test1, test2, test3, ... and you will play the test1. When this finished you will start by test2 ...
Vector v = new Vector();
v.addElement("Test(2seconds).mp3");
v.addElement("Test(2seconds)2.mp3");
v.addElement("Test(2seconds)3.mp3");
for (int i = 0 ; i < v.size() ; i ++) {
String address = (String) v.elementAt(i);
//play here the audio file
//You can use here thread.wait() and Thread.notify to handle the event of beggining of an audio file and the ending of it. Do not play them all in the same time. You have to wait for the audio file to end before playing the other one
}

Resources