# createFileInput function

**URL:** <https://discourse.processing.org/t/createfileinput-function/21478>\
**Category:** Libraries\
**Created:** [June 1, 2020, 1:53pm UTC](https://discourse.processing.org/t/createfileinput-function/21478 "2020-06-01T13:53:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![RbRB](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@RbRB](https://discourse.processing.org/u/RbRB)\
**Post date:** [June 1, 2020, 1:53pm UTC](https://discourse.processing.org/t/createfileinput-function/21478/1 "2020-06-01T13:53:58Z")

</div>

I create the code like this  
but the error keeps popping up.  
what’s should I do?

let song;  
let input;

function setup() {  
input = createFileInput(handleFile);  
input.position(0, 0);  
createCanvas(720, 200);  
background(255,0,0);  
}

function handleFile(file) {  
print(file);  
if (file.type === ‘mp3’) {  
song = songPlay(“audio”);  
song.hide();  
} else {  
song = null;  
}  
}  
function mousePressed() {  
if (!song.isPlaying())  
{song.stop();  
background(255,0,0);  
}  
else {  
song.play();  
background(0,255,0);  
}  
}

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 1, 2020, 3:33pm UTC](https://discourse.processing.org/t/createfileinput-function/21478/2 "2020-06-01T15:33:10Z")

</div>

Hello,

welcome to the forum!

Great to have you here!

Sorry, I don’t know.

In processing AND using _minim library_ like shown below.

Warm regards,

Chrisir

```auto

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

File result = null;
File resultOld; 

Minim minim;
AudioPlayer song;

boolean Loaded=false; 
boolean pause = false; 

void setup() {
  size(720, 200);
  background(0);

  minim = new Minim(this);
  selectInput("Select a song to load:", "fileSelected");
}

void draw() {

  // better use int state here: it can be : waitForLoad, loadSong, playSong, songPaused

  if (result==null) {
    //wait
    background(0);
    fill(255); 
    String s1= "Hit any key to load a new song ++ " ; 
    text(s1+s1+s1+s1+s1+s1+s1+s1, 1, 55);
  } else {
    // We got a result from the Load Dialog: 
    if ( ! Loaded) {
      // Load a soundfile and play it back
      Loaded = true; 
      song = minim.loadFile( result.getAbsolutePath(), 1024); // new SoundFile(this, result.getPath());
      song.play();
      background(0, 255, 0); // green - green means we play a song
    } else {
      // playing song
      // show screen Pause / Play 
      if ( pause ) {
        // Pause
        background(255, 0, 0); // red = Pause
        fill(0); 
        text("Pause. Press nouse to play", 55, 55);
      } else {
        // Play 
        background(0, 255, 0); // green - green means we play a song
        fill(0); 
        text(result.getAbsolutePath()
          +"\nPress mouse to pause."
          +"\nHit any key to load a new song", 55, 55);
      }//else
    }//else
  }//else
}//func 

void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
    // Did we have a song already?
    if (song!=null) { 
      // restore old song
      song.play(); 
      result=resultOld; 
      Loaded=true; 
      background(0, 255, 0); // green - green means we play a song 
      pause=false;
    }//if
  } else {
    println("User selected " + selection.getAbsolutePath());
    result = selection;
  }//else
}//func 

void mousePressed() {
  if (song==null) 
    return; 

  // toggle 
  if ( pause ) {
    song.play();
    pause=false;
  } else {
    song.pause();
    pause=true;
  }
}

void keyPressed() {
  // load a new song
  background(0);
  Loaded=false; 
  pause = false;
  resultOld=result; 
  result = null;
  if (song!=null) 
    song.pause();
  selectInput("Select a song to load:", "fileSelected");
}
//

```
