Java replacing JMF with an mp4 media player - java

this is a really newbie-ish question. So any information I have missed please tell me and I will add it.
I really have no idea about java, I received a project from my predecessor at work.
It actually has the classic media player on that doesn't support mp4, the volume of the videos is large so I wanted to change it and make it able to play .mp4
I have downloaded the openjfx but I am not really sure on how to implement that on the existing code I could really use some help/guidelines because I am kind of at a loss.
Here is an images of the player and the preview buttons. Also this is the code he's using (I think) for the media player.
private void previewClip(String path) {
try {
URL pathToUrl = new File(path).toURI().toURL(); // Manager and player recognize urls not paths or strings
Manager.setHint( Manager.LIGHTWEIGHT_RENDERER, true ); // the creation of the player manager
if(isPreviewActive) { // check if a video is already added to jPanel22
previewPanel.removeAll();
// previewPanel.remove(0); // removing previous video
}
Player previewPlayer = Manager.createRealizedPlayer(pathToUrl); // creation of the player
Component video = previewPlayer.getVisualComponent(); // adding visual (drawing) of the video
previewPanel.add(video, BorderLayout.CENTER); // adding video to jPanel22
pack(); // packing the assembly
previewPlayer.start(); // start the video
isPreviewActive = true; // change the check variable from 0 to 1
} catch (IOException | NoPlayerException | CannotRealizeException ex) {
showMessage("There is an error with preview Video.\nPlease contact support team\n" + ex.toString());
// Logger.getLogger(Chroma_boilerplate.class.getName()).log(Level.SEVERE, null, ex);
}
}

Related

How to play several audio clips (not at the same time) in an applet?

I need help playing music in my java code. Right now I'm using the mouseClicked method in my JApplet, and each time I click on a certain location music, from a wav file is supposed to play. But when I run the applet, only the first object I click on will play, after nothing else will play and I cannot exit the applet (I have to force quit)
I have already tried using the clip.stop() method after it has played, and after using that no sound plays at all.
this is my code in my class for playing music that's attached to my JApplet.
public void playMusic()
{
try{
File file = new File("island_music_x.wav");
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(file));
clip.start();
Thread.sleep(clip.getMicrosecondLength());
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
this is code for an object that is supposed to be clicked in my applet to play music.
xpos5 = me.getX();
ypos5 = me.getY();
if
(xpos5 > rect5xco && xpos5 < rect5xco+rect5width && ypos5 >
rect5yco && ypos5 < rect5yco+rect5height)
rect5Clicked = true;
else
rect5Clicked = false;
when it is set to true I have code to play the music
if
(rect5Clicked == true)
{
try{
m5.playMusic5();}
catch (Exception e)
{}
}
When I click on an object, music is supposed to play, which occurs, however; I should be able to click on other objects after the first one and play music as well. But, the music only plays the first time. I am also unable to exit the appletviewer.
Any help would be great thank you!!

Android camera2 preview image disorder when saved using ImageReader

I am taking a series of pictures using Android Camera2 API for real time pose estimation and environment reconstruction (the SLAM problem). Currently I simply save all of these pictures in my SD card for off-line processing.
I setup the processing pipeline according to google's Camera2Basic using a TextureView as well as an ImageReader, where they are both set as target surfaces for a repeat preview request.
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mIsShooting){
try {
mCaptureSession.stopRepeating();
mPreviewRequestBuilder.removeTarget(mImageReader.getSurface());
mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler);
mIsShooting = false;
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
else{
try {
mCaptureSession.stopRepeating();
mPreviewRequestBuilder.addTarget(mImageReader.getSurface());
mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler);
mIsShooting = true;
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
}
});
The ImageReader is added/removed when pressing the button. The ImageReader's OnImageAvailableListener is implemented as follow:
private ImageReader.OnImageAvailableListener mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() {
#Override
public void onImageAvailable(ImageReader reader) {
Image img = reader.acquireLatestImage();
if(null == img){
return;
}
if(img.getTimestamp() <= mLatestFrameTime){
Log.i(Tag, "disorder detected!");
return;
}
mLatestFrameTime = img.getTimestamp();
ImageSaver saver = new ImageSaver(img, img.getTimestamp());
saver.run();
}
};
I use acquireLatestImage (with buffer size set to 2) to discard old frames and have also checked the image's timestamp to make sure they are monotonously increasing.
The reader does receive images at an acceptable rate (about 25fps). However a closer look at the saved image sequence show they are not
always saved in chronological order.
The following pictures come from a long sequence shot by the program (sorry for not being able to post pictures directly :( ):
Image 1:
Image 2:
Image 3:
Such disorder does not occur very often but they can occur any time and seems not to be an initialization problem. I suppose it has something to do with the ImageReader's buffer size as with larger buffer less "flash backs" are occurred. Does anyone have the same problem?
I finally find that such disorder disappears when setting ImageReader's format to be YUV_420_888 in its constructor. Originally I set this field as JPEG.
Using JPEG format incurs not only large processing delay but also disorder. I guess the conversion from image sensor data to desired format utilizes other hardware such as DSP or GPU which does not guarantee chronological order.
Are you using TEMPLATE_STILL_CAPTURE for the capture requests when you enable the ImageReader, or just TEMPLATE_PREVIEW? What devices are you seeing issues with?
If you're using STILL_CAPTURE, make sure you check if the device supports the ENABLE_ZSL flag, and set it to false. When it is set to true (generally the default on devices that support it, for the STILL_CAPTURE template), images may be returned out of order since there's a zero-shutter-lag queue in place within the camera device.

Two Object of media Player not play song in Sync at same time

I have working on to one android application.Normal concept of this application is to Play Song in Multiple Device at Same Time.Everything is done but Song is not play in Sync.I have done lot's of R&d to solve this problem but not getting success.but at end I have found one Issue of Android Media Player.For finding this issue I have making one demo Application.Description is follow.
Demo App Description
I create a two object of Android media player firstPlayer and secondPlayer.firstPlayer Song playing will be start once App is Lunch.now i have taking one Button in test.xml.once the User will click in this button secondPlayer is start and play same music which I set in firstPlayer.and I also Seek secondPlayer at Position where firstPlayer is Play.
Code
changePlayer = (Button) findViewById(R.id.changePlayer);
String fileName = "/qq.mp3";
String baseDir = Environment.getExternalStorageDirectory() + "/Test App" + fileName;
Log.e(TAG, "path :" + baseDir);
try {
firstPlayer = new MediaPlayer();
secondPlayer = new MediaPlayer();
firstPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
secondPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
firstPlayer.setDataSource(baseDir);
secondPlayer.setDataSource(baseDir);
firstPlayer.prepare();
secondPlayer.prepare();
firstPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
changePlayer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!secondPlayer.isPlaying()) {
Log.e(TAG, "Media Player Start M2");
secondPlayer.start();
}
int sek = firstPlayer.getCurrentPosition();
secondPlayer.seekTo(firstPlayer.getCurrentPosition());
//firstPlayer.seekTo(sek);
Log.e(TAG, "Current Playe Time ::" + secondPlayer.getCurrentPosition());
Log.e(TAG, "First Playe Time ::" + sek);
Log.e(TAG, "firstPlayer Playe Time ::" + firstPlayer.getCurrentPosition());
Log.e(TAG, "secondPlayer Playe Time ::" + secondPlayer.getCurrentPosition());
}
});
Problem
I am getting one Strange problem both media player not play in Sync it's having little bit different for play song.if I seek secondPlayer where firstPlayer is play then it's playing in Sync but it's not happend both player playing different.
Note
I also try to Solve this Issue using any third party Media player but i didn't get luck.I have implement ExoPlayer,VlcPlayer,UniversalPlayer to solve this problem but It's also having same problem.
Requirement.
I want to play song in Sync in Two different object of MediaPlayer.
Did anyone know about any powerful media player which is not having this delay issue.
I hardly Try to solve this Sync issue but i Didn't get Luck.I want 100% Sync in My Application.Just Like Seedio app in IOS.I also test this in IOS but in IOS it's Working fine Both Player play song with 100% sync.
Please Help me if Anyone have any Idea.
I hope you all are clear with my problem.
Thank You
The Seek does take some time on a MediaPlayer. (As I understand the official docs: https://developer.android.com/reference/android/media/MediaPlayer)
One idea is to have the second stream not started, seek to a known future position (higher than currect pos of first stream), hope it gets there "in time". And then start the second stream when you get there (time has passed). I have not tested it, just been thinking about it as I want to solve a very similar issue.

Adding Video to Desktop App

I'm working on a project for a programming class, and I want to add a video file. I've tried using JMF, except when I try to play a video (using one of the formats listed on the Java website), it gives me the error:
Unable to handle format: XVID, 320x213, FrameRate=29.9, Length=204480 0 extra bytes
Unable to handle format: mpeglayer3, 22050.0 Hz, 0-bit, Stereo, Unsigned, 8000.0 framerate,FrameSize=4608 bits
Failed to realize: com.sun.media.PlaybackEngine#39b27b
Error: Unable to realize com.sun.media.PlaybackEngine#39b27b
Could not realize media player
This is the code I use to test the player:
public MediaPanel(String path) {
try {
videoPath = new File(path).toURI().toURL();
} catch (MalformedURLException e) {
e.printStackTrace();
}
setLayout(new BorderLayout());
Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);
try {
Player mediaPlayer = Manager.createRealizedPlayer(videoPath);
Component video = mediaPlayer.getVisualComponent();
Component controls = mediaPlayer.getControlPanelComponent();
if (video != null)
add(video, BorderLayout.CENTER);
if (controls != null)
add(controls, BorderLayout.SOUTH);
mediaPlayer.start();
} catch (NoPlayerException NoPlayerException) {
System.err.println("No media player found");
} catch (CannotRealizeException CannotRealizeException) {
System.err.println("Could not realize media player");
} catch (IOException IOException) {
System.err.println("Error reading from the source");
}
}
MediaPanel player = new MediaPanel("intro.avi");
I assume my two options are to make a video with the exact specifications listed on the Java website (because apparently just the file extension isn't enough), or use another method to display video.
How can I fix this? Do I need to make a video exactly as the specifications note (see link at bottom)?
NOTE: The class I'm in uses and older version of java that DOESN'T contain javaFX. Please don't recommend this unless there is a way I can add it without having the school reinstall a newer version of Java.

J2ME - using javax.microedition.amms.control.camera.CameraControl; is it possible to disable shutter sound?

In my Blackberry app I've implemented the camera and would like to replace the default shutter sound with my own. I figured I could do this by silencing the default camera sound by using the method enableShutterFeedback(false) then playing my own sound, or playing my sound immediately before the camera is activated.
private void initializeCamera()
{
try
{
// Create a player for the Blackberry's camera
Player player = Manager.createPlayer( "capture://video" );
// Set the player to the REALIZED state (see Player javadoc)
player.realize();
// Grab the video control and set it to the current display
_videoControl = (VideoControl)player.getControl( "VideoControl" );
if (_videoControl != null)
{
// Create the video field as a GUI primitive (as opposed to a
// direct video, which can only be used on platforms with
// LCDUI support.)
_videoField = (Field) _videoControl.initDisplayMode (VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
_videoControl.setDisplayFullScreen(true);
_videoControl.setVisible(false);
}
cc = (CameraControl)player.getControl("CameraControl");
cc.enableShutterFeedback(false);
// Set the player to the STARTED state (see Player javadoc)
player.start();
}
catch(Exception e)
{
MyApp.errorDialog("ERROR " + e.getClass() + ": " + e.getMessage());
}
}
This results in a Null pointer exception but can't figure out what's causing it, the camera's video doesn't get displayed. If I remove the CameraControl code in bold then the camera's video is shown. Any ideas what I should try to get rid of the shutter sound? I tried VolumeControl in place of CameraControl, same results, null pointer.
The CameraControl code gives a NPE because player.getControl returns null, and it does so because the string param is not correct. Try this one:
CameraControl control = (CameraControl) p.getControl("javax.microedition.amms.control.camera.CameraControl");

Categories

Resources