williamfiset / FruitFever

Fruit Fever adventure game by Micah Stairs and William Fiset
5 stars 2 forks source link

Sound Effect and SoundTrack #109

Closed williamfiset closed 10 years ago

williamfiset commented 10 years ago

I'm having trouble implementing a way to play a SoundTrack as a compressed file. If we do not want our soundtracks to be 100mb for 5 minutes of music we will need to find a way to do this.

I tried the side you sent me: http://java-demos.blogspot.ca/2012/11/java-code-to-play-mp3-file.html It seems to be a good source so I downloaded the file and it made me create a jar which I called 'musicJar.jar'. This jar contains a ton of classes to load music and video files the only problem is I cannot import any of the classes.

williamfiset commented 10 years ago

Here's the code I got going on for the sound track. The sound effect code (not yet created) is trivial since they're short clips and we can use .wav format for them and java supports that easy. The issue remains with mp3 format. The code I have here has three major problems. First, it has a main method. Second, it extends Application from javafx.application.Application which seems to be needed to play the clip. And lastly the clip crashes after 1-2 minutes of playing... I'm using FadeToBlack.mp3 as a test clip which is about 6 min and stops playing after some time and I do not know why.

Here's the audio I used if you want to replicate the test case: https://www.dropbox.com/s/2766a32wh1qogwy/FadeToBlack.mp3

I tried a lot of different methods to get .mp3 to play and this is the only one that semi worked up until now. This issue is tagged "help wanted" because finding the right framework for this is a pain in the ass. Meanwhile I will take care of the sound effect code and once you finish your level designer we can tag team this one.


import java.io.File;
import java.net.MalformedURLException;
import javafx.application.Application;
import javafx.scene.media.*;
// import javafx.scene.Scene;
// import javafx.scene.control.Label;
// import javafx.scene.layout.StackPane;
// import javafx.scene.paint.Color;
import javafx.stage.Stage;

/** plays an audio in JavaFX 2.x */
public class SimpleAudioPlayer extends Application {

  public static void main(String[] args) { launch(args); }

  @Override public void start(Stage stage) throws MalformedURLException {

    MediaPlayer mediaPlayer = createMediaPlayer( "/Users/williamfiset/Desktop/FadeToBlack.mp3" );

    if (mediaPlayer != null) 
        mediaPlayer.play();

  }

  /** 
   * creates a media player using a file from the given filename path 
   * and tracks the status of playing the file via the status label 
   */
  private MediaPlayer createMediaPlayer(final String filename) throws MalformedURLException {

    File file = new File(filename);

    if (!file.exists()) 
        System.out.println("File does not exist: " + filename);

    final String mediaLocation = file.toURI().toURL().toExternalForm();

    Media media = new Media(mediaLocation);
    MediaPlayer mediaPlayer = new MediaPlayer(media);

    mediaPlayer.setOnError(new Runnable() {
        @Override public void run() {
            System.out.println("Error");
        }
    });

    mediaPlayer.setOnPlaying(new Runnable() {
        @Override public void run() {
            System.out.println("Player:  " + mediaLocation );
        }
    });

    mediaPlayer.setOnEndOfMedia(new Runnable() {
      @Override public void run() {
            System.out.println("Done");
        }
    });

    return mediaPlayer;

  }
}
williamfiset commented 10 years ago

I think I found the solution alas!

https://www.youtube.com/watch?v=ar0hTsb9sxM&list=PL-2t7SM0vDfcIedoMIghzzgQqZq45jYGv&index=8

williamfiset commented 10 years ago

The MusicPlayer class can play multiple sounds at the same time. We can make the sound effects and the soundtrack as mp3 files, no need to .wav anymore :0