hello friends
i dont know more about jmf i play video using java media framework. now
How Can repeat video automatically means video play for ever until usr press stop button
thanks
"Reaching the end of play (signaled by an EndOfMedia event) is dealt with by setting the media's own time to zero (back to the start) and restarting the Player" [Book: (Java™ Media APIs: Cross-Platform Imaging, Media, and Visualization)]
In the code below, the object p is a player.
p.addControllerListener(new ControllerListener() {
public void controllerUpdate(ControllerEvent event) {
if (event instanceof EndOfMediaEvent) {
p.setMediaTime(new Time(0));
System.out.println("End of Media – restarting");
p.start();
}
}
});
After creating the player, you could add a ControllerListener. When the file ends an EndOfMediaEvent is generated. When you get that event, you can use the function setMediaTime(0) to start playing the file from the beginning
With an mp3, I had to call both player.start() and player.setMediaTime(new Time(0.)). Otherwise it would not replay any other way.
Related
I am working on a simpel game and I would like to have a video background. I first did this using a gif but this runs kind of slow. Now I created a video background using the mediaplayer and it works perfectly.
The video show without a problem.
The only problem I have is that the video does not want to loop. I tried every single aproache i found on the internet but nothing seems to work.
The video always plays 1 time and then stops.
I am using the java JDK8. Windows 10, 64 bit.
This is my code:
Media media = new
Media(getClass().getClassLoader().getResource("img/menu.mp4").toString());
MediaPlayer player = new MediaPlayer(media);
player.setAutoPlay(true);
player.setCycleCount(MediaPlayer.INDEFINITE);
MediaView view = new MediaView(player);
All of this is inside a stackpane.
I have tried exporting the mp4 to flv but this does not work.
If anyone knows different ways to create a video background, everything is welcome.
Edit:
So far no luck, I was thinking of using:
player.setOnEndOfMedia(new Runnable() {
#Override
public void run() {
player.seek(Duration.ZERO);
}
});
But not even this works..
Probably it's a bit late but you forgot to play after seeking to duration zero.
player.setOnEndOfMedia(new Runnable() {
#Override
public void run() {
player.seek(Duration.ZERO);
player.play();
}
});
It worked for me
From the MediaPlayer API:
Media playback commences at startTime and continues to stopTime. The interval defined by these two endpoints is termed a cycle with duration being the difference of the stop and start times. This cycle may be set to repeat a specific or indefinite number of times.
So you need to set startTime and stopTime before cycling works. For example (for a 5 second video):
player.setStartTime(Duration.seconds(0));
player.setStopTime(Duration.seconds(5));
If your keyboard has buttons for play/pause/etc (music control shortcuts), and you press them, iTunes will open (at least on Mac).
If you recently opened another music player, like Spotify, it will actually intercept the shortcut keys, and iTunes won't do anything.
Well, I want to make a music player with Java, and I want to have the same behavior. I want my application to intercept such shortcuts, and other programs shouldn't be able to interfere.
I am using JavaFX, although I don't think that really matters.
How can I achieve this?
I am already able to detect they keys the user presses using JNativeHook, but I do not know how to intercept the keys so that other applications won't do things with them.
Once you detect the keys, you could send the pause key so that the song that is being played by itunes is paused, you could use a boolean variable to detect between the shortcuts being typed on the keyboard or being send by the program(in case if you need)
or
You could use some c code(start the c program along with your java program) take a look at #Dave Delongs answer over here Modify NSEvent to send a different key than the one that was pressed
You could have a different keyboard shortcut and modify the c program to send your shortcut keys while the Itunes Shortcut keys are pressed, if you need the key codes Where can I find a list of Mac virtual key codes?
for example if your music program uses p to play songs and r to listen to the next song, and itunes uses spacebar to play songs and right arrow key to go to the next one, you could do modify #Dave Delongs answer here are the changes :-
#import <Cocoa/Cocoa.h>
CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
//0x31 is the virtual keycode for "Spacebar"
//0x23 is the virtual keycode for "p"
if (CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode) == 0x31) {
CGEventSetIntegerValueField(event, kCGKeyboardEventKeycode, 0x23);
}
//0x7C is the virtual keycode for "Right arrow"
//0x0F is the virtual keycode for "R"
if (CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode) == 0x7C) {
CGEventSetIntegerValueField(event, kCGKeyboardEventKeycode, 0x0F);
}
return event;
}
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
CFRunLoopSourceRef runLoopSource;
CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, kCGEventMaskForAllEvents, myCGEventCallback, NULL);
if (!eventTap) {
NSLog(#"Couldn't create event tap!");
exit(1);
}
runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
CFRunLoopRun();
CFRelease(eventTap);
CFRelease(runLoopSource);
[pool release];
exit(0);
}
You may be able to use some of the code from iTunesPatch to accomplish what you're looking for, but it appears that a system daemon may need to be modified upon installation, and you will likely have to use Objective-C/Swift.
There are further details about iTunesPatch in a blog post here.
I'm trying to make simple audio player with JavaFX Mediaplayer component. All local files are fine but i want also implement internet radio.
Code:
public static void main(String[] args) throws URISyntaxException {
new JFXPanel(); //init jfx library
String u = "http://91.121.164.186:8050";
Media m=null;
try {
m = new Media(u);
} catch (MalformedURLException e) {
e.printStackTrace();
}
MediaPlayer player = new MediaPlayer(m);
System.out.println("play");
player.play();
player.setVolume(new Double(1));
}
When I run it like this there is no errors but there is no audio. What's wrong ? What are other posibilities to play radio stream in Java ?
In your current example I can see two errors,
You are trying to run a JAVAFX component on a non-Javafx thread, which will result in error. Try running your program inside the start method. Please go through How to use JavaFX MediaPlayer correctly?
The URL you are trying to access must be a Media Compoenent
Try going through this extremely great example on Javafx Media
http://docs.oracle.com/javafx/2/media/EmbeddedMediaPlayer.zip
N.B. The example has lot more data than your require, but its a great example !
"http://91.121.164.186:8050" is a website, (HTML document), not a audio file. You need to download an audio file, something that the player knows what to do with.
I'm new to Slick2D, LWJGL, etc. Anyway I've been trying to make a simple in-game menu when pressing the escape button. Everything works fine, however when I hit the escape button I would like the current music to pause and play a sound when the menu opens. Then for the music to continue when the menu closes. However when I do hit escape it works the sound plays and the music stops, but when I hit escape again the music doesn't resume. Any suggestions?
if(input.isKeyPressed(Input.KEY_ESCAPE)) {
//Toggles Menu Open/Close
inGameMenu = !inGameMenu;
//Toggle Music to shut off
pauseMusic = !pauseMusic;
//Opacity trick
InGameMenu.resetOpacity = !InGameMenu.resetOpacity;
//toggle menu open/close
InGameMenu.closeMenu = !InGameMenu.closeMenu;
if(pauseMusic){
if(Sound.bgMusic.playing()){
Sound.bgMusic.pause();
}
ObjectSounds.menuOpen.play();
} else {
if(ObjectSounds.menuOpen.playing()) {
ObjectSounds.menuOpen.stop();
}
if(!Sound.bgMusic.playing())
Sound.bgMusic.play();
}
System.out.println(pauseMusic);
}
Turns out Music is its own dedicated audio channel, but there is Sound which is another.
In my j2me application i have to play a small sound file each times user click on an item. But the issues is when i play sound file multiple times like after 10-14 times it gives me
out of memory exception. Although i release the player each time i play the file but still it
gives out of memory exception : Here is the code snippet,
public void playSound(String soundFile) {
try{
if (player!=null) {
try {
player.deallocate(); //deallocate the unnecessary memory.
} catch (Exception ex) {
player=null;
System.gc();
}
}
player = Manager.createPlayer(getClass().getResourceAsStream(musicFolder + soundFile), "audio/mpeg");
// player = Manager.createPlayer(is, "audio/mpeg");
player.realize();
// get volume control for player and set volume to max
VolumeControl vc = (VolumeControl) player.getControl("VolumeControl");
if (vc != null) {
vc.setLevel(100);
}
player.prefetch();
player.start();
isException=false;
} catch (Exception e) {
isException=true;
}
}
Can someone tell me what is going wrong?
3 things to keep in mind
If you are going to play the same sound several times, you might want to keep one Player prefetched and simply start it multiple times.
When you want to properly cleanup a player, you should call Player.close()
You may want to use a media event listener to close and/or restart a player independently of user input.
I think you should also call
player.close()
right after after
player.deallocate();
According to documentation "When deallocate returns, the Player is in the UNREALIZED or REALIZED state." but close goes further... "When the method returns, the Player is in the CLOSED state and can no longer be used."
I'm not sure why the de-allocation isn't working. I guess it either takes longer to de-allocated than to create a new one, or the de-allocation fails for some reason. Is there a player.stop() to match the player.start()?
Another thing to try (if nothing else, for good form :) is not to create new player unless you need to/should. I.e. move the
if(player!=null){
So it also covers
player = Manager.createPlayer(getClass().getResourceAsStream(musicFolder + soundFile), "audio/mpeg");
HTH!