JAVA-OpenCV: Video Writer not opening - java

I want to save the video I'm capturing with opencv in Android and I've chosen to use the openCV VideoWriter class (if there's a better way for Android please let me know).
The problem is that I'm not being capable of opening the object.
This is what I'm trying
videoWriter = new VideoWriter("saved_video.avi", VideoWriter.fourcc('M','J','P','G'),
25.0D, new Size(mOpenCvCameraView.getWidth(),mOpenCvCameraView.getHeight()));
videoWriter.open("saved_video.avi", VideoWriter.fourcc('M','J','P','G'),
25.0D, new Size(mOpenCvCameraView.getWidth(),mOpenCvCameraView.getHeight()));
I keep getting videoWriter.isOpened()=false when it should be true.
Does anyone know what I'm doing wrong? Thanks in advance

The problem was the video path! It should be fully stated, like:
pathSavedVideoFolder = getExternalFilesDir(null).getPath();
filenameRawVideo = pathSavedVideoFolder + "/SavedVideo.avi";
After this you just need to refresh in order to see the file inside "Android/data/YOUR PROJECT/files" folder.

Related

I am trying to implement some sounds into a JavaFX project I am working on, and running into a IllegalArgumentException

So as you read, I'm trying to get some sound working on a main menu of a JavaFX program I am writing. I have put the mp3 I'm trying to play inside of a sounds folder, in a res folder in the project package. Yet when I call it similar to how I call an image for the window, I get an IllegalArgumentException error.
Media menuMusic = new Media("sounds/menu_music.mp3");
menuPlayer = new MediaPlayer(menuMusic);
menuPlayer.setAutoPlay(true);
menuPlayer.setVolume(0.8);
Here is a link to the photo of where the file is stored in the project.
And the error I get:
Caused by: java.lang.IllegalArgumentException: uri.getScheme() == null! uri == 'sounds/menu_music.mp3'
If anyone could help me out it would be much appreciated. I've never worked with Media in JavaFX, so I could be doing this completely wrong for all I know, thanks!
menuPlayer = new Media(Paths.get("sounds/menu_music.mp3").toUri().toString());
" java.lang.IllegalArgumentException: uri.getScheme() == null! " error using JavaFX to play audio
EDIT: As #James_D points out. The above code will fail if you try to compile it into a jar. The correct way to load a file from a resource is as follows.
menuPlayer = new Media(getClass().getResource("/sounds/menu_music.mp3").toExternalForm());

How to use an image in a java project without an absolute path

I've been working in a little project to get a bit of practice programming. It's basically done, but I won't be satisfied until I can use images properly, a bit of help would be appreciated.
So, currently I'm using the the getImage method from the ImageIcon class, like so:
Image body = new ImageIcon("C:/Users/Centollo/Documents/NetBeansProjects/Chess/build/classes/chess/img/WhiteBishop.png").getImage();
I've been trying to figure out how to do the same thing without using an absolute path, but I don't know how to make the images a part of the jar so that it works fine in any other machine.
All I need to know is where to put the images and how would the code to access them look like.
Try to explain it like I'm stupid, please. I've read answers to similar questions but I can't make heads or tails of them.
I'm working in NetBeans with a "chess" package with all the .java and a "chess.img" package with all the .png.
If your class extends from JFrame, you can do this:
Image image = new ImageIcon(this.getClass().getResource("/images/MyImage.jpg")).getImage();
If your class extends Applet, you can go this way:
private URL base = null;
private Image myImage = null;
try
{
base = getDocumentBase();
} catch (Exception e)
{
e.printStackTrace();
}
myImage = getImage(base, "images/MyImage.jpg");
A very quick google search yields this:
URL resource = getClass().getClassLoader().getResource( "img/WhiteBishop.png" );
Image body = new ImageIcon( resource );
There are a couple ways to do this but here's the way I would suggest:
Make sure the chess.img folder is in your application's classpath. Then try referring to the path like chess.img/image (yes, you can use forward slash in windows.)
If that doesn't work use:
ChessClass.class.getClassLoader().loadResourceAsStream("/chess.img/image");
Note the forward slash at the beginning of the file reference. This points to the root of the classpath. It's a bit confusing as someone with unix/linux experience might think it refers to the root of the file system. This tends to work better than the other answer given for reasons I knew 10 years ago. This is an ugly bit of Java that was never quite cleaned up.

YouTubeEmbededPlayer GWT HowTo stop videos?

I am trying to use the youtube video gwt api.
The youtube-player works but how can I stop videos? I didnt find a command for that...
I created my player following:
protected YouTubeEmbeddedPlayer _youTubeEmbeddedPlayer;
_youTubeEmbeddedPlayer = new YouTubeEmbeddedPlayer(youTubeVideoID);
That´s the YouTube Player:
https://code.google.com/p/gwt-youtube-api/wiki/EmbededPlayer
By using YouTubePlayerWrapper.
You can stop video by calling the method
youTubePlayerWrapper.stopVideo();
This answer may not resolve the problem mentioned with the same library. In fact I tried multiple different library with every library having some issues. So end up creating my own wrapper. I have made it public check it if you can use it https://github.com/pandurangpatil/gwt-youtube

how can I create a thumbnail of a video url in android?

I have a problem, I can only create thumbnails of local video files but not of a remote url, here is my code:
bmThumbnail = ThumbnailUtils.extractThumbnail(ThumbnailUtils.createVideoThumbnail("http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4", MediaStore.Video.Thumbnails.MINI_KIND), 50, 50);
I hope you can help me,
regards
christian
I suppose there is no easy way to build the thumbnail without actually downloading the video locally.
So if your question is 'Can I get a thumbnail without having to download the full video?', I'd say...no.
Otherwise, once you have downloaded the video locally, then I guess you can perfectly use ThumbnailUtils.createVideoThumbnail(...) by giving the path to the downloaded file.
I also have the same problem - but what can I say from my tests:
The problem occurs only on android >2.3
in android 2.0 -> 2.3 You can use just
Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail( videoUrl, MediaStore.Video.Thumbnails.MINI_KIND);
I hope someone explain what change is on android 4. it doesn't work
I have no problem generating thumbnails from remote videos with the following code:
final Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail( videoUrl, MediaStore.Video.Thumbnails.MINI_KIND );
You don't have to wrap an extractThumbnail() call around it

How to play sound in JavaFX?

I'm experimenting with JavaFX making a small game.
I want to add sound.
How?
I tried MediaPlayer with media defined with relative source attribute like:
attribute media = Media{
source: "{__FILE__}/sound/hormpipe.mp3"
}
attribute player = MediaPlayer{
autoPlay:true
media:media
}
It doesn't play.
I get
FX Media Object caught Exception com.sun.media.jmc.MediaUnavailableException: Media unavailable: file: ... Sound.class/sound/hormpipe.mp3
Just a guess, but is that file "hornpipe.mp3" and not "hormpipe.mp3" (with an m)?
var player = javafx.scene.media.MediaPlayer {
repeatCount: javafx.scene.media.MediaPlayer.REPEAT_FOREVER
media: Media { source: "{\_\_DIR\_\_}clip.wav"
};
};
player.play();
You have to incluye the audio file in the build/compiled directory so Netbeans can pack it into the jar file.
Just a guess, but I think your {__FILE__} will expand to the name of your file. Try replacing it with {__DIR__}.
Also note that {__DIR__} includes the trailing /, so try this instead:
attribute media = Media{
source: "{__DIR__}sound/hormpipe.mp3"}
EDIT: I did some digging, and apparently, the source of a Media object has to be either a remote URL, or an absolute file path, since media files aren't allowed in JARs (something I hope gets changed with future releases, since I really like JavaFX and want to be able to make desktop apps with it). See: JavaFX FAQs.
This worked for me:
MediaPlayer audio = new MediaPlayer(
new Media(
new File("file.mp3").toURI().toString()));
Source file should be in project's root directory (not src, not dist).
OK, having used this question to get MP3 audio working (kinda), I've learned the following (not much).
1) Audio for compressed formats is very platform dependent. My continually upgraded Mint 17.1->18 machine plays mp3 fine using Media and MediaPlayer. Fresh installs of Mint 18 won't (with the dev tools).
So use .wav files.
Media sound=new Media(new File("noises/roll.wav").toURI().toString());
MediaPlayer mediaPlayer=new MediaPlayer(sound);
mediaPlayer.play();
2) One of the things you need to be aware of with Media/MediaPlayer is that in order to play multiple times (repeatedly or all at once ie, on a button press/whatever in a game) you have to spawn N number of MediaPlayer objects, and each one will play once and then stop.
So use javafx.scene.media.AudioClip
AudioClip soundMyNoise = new AudioClip(new File("noises/roll.wav").toURI().toString());
soundMyNoise.play();
AudioClip also has its issues, which include storing the raw audio data in RAM all at once instead of buffering. So there is the possibility of excessive memory use.
No matter which method you end up going with, one thing to be critically aware of was mentioned by daevon earlier - the path issue. With NetBeans, you have NetBeansProjects/yourproject/src/yourproject/foo.java. The sounds in the example above go in NetBeansProjects/yourproject/noises/roll.wav

Categories

Resources