Results 1 to 8 of 8
  1. #1
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    3

    Default Using audio files

    Java FX Code:
    import java.io.File;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    import javafx.application.Application;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.paint.Color;
    import javafx.stage.Stage;
    
    public class More extends Application {
    	
    	Button audio;
    	Scene scene1;
    	Stage window;
    	
    	public static void main(String[] args) {
    		  launch(args);
        }
    	public void start(Stage primaryStage) {
    	
    		    window = primaryStage;
    	        primaryStage.setTitle("Moves");
    	       
    	         audio = new Button ("Play me");
    		     audio.setTranslateY (150);
    		     audio.setTranslateX (150);
    		     audio.setOnAction(e -> {
    		    
    		    		    try
    		    		    {
    		    		        Clip clip = AudioSystem.getClip();
    		    		        clip.open(AudioSystem.getAudioInputStream(new File("back.wav")));
    		    		        clip.start();
    		    		        clip.loop(3);
    		    		        
    		    		    }
    		    		    catch (Exception exc)
    		    		    {
    		    		        exc.printStackTrace(System.out);
    		    		    }
    		    		    		   
    		     						});
    	   
    		     Group full = new Group ();
    		     full.getChildren().addAll(audio);
    		     
    		     scene1 = new Scene (full,600,600,Color.LIGHTBLUE);
    	        
    		     primaryStage.setScene(scene1);
    		     window.show();
           
    		
    	}
    }
    This code plays wav files well. However it do not play mp3 files or other formats.
    Why it is so?

    And is it possible to have this code work without try/catch ? If I remove thems - compilator demands to add them back.
    But perhaps it is possible to overcome them, to have more clear code?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Using audio files

    You should keep the try catch. And within the exception use the exc to print the trace. It will help in debugging.
    It's possible you need to set the audio format type. As I am not familiar with the API you will need to check the JavaDoc.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Using audio files

    you are loading a file in your code and that's why the try-catch block is there to stay :).
    In line 33 you are calling the constructor for File and it expects a URI as String as parameter. I would guess that your URI is corrupt.
    imho it should look something like
    Java FX Code:
     ... new File("myFile.mp3".toURI().toString())
    Let us know whether that solves the problem.
    mp3 files work well with JavaFX.

  4. #4
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    3

    Default Re: Using audio files

    toURI is marked as error.

    I found this one:

    Java FX Code:
     String uriString = new File("grip.mp3").toURI().toString();
     MediaPlayer player = new MediaPlayer( new Media(uriString));
        	player.play();
    Plays .wav and .mp3

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Using audio files

    Use:
    Java FX Code:
    AudioSystem.getAudioFileTypes();
    to get the list of file types that javax.sound supports.
    It never used to support MP3 by default.

    As you've seen, the media part of JavaFX does support MP3 files.

    Checking on my Mac I get:
    WAVE
    AU
    AIFF
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Using audio files

    I have no issues playing .mp3 files.

    here is a sample code that I just tested. You have to replace the URI and the filename, but I guess you figured that already. Major portions of this code might come from the book JavaFX 8.0 (just to keep things clean).

    Java FX Code:
    package media;
    
    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.HBox;
    import javafx.scene.media.Media;
    import javafx.scene.media.MediaPlayer;
    import javafx.stage.Stage;
    
    public class MediaMain extends Application {
    	Double volume = 0.5;
    	@Override
    	public void start(Stage primaryStage) {
    		HBox root = new HBox();
    		Scene scene = new Scene(root, 500,100);
    		
    		Media media = null;
    		try{
    		String dingens = "file:///Users/xxxxx/xxxx/2_WIP/FXPlayGround/src/media/Tagewiediese.mp3";
    		media = new Media(dingens);		// works the same way for video files
    		}
    		catch (Exception e){e.printStackTrace();}
    		
    		MediaPlayer mediaPlayer = new MediaPlayer(media);
    		Button play  = new Button("play");
    		Button pause = new Button("pause");
    		Button stop = new Button("stop");
    		Button down = new Button("Vol -");
    		Button up = new Button("Vol +");
    		TextField voltext = new TextField();
    		
    		voltext.setText(volume.toString());
    		
    		play.setOnAction(new EventHandler<ActionEvent>(){
    			public void handle(ActionEvent event) {
    				mediaPlayer.play();
    			}
    		});
    		pause.setOnAction(new EventHandler<ActionEvent>(){
    			public void handle(ActionEvent event) {
    				mediaPlayer.pause();
    			}
    		});
    		stop.setOnAction(actionevent -> {
    			mediaPlayer.stop();
    		});
    		down.setOnAction(actionevent -> {
    			double newVolume = 0;
    			if (volume>0){
    				newVolume = volume-0.1;
    			}
    			else newVolume=0;
    			mediaPlayer.setVolume(newVolume);
    			voltext.setText(Double.toString(newVolume));
    			setVol(newVolume);
    		});
    		up.setOnAction(actionevent -> {
    			double newVolume=1;
    			if (volume<1){
    				newVolume = volume+0.1;
    			}
    			else newVolume = 1;
    			mediaPlayer.setVolume(newVolume);
    			voltext.setText(Double.toString(newVolume));
    			setVol(newVolume);
    		});
    		root.setAlignment(Pos.CENTER);
    		root.getChildren().addAll(play, pause, stop, up, down, voltext);
    		primaryStage.setTitle("MediaPlayer");
    		primaryStage.setScene(scene);
    		primaryStage.show();
    	}
    	
    	private void setVol(Double vol){
    		volume = vol;
    	}
    
    	public static void main(String[] args) {
    		launch(args);
    	}
    }
    Last edited by benji2505; 01-20-2017 at 08:55 PM.

  7. #7
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    3

    Default Re: Using audio files

    Bravo. Your and my code have no problems.

    If I run your code on my other computer I receive:
    Java FX Code:
    String dingens = "file://c:/music.mp3";
    Please note: 2x slashes after "file:


    MediaException: MEDIA_INACCESSIBLE : c
    at javafx.scene.media.Media.<init>(Media.java:409)
    at OP.Sounds.start(Sounds.java:24)
    at com.sun.javafx.application.LauncherImpl.lambda$lau nchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$run AndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$nul l$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$run Later$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run( InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Nativ e Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$14 8(WinApplication.java:191)
    at java.lang.Thread.run(Unknown Source)
    Exception in Application start method
    java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchAppl icationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchAppl ication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
    Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchAppl ication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$lau nchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.NullPointerException: media == null!
    at javafx.scene.media.MediaPlayer.<init>(MediaPlayer. java:396)
    at OP.Sounds.start(Sounds.java:28)
    at com.sun.javafx.application.LauncherImpl.lambda$lau nchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$run AndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$nul l$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$run Later$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run( InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Nativ e Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$14 8(WinApplication.java:191)
    ... 1 more
    Exception running application OP.Sounds

    Java FX Code:
    String dingens = "file:///c:/music.mp3";
    Please note: 3x slashes.

    #
    # A fatal error has been detected by the Java Runtime Environment:
    #
    # EXCEPTION_ILLEGAL_INSTRUCTION (0xc000001d) at pc=0x000007fef77a5c01, pid=5128, tid=0x0000000000000ccc
    #
    # JRE version: Java(TM) SE Runtime Environment (8.0_121-b13) (build 1.8.0_121-b13)
    # Java VM: Java HotSpot(TM) 64-Bit Server VM (25.121-b13 mixed mode windows-amd64 compressed oops)
    # Problematic frame:
    # C [msvcr120.dll+0x95c01]
    #
    # Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
    #
    # An error report file with more information is saved as:
    # E:\workplace\GameDev\hs_err_pid5128.log
    #
    # If you would like to submit a bug report, please visit:
    # Java Crash Reporting Page
    # The crash happened outside the Java Virtual Machine in native code.
    # See problematic frame for where to report the bug.
    #

  8. #8
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Using audio files

    seems to be a known problem for a certain build. Check this.
    Some people report that installing the 32bit version of the msvcr120.dll helped. Try it.

Similar Threads

  1. Replies: 9
    Last Post: 02-18-2015, 03:08 PM
  2. Playing audio files, simplest way possible
    By herpeslurpy in forum New To Java
    Replies: 2
    Last Post: 09-19-2013, 02:20 AM
  3. Generating audio files from a binary source
    By _max_ in forum New To Java
    Replies: 2
    Last Post: 10-05-2011, 07:23 AM
  4. Multiple audio files played at once?
    By RightGoodEnt in forum CLDC and MIDP
    Replies: 0
    Last Post: 04-05-2011, 03:31 PM
  5. Audio files.
    By sarah jain in forum Networking
    Replies: 1
    Last Post: 03-11-2011, 05:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •