美文网首页
Processing音乐播放-几个简单例子

Processing音乐播放-几个简单例子

作者: 思求彼得赵 | 来源:发表于2022-04-19 11:38 被阅读0次

2022-04-19
学习的几点:

  1. 调用函数库。sound函数库需要确认是否下载好。如无,则需要提前准备:


    image.png
  2. 待播放的音乐文件准备。
    这个通过加载就可以自动放到文件夹下的data子目录下。当然也可以自己建立data子目录并把音乐文件放到里面。


    image.png
    1. 第一个例子。
import processing.sound.*;

SoundFile file;

void setup() {
  size(640, 360);
  background(255);
    
  // Load a soundfile from the /data folder of the sketch and play it back
  file = new SoundFile(this, "kb.mp3");
  file.play();
}      

void draw() {
}
 

本示例只是播放音乐,没有绘制任何内容。所以出来的窗口是空白的。
这里使用到一个函数库。利用其中方法生成一个对象并调用播放(play)方法即可。

    1. 对播放进行控制。如鼠标暂停等,参考如下:
      为实现鼠标控制,增加了鼠标点击事件。
//引入processing的sound库
import processing.sound.*;
SoundFile song;

void setup(){
 size(640, 360);
 //初始化一个音频文件对象,音频文件为1.mp3,文件存放在当前*.pde文件所在目录的data文件夹下,同图片的存放路径
 song =new SoundFile(this,"1.mp3");
}

void draw(){
}

//添加鼠标事件
void mousePressed(){//点击鼠标
 if(song.isPlaying()){//如果声音在播放
   song.pause();//暂停
 }else{//否则
   song.play();//播放声音
   //play()函数只播放声音一次,如果要重复播放使用loop(),也就是song.loop()
 }
}
    1. 控制音量。
import processing.sound.*;


SoundFile song;

void setup(){
 size(200, 200);
 song =new SoundFile(this,"1.mp3");
 song.loop();
}

void draw(){
  background(255);
  float volume = map(mouseX, 0, width, 0, 1);
  song.amp(volume);
}

改进版本如下:(安居正作品)

import processing.sound.*;

SoundFile song;

void setup()
{
  size(640,360);
  background(255);
  
  song=new SoundFile(this,"kb.mp3");
  song.loop();

}
void draw()
{
  String str="Vol";
  String str1="click to continue/stop the music";
  text(str,250,180);
  textSize(15);
  fill(0);
  text(str1,400,300);
  textSize(30);
  
  stroke(0);
  strokeWeight(20);

  line(320,360,320,mouseY);//show the volum directly by a line.
  float vol=map(mouseY,height,0,0,1);
  song.amp(vol);
  
  stroke(255);
  line(320,0,320,mouseY);//cover the old line with a same white line,which equals to delete the old one.
}

void mousePressed()
{
  if(song.isPlaying())
  {
    song.pause();
  }
  else
  {
    song.play();
  }
}
image.png
  1. 控制循环播放:
import processing.sound.*;
SoundFile file;

void setup() {
  size(640, 360);
  background(255);
    
  // Load a soundfile from the data folder of the sketch and play it back in a loop
  file = new SoundFile(this, "sample.mp3");
  file.loop();
}      

void draw() {
}



核心的是使用sound库。

SoundFile

  • SoundFile

    This is a Soundfile Player which allows to play back and manipulate soundfiles.

  • add()

    Offset the output of the player by the given value.

  • amp()

    Changes the amplitude/volume of the player.

  • channels()

    Returns the number of channels of the soundfile as an int (1 for mono, 2 for stereo).

  • cue()

    Cues the playhead to a fixed position in the soundfile.

  • duration()

    Returns the duration of the soundfile in seconds.

  • frames()

    Returns the number of frames of this soundfile.

  • isPlaying()

    Check whether this soundfile is currently playing.

  • jump()

    Jump to a specific position in the soundfile while continuing to play (or starting to play if it wasn't playing already).

  • loop()

    Starts playback which will loop at the end of the soundfile.

  • pan()

    Move the sound in a stereo panorama.

  • pause()

    Stop the playback of the file, but cue it to the current position.

  • play()

    Starts the playback of the soundfile.

  • rate()

    Set the playback rate of the soundfile.

  • removeFromCache()

    Remove this SoundFile's decoded audio sample from the cache, allowing it to be garbage collected once there are no more references to this SoundFile.

  • set()

    Set multiple playback parameters of the soundfile at once.

  • stop()

    Stops the playback.

相关文章

网友评论

      本文标题:Processing音乐播放-几个简单例子

      本文链接:https://www.haomeiwen.com/subject/upjsertx.html