loadFile(path)

edited January 2017 in Questions about Code

Hey i want to choose my own mp3 file and i use atm this code here

import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;

Minim minim;
AudioPlayer Player;
int farbe;
PImage playPause;
PImage hintergrund;
void setup() {
  selectInput("Suche einen Song aus","selected");
  size(500,400);
  background(0);
  minim = new Minim(this);
  Player = minim.loadFile(path); // Spielt die Mp3 datei ab
  Player.loop(); // Wiederholt den aktuellen Song
  farbe = 0;
  smooth();
  playPause = loadImage("play3.jpg");
  hintergrund = loadImage("hintergrund2.jpg");
}

void selected(File song){
  path = song.getAbsolutePath();
  }

void interrupt(){
  while(path == null){
      noLoop();
    }
    loop();
  }

void draw()  {
  background(0);
  image(hintergrund,0,0);
  image(playPause,25,25);
  translate(width/2, height/2);
  rotate(radians(frameCount % 360 * 2));
  colorMode(HSB);
  stroke(farbe, 255, 255);
  colorMode(RGB);

  for(int j = 0; j < 360; j++){

    if(Player.mix.get(j)*200 > 50) {
      colorMode(HSB);
      stroke(farbe, 255,255);
      colorMode(RGB);
    }
    else {
      colorMode(HSB);
      stroke(farbe,255,255);
      colorMode(RGB);
    }
    line(cos(j)*100, sin(j)*100, cos(j)*abs(Player.left.get(j))*250 + cos(j)*100, sin(j)*abs(Player.right.get(j))*250 + sin(j)*100);
  }

    //for(int i = 0; i < fft.avgSize(); i++){
    //line((i * w)*3, height/1.1, (i * w)*3, height/1.1 - fft.getAvg(i) * 0.5);

  farbe += 2;
  if( farbe > 255)
  {
    farbe = 0;
  }
// #########MOUSE############
    if (mousePressed && (mouseButton == LEFT)) {
    Player.pause();
  } else if (mousePressed && (mouseButton == RIGHT)) {
    Player.play();
  }

  }

but he says The variable "path" does not exist what should i do to fix this?

Tagged:

Answers

  • Insert String path; just after your imports. You should also consider handling the null case in line 27, in case the user decides to cancel the operation. This is demonstrated in the reference.

    Kf

  • edited January 2017

    Hey i did what you say and i get an error it calls NullPointerExeption Here is my code

        import ddf.minim.*;
        import ddf.minim.analysis.*;
        import ddf.minim.effects.*;
        import ddf.minim.signals.*;
        import ddf.minim.spi.*;
        import ddf.minim.ugens.*;
        String path;
    
        Minim minim;
        AudioPlayer Player;
        int farbe;
        PImage playPause;
        PImage hintergrund;
    
        void setup() {
          size(500,400);
          background(0);
          selectInput("Suche einen Song aus","selected");
          minim = new Minim(this);
          Player = minim.loadFile(path); // Spielt die Mp3 datei ab
          Player.loop(); // Wiederholt den aktuellen Song
          farbe = 0;
          smooth();
          playPause = loadImage("play3.jpg");
          hintergrund = loadImage("hintergrund2.jpg");
        }
    
        void selected(File song){
          path = song.getAbsolutePath();
          }
    
        void interrupt(){
          if (path == null){
            println("Windows was closed or the user hit cancel.");
          } else {
            println("User selected " + path);
            noLoop();
          }
          }
    
        void draw()  {
          background(0);
          image(hintergrund,0,0);
          image(playPause,25,25);
          translate(width/2, height/2);
          rotate(radians(frameCount % 360 * 2));
          colorMode(HSB);
          stroke(farbe, 255, 255);
          colorMode(RGB);
    
          for(int j = 0; j < 360; j++){
    
            if(Player.mix.get(j)*200 > 50) {
              colorMode(HSB);
              stroke(farbe, 255,255);
              colorMode(RGB);
            }
            else {
              colorMode(HSB);
              stroke(farbe,255,255);
              colorMode(RGB);
            }
            line(cos(j)*100, sin(j)*100, cos(j)*abs(Player.left.get(j))*250 + cos(j)*100, sin(j)*abs(Player.right.get(j))*250 + sin(j)*100);
          }
    
            //for(int i = 0; i < fft.avgSize(); i++){
            //line((i * w)*3, height/1.1, (i * w)*3, height/1.1 - fft.getAvg(i) * 0.5);
    
          farbe += 2;
          if( farbe > 255)
          {
            farbe = 0;
          }
        // #########MOUSE############
            if (mousePressed && (mouseButton == LEFT)) {
            Player.pause();
          } else if (mousePressed && (mouseButton == RIGHT)) {
            Player.play();
          }
    
          }
    
  • edited January 2017

    I'm not a minim expert, but I see two problems:

    1. selectInput begins to launch the dialog and the code keeps going -- so your next variable is null because the dialog hasn't run and the callback hasn't fired. The solution is to put the next few lines of code in the callback (see below).

    2. everywhere in your code, name your AudioPlayer player -- lowercase. This might not be causing an error right now, but calling it Player is a bad idea -- capital names should be class names, and it might be an existing class name in an audio library.

      void setup() {
        size(500, 400);
        background(0);
        selectInput("Suche einen Song aus", "selected");
        farbe = 0;
        smooth();
        playPause = loadImage("play3.jpg");
        hintergrund = loadImage("hintergrund2.jpg");
      }
      
      void selected(File song) {
        path = song.getAbsolutePath();
        minim = new Minim(this);
        player = minim.loadFile(path); // Spielt die Mp3 datei ab
        player.loop(); // Wiederholt den aktuellen Song
      }
      
    3. Now your draw loop may be giving you an error because player is undefined (for the same reason it was in setup). You could handle this in many ways. Two approaches:

      1. Have the sketch load a default player file object in setup before giving the user an select dialog -- e.g. a short / silent / empty mp3. Now the sketch can run while on fake input while it waits for real input.
      2. Wrap references to player in if statements and check if player is null.
This discussion has been closed.