We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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.
http://forum.processing.org/two/discussion/8235/need-processing-to-trigger-either-audio-clip-or-video-clip-using-random-function-
rewinding it would play the audio from the start right?? dont really want that. whats going wrong is, in the following piece of code-
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
Decided to make my own based on 3 sketches I've already had:
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
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