I`m trying to put some music or a short clip of a sound to my project. I try some helps from here. It kinda work, but I could not hear anything. It just wrote that its playing.
I found a lot of examples, but nothing is working for me. Maybe you can help me with that AudioStream, AudioPlayer. Many people are using it. How can I use it in my Java Eclipse 4.4.0?
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import sun.audio.*;
public class zaciatky {
public static void main(String[] args) throws LineUnavailableException {
// TODO Auto-generated method stub
try {
AudioInputStream audoAudioInputStream=AudioSystem.getAudioInputStream(new File("D:\\Betka\\Dokumenty\\INF\\JAVA\\Saxik\\Music\\saxik.wav"));
Clip songik=AudioSystem.getClip();
songik.open(audoAudioInputStream);
songik.start();
System.out.println("Song is playing...");
System.out.println(songik.getFormat());
if(songik.isActive()) System.out.println("is active");
songik.stop();
if(songik.isActive()) System.out.println("is active");
else System.out.println("not active");
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
System.out.println("Error occures with playing the sound.");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Error occures with playing the sound.");
e.printStackTrace();
}
}
}
Related
I am making javafx cinema seat reservation system as semester project.I want to add .mp3 music so that it plays in background while application is running and performing its fuctions.I added music but what happens is when I run code applications starts and I can perform my seat selections in GUI but there is no background music.but when I click cross(close button) GUI closes and music starts playing...I only included the music code portion.have a look
import java.io.File;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
public class Cinemafx extends Application{
public void start(Stage primaryStage) {
try {
FileInputStream fileInputStream = new FileInputStream("song.mp3");
Player player = new Player(fileInputStream);
System.out.println("Song is playing...");
player.play();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JavaLayerException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
package com.testng.learn;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import io.appium.java_client.MobileBy;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidKeyCode;
public class First {
AndroidDriver driver;<br/>
DesiredCapabilities caps;<br/>
#BeforeTest<br/>
public void DC() {<br/>
try {<br/>
// Set the Desired Capabilities<br/>
caps = new DesiredCapabilities();<br/>
caps.setCapability("deviceName", "My Phone");<br/>
caps.setCapability("udid", "LGM70021d764e8"); // Give Device ID of your mobile phone<br/>
caps.setCapability("platformName", "Android");<br/>
caps.setCapability("platformVersion", "7.1.1");<br/>
caps.setCapability("appPackage", "com.android.contacts");<br/>
caps.setCapability("appActivity", "com.android.contacts.activities.DialtactsActivity");<br/>
caps.setCapability("noReset", "true");<br/>
driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), caps);<br/>
} catch (MalformedURLException e) {<br/>
// TODO Auto-generated catch block<br/>
e.printStackTrace();<br/>
}
}
#DataProvider()<br/>
public void scroll(String text) {<br/>
try {<br/>
System.out.println(text);<br/>
driver.findElement(MobileBy.AndroidUIAutomator(
"new UiScrollable(new UiSelector()).scrollIntoView(" + "new UiSelector().text(\"" + text + "\"));"))
.click();<br/>
Thread.sleep(5000);<br/>
} catch (InterruptedException e) {<br/>
// TODO Auto-generated catch block<br/>
e.printStackTrace();<br/>
}<br/>
}<br/>
#Test(priority = 0)<br/>
public void print() {<br/>
try {<br/>
String text = "Karthik";<br/>
Thread.sleep(2000);<br/>
driver.findElementByAccessibilityId("Contacts Tab 3 of 4").click();<br/>
Thread.sleep(1000);<br/>
First f1 = new First();<br/>
f1.scroll(text);<br/>
Thread.sleep(1000);<br/>
driver.pressKeyCode(AndroidKeyCode.BACK);<br/>
Thread.sleep(2000);<br/>
driver.pressKeyCode(AndroidKeyCode.HOME);<br/>
} catch (InterruptedException e) {<br/>
// TODO Auto-generated catch block<br/>
e.printStackTrace();<br/>
}<br/>
}<br/>
}
As according to your code, You have define #DataProvider() method incorrectly. You should need to refer articles on TestNG Dataprovider.
I'd like to type: "/ammo" when I type ALT+A.
The program runs but it seems like to stop right after the running:
I press alt+A or A and the code is not doing anything at all.
package jnativehook01;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
public class Example implements NativeKeyListener {
public void nativeKeyPressed(NativeKeyEvent e) {
if (NativeKeyEvent.getKeyText(e.getKeyCode()).equals("A")) {
try {
GlobalScreen.unregisterNativeHook();
Robot bot;
try {
bot = new Robot();
String text = "/ammo";
StringSelection stringSelection = new StringSelection(text);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);
//type: /ammo
bot.keyPress(KeyEvent.VK_T);
bot.keyRelease(KeyEvent.VK_T);
} catch (AWTException e1) {
}
} catch (NativeHookException e1) {
}
}
}
public void nativeKeyReleased(NativeKeyEvent e) {
System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
}
public void nativeKeyTyped(NativeKeyEvent e) {
System.out.println("Key Typed: " + e.getKeyText(e.getKeyCode()));
}
public static void main(String[] args) {
new Example();
}
}
Ok, now it's working:
package jnativehook01;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
import java.util.logging.*;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;
public class Example implements NativeKeyListener {
public void nativeKeyPressed(NativeKeyEvent e) {
if (NativeKeyEvent.getKeyText(e.getKeyCode()).equals("A")) {
Robot bot;
try {
String text = "/ammo";
StringSelection stringSelection = new StringSelection(text);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);
bot = new Robot();
for (int i = 0; i < 10; i++) {
//t
bot.keyPress(KeyEvent.VK_T);
bot.delay(100);
bot.keyRelease(KeyEvent.VK_T);
bot.delay(500);
bot.keyPress(KeyEvent.VK_CONTROL);
bot.keyPress(KeyEvent.VK_V);
bot.keyRelease(KeyEvent.VK_V);
bot.keyRelease(KeyEvent.VK_CONTROL);
bot.delay(500);
//Enter
bot.keyPress(KeyEvent.VK_ENTER);
bot.keyRelease(KeyEvent.VK_ENTER);
bot.delay(1000);
bot.keyPress(KeyEvent.VK_ENTER);
bot.keyRelease(KeyEvent.VK_ENTER);
bot.delay(400);
}
} catch (AWTException e1) {
}
}
}
public void nativeKeyReleased(NativeKeyEvent e) {
}
public void nativeKeyTyped(NativeKeyEvent e) {
}
public static void main(String[] args) {
Example ex = new Example();
try {
GlobalScreen.registerNativeHook();
Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
logger.setLevel(Level.OFF);
} catch (NativeHookException eb) {
System.out.println(eb.getMessage());
}
GlobalScreen.addNativeKeyListener(ex);
}
}
There are still a few problems with your code.
if (NativeKeyEvent.getKeyText(e.getKeyCode()).equals("A")) {
This is not the correct way to check for the A key. This will check to see if AWT produces the ascii 'A' for that key. These values could be overridden by the JVM at runtime. You should check for the NativeKeyEvent.VK_A constant. You also fail to check for the ALT flag on that key event. Something like the following is more of what you are looking for.
if (e.getKeyCode() == NativeKeyEvent.VC_A && e.getModifiers() & NativeInputEvent.ALT_MASK) {
A note on thread safety. You are relying on AWT inside of the key callback, however, this library does not use AWT to dispatch events by default. You need to take a look at the Thread Safety section of the wiki for Thread Safe examples.
You may choose to replace the Robots class with GlobalScreen.postNativeEvent(...) for convenience, to omit Swing/AWT, but it is not required.
A note on blocking inside of the event listener callback. If you block inside this function, via sleep or other long running process, you may cause a delay in key event delivery by the OS or worse, library removal by some operating systems. This removal is outside the control of the library and controlled by the OS.
I've written a program that has to open an image I've placed on the desktop. The problem is that the program runs but nothing happens in the console nor does it open the image. Do I've to implement a GUI to fix the problem or...?
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Image {
public JavaImage() {
try {
BufferedImage image = ImageIO.read(new File("C:/Users/Username/Desktop/index.jpg"));
}
catch (IOException e) {
System.out.println("There isn't a picture in the folder");
}
}
public static void main(String[] args) {
new Image();
}
}
I want to display information about the musical composition in the browser via Java applet. I use the library beaglebuddy_mp3.jar for id3 tags. A folder with files looks like this:
applet
- index.html
- FirstApplet.class
- beaglebuddy_mp3.jar
In index.html I connect an applet:
<applet code="FirstApplet.class" archive="beaglebuddy_mp3.jar" width="500" height="500"></applet>
FirstApplet.class contains the following code:
import java.applet.Applet;
import java.awt.Graphics;
import java.io.File;
import java.io.IOException;
import com.beaglebuddy.mp3.MP3;
public class FirstApplet extends Applet{
public void paint(Graphics g){
try {
MP3 mp3 = new MP3("D:\\Music\\abc.mp3");
g.drawString(mp3.getBand() +" "+mp3.getTitle(), 20, 20);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
After starting the index.html file dialog box appears with a warning stating that I run the application at your own risk. Then I click "Run", instantly appears and disappears gray square. On that nothing is displayed.
Try the following:
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;
import java.io.File;
import java.io.IOException;
import com.beaglebuddy.mp3.MP3;
public class FirstApplet extends JApplet {
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
MP3 mp3 = new MP3("D:\\Music\\abc.mp3");
JLabel label = new JLabel(mp3.getBand() +" "+mp3.getTitle());
add(label);
}
});
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
}
And secondly you have to sign your applet code with an official certificate to be able to run it in your web browser.