Aim:
Play video in blackberry device from remote server.
Current Output
Blank screen and this message: "Press space to start/stop/resume playback."
My code
public class MyApp extends UiApplication
{
private Player player;
private VideoControl videoControl;
public static void main(String[] args)
{
MyApp theApp = new MyApp();
theApp.enterEventDispatcher();
}
public MyApp()
{
MainScreen ms = new MainScreen();
public boolean onClose()
{
player.close();
videoControl.setVisible(false);
close();
return true;
}
protected boolean keyChar(char c, int status, int time)
{
boolean retVal = false;
if (c == Characters.SPACE)
{
if (player.getState() == Player.STARTED)
{
//Stop playback.
try
{
player.stop();
}
catch (Exception ex)
{
System.out.println("Exception: " + ex.toString());
}
}
else
{
//Start playback.
try
{
player.start();
}
catch (Exception ex)
{
System.out.println("Exception: " + ex.toString());
}
}
retVal = true;
}
return retVal;
}
};
ms.setTitle(new LabelField("Let's play some video..."));
LabelField lf = new LabelField("Press space to start/stop/resume playback.");
ms.add(lf);
pushScreen(ms);
try
{
player = Manager.createPlayer("http://224.1.2.3:12344:8082/ACSATraffic/blackberry.3gp");
player.realize();
//Create a new VideoControl.
videoControl = (VideoControl)player.getControl("VideoControl");
//Initialize the video mode using a Field.
videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
videoControl.setVisible(true);
}
catch (Exception ex)
{
System.out.println(ex.toString());
}
}
}
}
What am I doing wrong here?
Or else is there any sample for playing video from local and
server? A link would be greatly appreciated.
Related
I am trying to run the music that I have, but it does not work. There is no error showing up in the eclipse any more, but the sound is not played. This is my code that I have
public class Music extends Thread {
private Player player;
private boolean isLoop;
private File file;
private FileInputStream fis;
private BufferedInputStream bis;
public Music(String name, boolean isLoop)
{
try {
this.isLoop = isLoop;
//Find the link to the file and play, save the file to the buffer
file = new File(Main.class.getResource("/music/" + name).toURI());
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
player = new Player(bis);
} catch(Exception e){
System.out.println("No music");
}
}
//Get the time of the music, how long it is played
public int getTime() {
if(player == null)
{
return 0;
}
return player.getPosition();
}
//Stop the music played
public void close() {
isLoop = false;
player.close();
this.interrupt();
}
#Override
public void run() {
try {
player.play();
do {
player.play();
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
player = new Player(bis);
} while(isLoop);
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
Also this my setting I have
Thank you for the help
Your problem starts here -> file = new File(Main.class.getResource("/music/" + name).toURI());
An embedded resource can't be reference as a File, because, well, it's not. Instead use Class#getResourceAsStream directly.
Next, you should avoid extending from Thread, thread's are not re-entrant, that is, once stopped, you can't restart them. Instead, implement Runnable, for example...
public class Music implements Runnable {
private String name;
private Player player;
private boolean isLoop;
private Thread playerThread;
public Music(String name, boolean isLoop) {
this.isLoop = isLoop;
this.name = name;
}
//Get the time of the music, how long it is played
public int getTime() {
if (player == null) {
return 0;
}
return player.getPosition();
}
//Stop the music played
public void stop() {
isLoop = false;
if (player == null) {
return;
}
player.close();
playerThread = null;
}
public void waitFor() throws InterruptedException {
if (playerThread == null) {
return;
}
playerThread.join();
}
public void play() throws InterruptedException {
if (playerThread != null) {
stop();
waitFor();
}
playerThread = new Thread(this);
playerThread.start();
}
protected void playAudio() throws IOException, JavaLayerException {
try (BufferedInputStream bis = new BufferedInputStream(getClass().getResourceAsStream("/music/" + name))) {
player = new Player(bis);
player.play();
}
}
#Override
public void run() {
try {
do {
System.out.println("Start playing");
playAudio();
System.out.println("All done");
} while (isLoop);
} catch (Exception e) {
e.printStackTrace();
}
stop();
}
}
I am trying to read GPS data from a serial port(ttyACM0) in java using jssc jar which I need to display as a label in a JavaFx application. I have a created a thread for reading the GPS data but this thread is consuming 100% CPU(checked using top command + Shift H) because of which my GUI is getting freezed. This is a sample code I have written for reading GPS data
Serial Interface
import jssc.*;
public class SerInterface {
String portName;
int baud;
boolean lineMode;
// The chosen Port itself
SerialPort port;
public byte[] recvBuff;
public SerInterface(String portName, int baud, boolean lineMode) {
this.portName = portName;
this.baud = baud;
this.lineMode = lineMode;
recvBuff = new byte[8192];
openPort();
if((port == null) || (!port.isOpened()))
System.out.println(portName + " not opened successfully");
}
void openPort() {
port = new SerialPort(portName);
if(port == null)
return;
try {
port.openPort();
} catch (SerialPortException e) {
e.printStackTrace();
}
try {
if(port.isOpened())
port.setParams(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
else
return;
} catch (SerialPortException e) {
e.printStackTrace();
}
System.out.println(portName + " opened successfully");
}
public int recvData() {
int len = 0;
if(port.isOpened()) {
try {
byte[] buff = port.readBytes();
if((buff == null) || (buff.length == 0))
return 0;
len = buff.length;
if(len > recvBuff.length)
len = recvBuff.length;
System.arraycopy(buff, 0, recvBuff, 0, len);
buff = null;
} catch (SerialPortException e) {
e.printStackTrace();
}
}
return len;
}
public void sendData(byte[] sendBuff, int len) {
if(port.isOpened()) {
try {
port.writeBytes(sendBuff);
} catch (SerialPortException e) {
e.printStackTrace();
}
}
}
public boolean validatePort() {
if(!port.isOpened()) {
openPort();
if(port.isOpened())
return true;
else
return false;
}
else
return true;
}
public void flushPort() {
if(port.isOpened()) {
try {
port.purgePort(SerialPort.PURGE_RXCLEAR | SerialPort.PURGE_TXCLEAR);
} catch (SerialPortException e) {
e.printStackTrace();
}
}
}
public void closePort() {
if(port.isOpened()) {
try {
port.closePort();
System.out.println("port closed");
} catch (SerialPortException e) {
e.printStackTrace();
}
}
}
}
GPSReceiver thread extends SerInterface and implements Runnable
#Override
public void run() {
flushPort();
while(true) {
if(!validatePort()) {
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue;
}
int dataLength = recvData();
if(dataLength < 2)
continue;
try {
InputStream gpsStream = new ByteArrayInputStream(recvBuff, 0, dataLength);
BufferedReader br = new BufferedReader(new InputStreamReader(gpsStream, StandardCharsets.US_ASCII));
while(true) {
try {
if (!((output = br.readLine()) != null)) break;
} catch (IOException e) {
e.printStackTrace();
}
extractData();
}
}
catch(NullPointerException e) {
e.printStackTrace();
}
}
Main Thread
GPSInterface objGPSThreadInterface = new GPSInterface(GPSPortName, 9600, true);
GPSThreadInt = new Thread(objGPSThreadInterface, "GPSINT");
I am using jdk-13.0.1 and jssc-2.9.1 jar
I have two programs for my project. I would like to run program1 via program2 as a thread. I tried extending the Thread class in program1 but, I am getting whole lot of errors:
public void main(String[] args) {
Void is an invalid type for the variable main.
private static void start(Result result) {
Void is an invalid type for the variable start.
Program 1:
public class HelloWorld extends Thread {
private String[] args;
public HelloWorld(String[] args){
this.args = args;
}
int i=1;
String resultText;
try {
URL url;
if (args.length > 0) {
url = new File(args[0]).toURI().toURL();
}
else {
url = HelloWorld.class.getResource("helloworld.config.xml");
}
ConfigurationManager cm = new ConfigurationManager(url);
Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
Microphone microphone = (Microphone) cm.lookup("microphone");
recognizer.allocate();
if (microphone.startRecording()) {
while (true) {
System.out.println("Start speaking. Press Ctrl-C to quit.\n");
Result result = recognizer.recognize();
}
}
else {
System.out.println("Cannot start microphone.");
recognizer.deallocate();
System.exit(1);
}
}
catch (IOException e) {
System.err.println("Problem when loading HelloWorld: " + e);
e.printStackTrace();
}
}
private static void start_recognition(Result result) {
if (result != null)
{
resultText = result.getBestFinalResultNoFiller();
System.out.println("You said: " + resultText + "\n");
if(resultText.equalsIgnoreCase("Command Prompt"))
{
try{
Runtime.getRuntime().exec("cmd /c start cmd");
}
catch(Exception er){
}
}
}
}
}
}
Program 2:
public class App {
public static void main(String[] args) {
HelloWorld obj = new HelloWorld(args);
obj.start();
}
}
How can I run program1 as a thread via program2?
Update of code after suggestions:
public class HelloWorld extends Thread{
public void run() {
int i=1;
String resultText;
try {
URL url;
if (args.length > 0) { // Getting error in this line
args cannot be resolved to a variable.
url = new File(args[0]).toURI().toURL(); // And the same error in this line
}
else {
url = HelloWorld.class.getResource("helloworld.config.xml");
}
ConfigurationManager cm = new ConfigurationManager(url);
Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
Microphone microphone = (Microphone) cm.lookup("microphone");
recognizer.allocate();
if (microphone.startRecording()) {
while (true) {
System.out.println("Start speaking. Press Ctrl-C to quit.\n");
Result result = recognizer.recognize();
start_recognition(result);
}
}
else {
System.out.println("Cannot start microphone.");
recognizer.deallocate();
System.exit(1);
}
}
catch (IOException e) {
System.err.println("Problem when loading HelloWorld: " + e);
e.printStackTrace();
}
}
private void start_recognition(Result result) {
{
if (result != null)
{
String resultText = result.getBestFinalResultNoFiller();
System.out.println("You said: " + resultText + "\n");
if(resultText.equalsIgnoreCase("Command Prompt"))
{
try{
Runtime.getRuntime().exec("cmd /c start cmd");
}
catch(Exception er){
}
}
}
}
}
}
In your class HelloWorld, get rid of public void main(String[] args)
It should look like this:
public class HelloWorld extends Thread {
public void run() {
int i=1;
String resultText;
try {
URL url;
if (args.length > 0) {
url = new File(args[0]).toURI().toURL();
}
else {
url = HelloWorld.class.getResource("helloworld.config.xml");
}
For further information, please refer to this link: Java Class
I'm trying to write a program that plays .mp3 files, playing them works perfectly. But pausing is causing some trouble. When I pause the song (stop the song and ask for the frame) I get an incorrect value.
class PauseStartMP3Test {
private static AdvancedPlayer player;
private static int pausedOnFrame = 0;
private static boolean playing = false;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(true) {
String s = in.next();
if(!s.equals("")) {
if(!playing) {
playing = true;
play();
}
else {
player.stop();
}
}
}
}
public static void play() {
File file = null;
file = new File("C:\\Users\\Remco\\Desktop\\Programming\\musictest/throughglass.mp3");
try {
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
try {
player = new AdvancedPlayer(bis);
new Thread() {
public void run() {
try {
if(playing) {
player.setPlayBackListener(new PlaybackListener() {
#Override
public void playbackFinished(PlaybackEvent event) {
pausedOnFrame = event.getFrame();
System.out.println(pausedOnFrame);
playing = false;
}
});
player.play(pausedOnFrame, Integer.MAX_VALUE);
}
else {
player.stop();
}
}
catch (Exception e) {
System.out.println(e);
}
}
}.start();
} catch (JavaLayerException ex) {
System.out.println(ex);
}
} catch (FileNotFoundException ex) {
System.out.println(ex);
}
}
I want to capture an image using an available webcam. I have successfully accessed the webcam but I couldn't stop the webcam process. I want to stop the webcam process using a stop button. How can I accomplish this? This my code:
public Component componen() throws IOException , NoPlayerException, CannotRealizeException
{
Component comp_video;
MediaLocator loo = new MediaLocator("vfw://0");
try {
broadcast = Manager.createRealizedPlayer(loo);
} catch (IOException ex) {
Logger.getLogger(CapturImage.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoPlayerException ex) {
Logger.getLogger(CapturImage.class.getName()).log(Level.SEVERE, null, ex);
} catch (CannotRealizeException ex) {
Logger.getLogger(CapturImage.class.getName()).log(Level.SEVERE, null, ex);
}
broadcast.start();
if((comp_video = broadcast.getVisualComponent()) != null)
{
comp_video.setSize(321,228);
return comp_video;
}
else
{
return null;
}
}
public void capture_image()
{
FrameGrabbingControl grab = (FrameGrabbingControl) broadcast.getControl("javax.media.control.FrameGrabbingControl");
javax.media.Buffer buff = grab.grabFrame();
BufferToImage buffer =new BufferToImage((VideoFormat) buff.getFormat());
img = buffer.createImage(buff);
}
public void set_iamge_label(final JLabel lb)
{
Thread web = new Thread(){
public void run(){
capture_image();
Rectangle rect = lb.getBounds();
Image img1 = img.getScaledInstance(rect.width,rect.height,Image.SCALE_DEFAULT);
lb.setIcon(new javax.swing.ImageIcon(img1));
}
};
web.start();
}