This discussion is archived
5 Replies Latest reply: Nov 4, 2012 6:15 AM by shakir.gusaroff RSS

audio clip problem

kapac Newbie
Currently Being Moderated
Hi;
I would use AudioClip but i don't see any method giving the length of the mp3;am I obliged to use a mediaplayer to get the length of the audio file
thanks
regards
  • 1. Re: audio clip problem
    jsmith Guru
    Currently Being Moderated
    You can create a Media object and query it's duration.
    You don't need a MediaPlayer to determine the length.
    As you note, there is no public API to get the length of an AudioClip.

    http://docs.oracle.com/javafx/2/api/javafx/scene/media/Media.html#durationProperty
  • 2. Re: audio clip problem
    kapac Newbie
    Currently Being Moderated
    Hi;thank you for your answer
    i had seen before the method media.getDuration() but when i use it i get the result UNKNOWN for duration with the following code
      private void strchoice(String str){
               String str2=str+".wav";
               res=getClass().getResource(str2);
               media =new Media(res.toString());
                 Duration dur= media.getDuration();
                clip=new AudioClip(res.toString());
                clip.play();
        }
    but the clip plays the sound
  • 3. Re: audio clip problem
    kapac Newbie
    Currently Being Moderated
    i found in javadoc :
    The media information is obtained asynchronously and so not necessarily available immediately after instantiation of the class. All information should however be available if the instance has been associated with a MediaPlayer and that player has transitioned to MediaPlayer.Status.READY status.
    So i must create a mediaplayer in status READY to get the length in media
  • 4. Re: audio clip problem
    kapac Newbie
    Currently Being Moderated
    i modify my code" without success
    {code}private void strchoice(String str){
    String str2=str+".wav";
    res=getClass().getResource(str2);
    media =new Media(res.toString());
    mp=new MediaPlayer(media);

    mp.setOnReady(new Runnable() {

    @Override
    public void run() {
    Duration dur=mp.getMedia().getDuration();
    }
    });
    Status status= mp.getStatus();
    clip=new AudioClip(res.toString());
    clip.play();
    } {/code}
    the status is always UNKNOWN
    i don't know how to get the READY status without playing the mediaplayer
  • 5. Re: audio clip problem
    shakir.gusaroff Expert
    Currently Being Moderated
    Hi. You can use the MediaPlayer.Status.PLAYING status, but this is probably not what you want.
       final String MEDIA_URL = getClass().getResource("coffee.wav").toExternalForm();
            final Media media = new Media(MEDIA_URL);
            MediaPlayer mediaPlayer = new MediaPlayer(media);
            final MediaView mediaView = new MediaView(mediaPlayer);
    
    
            mediaView.getMediaPlayer().statusProperty().addListener(
                    new ChangeListener<MediaPlayer.Status>() {
                        @Override
                        public void changed(ObservableValue<? extends MediaPlayer.Status> ov,
                                MediaPlayer.Status oldState, MediaPlayer.Status newState) {
    
                            if (newState == MediaPlayer.Status.PLAYING) {
                                Duration dur = media.getDuration();
                                System.out.println(dur);
    
                            }
                        }
                    });
    
            mediaPlayer.play();
    There is a discussion about audio effects in the mailing list.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points