2D array to trigger either audio or video clip (at random) based on string input

guys!
so i had posted a discussion regarding this earlier ( which no one replied to :( ) but well, back then i was absolutely clueless as to how i can achieve this. Now however, i have somehow managed to get it working, but it is fulllll of glitches. So what i wanted was, based on a string input from keyboard ( "apple" "blue" "red" ) either an audio or a video ( linked to that particular string ) is triggered. All the video clips are in an array movies[] & the audio clips are in the array players[]. ive placed both these arrays in a 2d array myArray[][]. when i fire up the applet and type in a string (say "apple" ) it triggers off an audio or a video which is perfect, now if i type say "red" it switches to another audio or video which is also awesome, however say if i type "red" again, then it just pauses, or does nothing or just glitches basically. Could someone please help me out?! i feel the problem is somewhere within the void keyPressed() but i just dont understand how to fix it. find attached the entire code. thanks!

import processing.video.Movie;
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

Minim minim;

static final int QTY = 3;
static final int QTY2 = 3;

final Movie[] movies = new Movie[QTY];
final AudioPlayer[] players = new AudioPlayer[QTY2];

int[][] myArray = new int[QTY][QTY2];

int idx;
int idx2;
int index;

String typing = "";
String saved = "";

void setup() { 
  size(1440, 900, JAVA2D);
  frameRate(30);
  noSmooth();

  movies[0] = new Movie(this, "sin_city.mp4");
  movies[1] = new Movie(this, "blue_velvet.mp4");
  movies[2] = new Movie(this, "finale.mp4");

  minim = new Minim(this); 

  players[0] = minim.loadFile("one.mp3");
  players[1] = minim.loadFile("two.mp3");
  players[2] = minim.loadFile("three.mp3");

}

void draw() { 
  background(0);
  set(0,0,movies[idx] ); 
}

void movieEvent(Movie m) { 
  m.read(); 
} 

void keyPressed() {


 if (key == '\n' ) {
    saved = typing;
    // A String can be cleared by setting it equal to ""
    typing = ""; 
    index = int(random(myArray.length)); 
} else {
    // Otherwise, concatenate the String
    // Each character typed by the user is added to the end of the String variable.
    typing = typing + key; 
  }
 int k = keyCode, n = getMovieIndex(k) ;

  if (n >= 0){ //& n!= idx) {
    if (index == 1){
    movies[idx].pause();
    players[idx].pause();  
    movies[idx = n].play();
    } else if (index == 2){
    movies[idx].pause();  
    players[idx].pause();  
    players[idx = n].play();
    } else{
    movies[idx].pause();
    players[idx].pause();  
    }
    //println(myArray[index]);
      }
}

int getMovieIndex(int k) {
  if ( saved.equals("apple")){
  return 0;
  }else if(saved.equals("blue")){
  return 1;
  }else if(saved.equals("red")){
  return 2;
  }else{
  return -1;
  }
}

Answers

  • Not sure about the movie but if an AudioPlayer object has been played then it must be rewound before playing again.

    In this code the player starts from the beginning if it is not already playing.

    // If it isn't playing rewind it
    if (!players[idx].isPlaying()) {
        players[idx].rewind();
        players[idx].play();
    }
    
  • edited November 2014

    rewinding it would play the audio from the start right?? dont really want that. whats going wrong is, in the following piece of code-

     if (n >= 0){ //& n!= idx) {
            if (index == 1){
            movies[idx].pause();
            players[idx].pause();  
            movies[idx = n].play();
            } else if (index == 2){
            movies[idx].pause();  
            players[idx].pause();  
            players[idx = n].play();
            } else{
            movies[idx].pause();
            players[idx].pause();  
            }
            //println(myArray[index]);
              }
        }
    

    so what i think is happening ( maybe im totally wrong ) is that when i type "apple" and hit enter, movies[idx = n] plays or players[idx = n] plays. Now if i type "apple" again, movies[idx = n] becomes the same as movies[idx] ( similarly players[idx = n] is the same as players[idx] ) and hence the code starts to glitch. at least thats my noob opinion. what dyu think?


    also to clarify again, this code work fine, exactly how i want it to, its just that sometimes i have to type the same string twice/ thrice to trigger off an audio or video..

  • I can't test your code because I don't have the movies or sounds to play with but there are some problems with the code you posted.

    The biggest problem is that it is hard coded for 3 movies and 2 sounds so has to be changed dramatically if that changes. The number of movies and sounds are different making the code messy and again difficult to maintain.

    The following corrects these issues, but as I say I can't be sure it will solve the glitches

    // First check that n is potentially valid >= 0 and is a change != idx
    if (n >= 0 && n!= idx) {
      // Loop through all the players playing the new sound
      // and pause the others
      if (n < players.length ) {
        for (int i = 0; i < player.length; i++)
          if (i != n)
            players[n].pause(); 
          else
            players[n].play();
      }
      // Loop through all the movies playing the new movie
      // and pause the others
      if (n < movies.length ) {
        for (int i = 0; i < player.length; i++)
          if (i != n)
            movies[n].pause(); 
          else
            movies[n].play();
      }
      // Remember the new selection
      idx = n;
    }
    
  • edited November 2014

    Decided to make my own based on 3 sketches I've already had:

    1. http://forum.processing.org/two/discussion/869/check-array-contents-with-arraylist
    2. http://forum.processing.org/two/discussion/7852/problem-with-toggling-between-multiple-videos-on-processing-2-2-1
    3. http://forum.processing.org/two/discussion/7940/line-break-and-backspace-in-string

    I haven't tested it yet. Gonna leave it w/ y'all for now. Probably won't work but gonna test-drive it soon: :-j

    /**
     * Media Word Chooser (v1.0)
     * by GoToLoop (2014/Nov/21)
     *
     * forum.processing.org/two/discussion/8260/
     * 2d-array-to-trigger-either-audio-or-video-clip-
     * at-random-based-on-string-input
     */
    
    import static javax.swing.JOptionPane.*;
    
    import processing.video.Movie;
    
    import ddf.minim.Minim;
    import ddf.minim.AudioPlayer;
    
    static final String[] WORDS = {
      "apple", "blue", "red"
    };
    
    static final String[] VIDEOS = {
      "sin_city.mp4", "blue_velvet.mp4", "finale.mp4"
    };
    
    static final String[] AUDIOS = {
      "one.mp3", "two.mp3", "three.mp3"
    };
    
    final Movie[] movies = new Movie[VIDEOS.length];
    final AudioPlayer[] players = new AudioPlayer[AUDIOS.length];
    
    Movie mv;
    AudioPlayer pl;
    
    int idx, x, y;
    boolean isAudio, isBusyInput;
    
    void setup() {
      size(1440, 900, JAVA2D);
    
      noSmooth();
      noLoop();
      frameRate(30);
      clear();
    
      Minim minim = new Minim(this);
    
      while (idx != WORDS.length) {
        movies[idx]  = new Movie(this, VIDEOS[idx]);
        players[idx] = minim.loadFile(AUDIOS[idx++]);
      }
    
      mv = movies[idx = 0];
      pl = players[0];
    }
    
    void draw() {
      if (!isAudio)  set(x, y, mv);
      showInfo();
    }
    
    void keyTyped() {
      if (!isBusyInput) {
        isBusyInput = true;
        thread("dialogRequest");
      }
    }
    
    void movieEvent(Movie m) {
      m.read();
      redraw();
    }
    
    void centralizeMovie() {
      x = width  - mv.width  >> 1;
      y = height - mv.height >> 1;
      clear();
    }
    
    void showInfo() {
      String info;
    
      if (isAudio)
        info = round(pl.position()/1e3) + "\t " + round(pl.length()/1e3)
          + "\t " + (AUDIOS[idx]);
      else
        info = round(mv.time()) + "\t " + round(mv.duration())
          + "\t " + (VIDEOS[idx]);
    
      frame.setTitle(info);
    }
    
    void dialogRequest() {
      println(WORDS);
      String w = showInputDialog("Enter media word:");
    
      if (w == null || "".equals(w))
        showMessageDialog(null, "Empty Input!", "Alert", ERROR_MESSAGE);
    
      else {
        int ind = java.util.Arrays.binarySearch(WORDS, w.toLowerCase());
    
        if (ind >= 0) {
          pauseCurrentMedia();
          isAudio = random(1) < .5;
          idx = min(ind, isAudio? AUDIOS.length : VIDEOS.length);
          playChosenMedia();
        }
      }
    
      isBusyInput = false;
    }
    
    void pauseCurrentMedia() {
      if (isAudio)  pl.pause();
      else          mv.pause();
    }
    
    void playChosenMedia() {
      if (isAudio)  (pl = players[idx]).play();
      else {
        (mv = movies[idx]).play();
        centralizeMovie();
      }
    }
    
    @ Override void exit() {
      mv.stop();
      super.exit();
    }
    
  • guys! thanks so much for the help. havnt tested the stuff yet. though im gonna give these a shot tonight!

  • any luck?> i would like to download the full folder

Sign In or Register to comment.