Play streaming audio with JavaFX - javafx

I tryed to play follwing streams:
url: "http://icecast.unitedradio.it/Radio105.mp3", codec: "mp3"
url: "http://shoutcast.radio24.it:8000/", codec: "AAC"
using the MediaPlayer of JavaFX.
This is the code:
Media media = new Media("http://icecast.unitedradio.it/Radio105.mp3");
MediaPlayer mediaPlayer = new MediaPlayer(media);
MediaView mediaView = new MediaView(mediaPlayer);
mediaView.setFitHeight(500);
mediaView.setFitWidth(500);
mediaPlayer.setVolume(new Double(1));
root.getChildren().add(mediaView);
Button playButton = new Button();
playButton.setOnMouseClicked(new EventHandler<javafx.scene.input.MouseEvent>()
{
#Override
public void handle(javafx.scene.input.MouseEvent event)
{
mediaPlayer.play();
}
});
root.getChildren().add(playButton);
primaryStage.setScene(scene);
primaryStage.show();
But pressing the playbutton the audio does not play.
Debugging the Media I have found that using one of stream above, in the Locator.init() the contentLength is -1. May be this the reason why it does not play?

Related

Play music in JavaFX

I have the following code to play music but cannot play the whole track. Music plays for 3 seconds then stops.
(I'm new to javaFX and hope anyone can help)
void Music(ActionEvent event) throws Exception {
String path = "Music.mp3";
playHitSound(path);
}
private void playHitSound(String fileName) {
String path = getClass().getResource(fileName).getPath();
Media media = new Media(new File(path).toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
mediaPlayer.play();
}

javafx mp4 working in IDE but not in jar file

ok so i have a mp4 file in my src folder in my project and it work's fine in eclipse but when i export it to a runnable jar file it doesn't work.
public void playRickRoll() throws MalformedURLException {
StackPane spRickRoll = new StackPane();
Stage stage = new Stage();
stage.setResizable(false);
stage.getIcons().add(new Image("60AKG.png"));
// URL videoURL = getClass().getResource("RickRoll.mp4");
// Media media = new Media(videoURL.toString());
File f = new File("src/RickRoll.mp4");
Media media = new Media(f.toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
MediaView mediaView = new MediaView();
mediaView.setMediaPlayer(mediaPlayer);
spRickRoll.getChildren().add(mediaView);
stage.setScene(new Scene(spRickRoll, 460, 360));
stage.show();
mediaPlayer.play();
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent arg0) {
mediaPlayer.stop();
}
});
}

why javafx mediaplayer status sometimes returns unknown?

First i am sorry for my poor english...
i made Media Player Application with Javafx.
this player can get mulit file media. and play files out of all limits.
it work well. but sometimes not work..
it is not media error. it is mediaplayer error.
error message is 'mediaPlayer Unknown, media Invalid..' why.??
i played same video file(1920 * 1080), sometimes work and sometimes not work..
and javafx is depend on OS ??
player works perfectly on windown7 computer
but player have this error on windown10 computer...
please give me advice..
MediaPlayer mediaPlayer = null;
Stage stage = new Stage();
AnchorPane pane = new AnchorPane();
Scene scene = new Scene(pane);
MediaView mediaView = new MediaView();
int mNextFileIndex = -1;
List<File> fileLists = new ArrayList<>();
Media media;
mediaplayer play Method
public void playNextMedia() {
if (mediaPlayer != null) {
mediaPlayer.dispose();
mediaView.setMediaPlayer(null);
}
mNextFileIndex = (mNextFileIndex + 1) % fileLists.size();
media =new Media(fileLists.get(mNextFileIndex).toURI().toString());
media.setOnError(()-> {
MainApp.makeLog("media error");
});
mediaPlayer = new MediaPlayer(media);
mediaView.setMediaPlayer(mediaPlayer);
mediaPlayer.setOnReady(() -> {
mediaPlayer.play();
});
mediaPlayer.setOnEndOfMedia(() -> {
playNextMedia();
});
mediaPlayer.setOnError(() -> {
systom.out.println("mediaPlayer error");
Systeom.out.println(mediaPlayer.getError().getMessage());
playNextMedia();
});
}
Button Method
#FXML
private void playMedia(ActionEvent event) {
mNextFileIndex = -1;
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().addAll(new
FileChooser.ExtensionFilter("Select a File (*.mp4)", "*.mp4"),
new FileChooser.ExtensionFilter("All Files", "*.*"));
List<File> list = fileChooser.showOpenMultipleDialog(null);
if (list != null) {
for (File file : list) {
fileLists.add(file)
}
playNextMedia();
pane.getChildren().add(mediaView);
stage.setScene(scene);
stage.show();
}

Getting webpage opened in another tab from hyperlink of current webpage in webview of javafx browser

I have developed a very simplified but fully functional JavaFX web browser for illustrating what I want to ask. Here follows the source code.
public class OpenInNewTab extends Application {
#Override
public void start(Stage primaryStage)
{
TabPane tabPane = new TabPane();
WebView webView = new WebView();
Tab tab = new Tab("Home Tab");
tab.setContent(webView);
tabPane.getTabs().add(tab);
webView.getEngine().load("https://www.google.co.in/?gws_rd=ssl#q=javafx");
BorderPane root = new BorderPane();
root.setTop(tabPane);
Scene scene = new Scene(root, 600, 339);
primaryStage.setTitle("Basic browser");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
} }
This browser loads a web page from this link. I want that when I click on any of the search results, the clicked hyperlink should open in new tab adjacent to the existing "Home tab".
After searching on the internet extensively, I reached nowhere.
Please help me with relevant code. Thanks.
First of all, you need to find a way to handle all outgoing click events on the displayed page. I've made a helper method to achieve that. It creates an event listener for each link, clicking on which will open a new tab.
private void handleUrls(Document doc) {
NodeList links = doc.getElementsByTagName("a");
for (int i = 0; i < links.getLength(); i++) {
EventTarget eventTarget = (EventTarget) links.item(i);
String link = links.item(i).toString();
eventTarget.addEventListener("click", e -> {
WebView webView = new WebView();
alterWebView(webView);
Tab tab = new Tab(link);
tab.setContent(webView);
tab.setClosable(true);
webView.getEngine().load(link);
tabPane.getTabs().add(tab);
e.preventDefault();
}, false);
}
}
You may want to set a better name for your tab... I've used a link as a makeshift solution, but it's long and messy.
webView.getEngine().load(link);
The alterWebView referenced above is another helper function. It's responsible for tracking the loading progress. Once the Worker does nothing, it will add listeners to all urls.
private void alterWebView(WebView webView) {
WebEngine engine = webView.getEngine();
Worker worker = engine.getLoadWorker();
worker.stateProperty().addListener((oldVal, newVal, o) -> {
if (newVal.equals(State.RUNNING)) {
handleUrls(engine.getDocument());
}
});
}
So basically, the logic behind the code above is as follows:
We track our WebEngine's State until the page is fully loaded and then we add event listeners to every url.
These listeners, when invoked, will create a new WebView, handle it accordingly, and add it to the TabPane inside a new Tab.
And here's the complete, working example based on your code:
public class JavaFXTest extends Application {
private TabPane tabPane;
#Override
public void start(Stage primaryStage) {
tabPane = new TabPane();
WebView webView = new WebView();
alterWebView(webView);
Tab tab = new Tab("Home Tab");
tab.setContent(webView);
tab.setClosable(true);
tabPane.getTabs().add(tab);
webView.getEngine().load("https://www.google.co.in/?gws_rd=ssl#q=javafx");
BorderPane root = new BorderPane();
root.setTop(tabPane);
Scene scene = new Scene(root, 600, 339);
primaryStage.setTitle("Basic browser");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private void alterWebView(WebView webView) {
WebEngine engine = webView.getEngine();
Worker worker = engine.getLoadWorker();
worker.stateProperty().addListener((oldVal, newVal, o) -> {
if (newVal.equals(State.RUNNING)) {
handleUrls(engine.getDocument());
}
});
}
private void handleUrls(Document doc) {
NodeList links = doc.getElementsByTagName("a");
for (int i = 0; i < links.getLength(); i++) {
EventTarget eventTarget = (EventTarget) links.item(i);
String link = links.item(i).toString();
eventTarget.addEventListener("click", e -> {
WebView webView = new WebView();
alterWebView(webView);
Tab tab = new Tab(link);
tab.setContent(webView);
tab.setClosable(true);
webView.getEngine().load(link);
tabPane.getTabs().add(tab);
e.preventDefault();
}, false);
}
}
}

Javafx sound play stopped after seconds

i want to play a sound when i start run the program...... the sound playing is working but he is stopped after seconds(about 7 seconds) what is the prob?
Here is the code that i used it to play sound
public void start(Stage primaryStage) {
MediaPlayer m=new MediaPlayer(new Media(new File("n.mp3").toURI().toString()));
Pane root=new Pane();
//MediaView mv=new MediaView(m);
m.play();
//root.getChildren().add(mv);
primaryStage.setTitle("Audio Player 1");
primaryStage.setWidth(200);
primaryStage.setHeight(200);
primaryStage.show();
}
}
Try using AudioClip class since MediaPlayer is meant for conbinnig with MediaView with use case such as controlls
AudioClip au = new AudioClip(MyClassName.class.getResource("n.mp3").toString());
au.play();

Resources