“设备上启动了草图”,但手机上出现黑屏

时间:2019-02-13 14:29:47

标签: android processing

我有一个完全有效的代码。它可以运行所有内容并查找草图的数据。唯一的错误是不赞成使用的方法,但是带有下划线的黄色,因此我认为它不会受到影响。 它说Sketch已在设备上启动,但在我的手机上,打开该应用程序时它会出现快速闪烁,然后出现黑屏。 Pd:我在Windows 10 Pro上使用Processing 3.5.3,并且我的手机运行Android 5.1 Lollipop。 对不起,我是初学者。我不确定Android模式版本,处理版本,Android API级别或权限是否存在问题。

import ketai.sensors.*;
import android.media.MediaPlayer;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;

KetaiSensor sensor;

float minSize = 150 * displayDensity;
float maxSize = 300 * displayDensity;
ArrayList<ColorDot> dots;
int stepCount = 0;
int updateTime = 0;
float walkSpeed = 0;
int numNotes = 5;
AudioPlayer [] notes = new AudioPlayer[numNotes];
int lastNote = int(random(1) * 4);

void setup() {
  fullScreen();
  orientation(LANDSCAPE);
  colorMode(HSB, 360, 100, 100, 100);
  noStroke();
  for (int i = 0; i < numNotes; i++) notes[i] = new AudioPlayer();
  notes[0].loadFile(this, "Piano.mf.A1.mp3");
  notes[1].loadFile(this, "Piano.mf.G1.mp3");
  notes[2].loadFile(this, "Piano.mf.E1.mp3");
  notes[3].loadFile(this, "Piano.mf.D1.mp3");
  notes[4].loadFile(this, "Piano.mf.C1.mp3");
  dots = new ArrayList<ColorDot>();
  sensor = new KetaiSensor(this);
  sensor.start();
}

void draw() {
  background(0, 0, 0);
  for (int i = dots.size() - 1; i >= 0 ; i--) {
    ColorDot d = dots.get(i);
    d.update();
    d.display();
    if (d.colorAlpha < 1) {
      dots.remove(i);
    }
  }
}

class ColorDot {
  float posX, posY;
  float rad, maxRad;
  float colorHue, colorAlpha;
  int note;

  ColorDot() {
    posX = int(random(1, width/maxSize)) * maxSize;
    posY = int(random(1, height/maxSize)) * maxSize;
    rad = 0.1;
    float speedf = constrain(walkSpeed, 0, 2)/2.0;
    maxRad = map(speedf, 1, 0, minSize, maxSize);
    selectNote();
    colorHue = map(note, 0, 4, 0, 360);
    colorAlpha = 70;
  }

  void selectNote() {
    float r = random(1);
    note = lastNote;
    if (r < 0.4) note--;
    else if (r > 0.6) note++;
    if (note < 0) note = 1;
    if (4 < note) note = 3;
    notes[note].play();
    lastNote = note;
  }

  void update() {
    if (rad < maxRad) {
      rad *= 1.5;
    } else {
      colorAlpha -= 0.1;
    }
  }

  void display() {
    fill(colorHue, 100, 100, colorAlpha);
    ellipse(posX, posY, rad, rad);
  }
}

void onStepDetectorEvent() {
  int now = millis();
  stepCount++;
  if (5000 < now - updateTime) {
    walkSpeed = stepCount/5.0;
    stepCount = 0;
    updateTime = now;
  }
  dots.add(new ColorDot());
}

class AudioPlayer extends MediaPlayer {
  boolean loadFile(PApplet app, String fileName) {
    AssetFileDescriptor desc;
    try {
      desc = app.getActivity().getAssets().openFd(fileName);
    } catch (IOException e) {
      println("Error loading " + fileName);
      println(e.getMessage());
    return false;
    }

    if (desc == null) {
      println("Cannot find " + fileName);
      return false;
    }

    try {
      setDataSource(desc.getFileDescriptor(), desc.getStartOffset(), desc.getLength());
      setAudioStreamType(AudioManager.STREAM_MUSIC);
      prepare();
      return true;
    } catch (IOException e) {
      println(e.getMessage());
      return false;
    }
  }

  void play() {
    if (isPlaying()) seekTo(0);
    start();
  }
}

0 个答案:

没有答案