2020年5月28日 星期四

很酷 可以放音樂~~~
import ddf.minim.*;
Minim minim;
AudioPlayer player;
void setup(){
    minim=new Minim(this);
    player=minim.loadFile("song.mp3");
    player.play();
}
void draw(){
}



PImage imgBird;
void setup(){
  size(500,400);
  imgBird=loadImage("bird.png");//圖檔如果沒有放好,會當掉 pointer
  imageMode(CENTER);//以正中心,當座標
}
float birdX=100, birdY=300, oldX, oldY, vx=0, vy=0;//之前的鳥在哪裡
boolean bMoving=false;//if(判斷) ex. if(bMoving)...
void draw(){
  background(255);//清背景
  image(imgBird, birdX,birdY,100,100);//不能一直跟著mouse動,改用birdX,birdY
  if(bMoving) line(birdX,birdY, oldX,oldY);//有按到,才能動它
  birdX+=vx; birdY+=vy;//鳥要照速度去飛!!!
  if(birdX>450 || birdX<50) vx = -vx*0.95;//撞到邊界,會改變方向,相反的方向
  if(birdY>350 || birdY<50) vy = -vy*0.95;//撞到邊界,會改變方向,相反的方向
}
void mouseReleased(){
  if(bMoving){
    bMoving=false;//放開時,就不能動了
    vx=(oldX-mouseX)/10; vy=(oldY-mouseY)/10;// 0.1倍
  }
}
void mousePressed(){//按下去,才會開始動
  if( dist(mouseX,mouseY, birdX,birdY)<50){
    oldX=birdX; oldY=birdY;
    bMoving=true;///按下去時,如果有按到,才能動它
  }
}
void mouseDragged(){//按下去,才會開始動
  if(bMoving) {//有按到,才能動它
    birdX=mouseX; birdY=mouseY;
  }
}






/**
  * This sketch demonstrates how to play a file with Minim using an AudioPlayer. <br />
  * It's also a good example of how to draw the waveform of the audio. Full documentation
  * for AudioPlayer can be found at http://code.compartmental.net/minim/audioplayer_class_audioplayer.html
  * <p>
  * For more information about Minim and additional features,
  * visit http://code.compartmental.net/minim/
  */

import ddf.minim.*;

Minim minim;
AudioPlayer player;

void setup()
{
  size(512, 200, P3D);

  // we pass this to Minim so that it can load files from the data directory
  minim = new Minim(this);

  // loadFile will look in all the same places as loadImage does.
  // this means you can find files that are in the data folder and the
  // sketch folder. you can also pass an absolute path, or a URL.
  player = minim.loadFile("groove.mp3");
}

void draw()
{
  background(0);
  stroke(255);

  // draw the waveforms
  // the values returned by left.get() and right.get() will be between -1 and 1,
  // so we need to scale them up to see the waveform
  // note that if the file is MONO, left.get() and right.get() will return the same value
  for(int i = 0; i < player.bufferSize() - 1; i++)
  {
    float x1 = map( i, 0, player.bufferSize(), 0, width );
    float x2 = map( i+1, 0, player.bufferSize(), 0, width );
    line( x1, 50 + player.left.get(i)*50, x2, 50 + player.left.get(i+1)*50 );
    line( x1, 150 + player.right.get(i)*50, x2, 150 + player.right.get(i+1)*50 );
  }

  // draw a line to show where in the song playback is currently located
  float posx = map(player.position(), 0, player.length(), 0, width);
  stroke(0,200,0);
  line(posx, 0, posx, height);

  if ( player.isPlaying() )
  {
    text("Press any key to pause playback.", 10, 20 );
  }
  else
  {
    text("Press any key to start playback.", 10, 20 );
  }
}

void keyPressed()
{
  if ( player.isPlaying() )
  {
    player.pause();
  }
  // if the player is at the end of the file,
  // we have to rewind it before telling it to play again
  else if ( player.position() == player.length() )
  {
    player.rewind();
    player.play();
  }
  else
  {
    player.play();
  }
}

沒有留言:

張貼留言