正在处理Android模式:找不到资产目录

问题描述 投票:0回答:1

我是编程的初学者,也是应用开发的初学者,所以当我听说Processing是Java的较简单版本,您还可以在其中开发应用时,我必须尝试一下。

我的代码是一个简单的音板。我从data / assets文件夹加载音乐文件,然后按一下按钮即可播放。

该代码在Java模式下“完全”起作用,但是当我切换到Android模式时,我开始出现错误。经过一番挖掘后,我意识到我需要将数据文件夹的路径更改为手机上存储的位置。我的问题是我不知道它的存储位置,即使经过数小时的网络搜索,我仍然找不到它。

我知道我的应用可以加载文件,因为尝试使用不同的路径选项加载了一些文件并显示了它们。

这是我的代码:

import processing.sound.*;
import android.os.*;
import android.os.Environment;
String path = ""; //This is where the assets path should be 
boolean loaded = false;
ArrayList<SoundFile> files = new ArrayList<SoundFile>();
ArrayList<button> buttons = new ArrayList<button>();
boolean exit = false;

void setup() {
  fullScreen();
  background(0);
  text("loading...", width/2, height/2);
}


void load() { //load files from path
  if (loaded == false) {
    String[] fileNames = listFileNames(path);
    try {
      printArray(fileNames);
      for (int i = 0; i < fileNames.length; i++) {
        files.add(new SoundFile(this, fileNames[i]));
      }
      for (int b = 0; b < fileNames.length; b++) {
        SoundFile file = files.get(b); //add buttons
        buttons.add(new button("rect", 5, (b*55)+5, width-75, 50, fileNames[b].replace(".mp3", ""), 255, 255, 255, file));
        buttons.add(new button("ellipse", width-35, (b*55)+30, 50, 50, fileNames[b].replace(".mp3", ""), 255, 255, 255, file));
      }
    } 
    catch(NullPointerException e) {
      textSize(18);
      fill(255);
      text("Loading failed", width/3, height/2);
      delay(700);
      exit = true;
    }
    loaded = true;
  }
}


String[] listFileNames(String dir) { //Gets name of all files in path
  File file = new File(dir);
  if (file.isDirectory()) {
    String names[] = file.list();
    return names;
  } else {
    return null;
  }
}

void draw() {
  frameRate(60);
  if (loaded == false){
    load();
  }
  if (exit == true) {
    exit();
  }
  background(0);
  for (button button : buttons) { //Button functions
    button.show();
    button.update(mouseX, mouseY);
  }
}

void keyPressed() {
  if (key == 'b') {
    for (SoundFile current : files) {
      //current.play();
    }
  } else {
  }
}

void touchStarted() {
  if (touchIsStarted) {
    for (button button : buttons) {
      button.show();
      button.update(mouseX, mouseY);
      button.clicked(mouseX, mouseY);
  }
  } else {

  }
}

class button {
  String type;
  int x;
  int y;
  int sizeX;
  int sizeY;
  String txt;
  int r;
  int g;
  int b;
  int rTemp;
  int gTemp;
  int bTemp;
  SoundFile file;

  button(String type, int x, int y, int sizeX, int sizeY, String txt, int r, int g, int b, SoundFile file) {
    this.type = type;
    this.x = x; 
    this.y = y;
    this.sizeX = sizeX;
    this.sizeY = sizeY;
    this.txt = txt; //Button label
    this.r = r; //red colour
    this.g = g; //green colour
    this.b = b; //blue colour
    this.rTemp = r;
    this.gTemp = g;
    this.bTemp = b;
    this.file = file; //sound file associated with the button
  }

  void show() { //Draws the buttons
    switch (type) {
    case "rect":
      fill(r, g, b);
      rect(x, y, sizeX, sizeY);
      fill(0);
      text(txt,x+10, y+sizeY/2+6);
      break;

    case "ellipse":
      fill(r, g, b);
      ellipse(x, y, sizeX, sizeY);
      break;
    }
  }

  void update(int mouseX, int mouseY){ //Mouse interactivity
    if (type.equals("rect") && mouseX > this.x && mouseX < this.sizeX && mouseY > y && mouseY < y+sizeY){
      this.r = rTemp-56;
      this.g = gTemp-80;
      this.b = bTemp-0;
    } else if (type.equals("ellipse") && sq(x-mouseX) + sq(y-mouseY) <= sq((sizeX+sizeY)/4)){
      this.r = rTemp-56;
      this.g = gTemp-80;
      this.b = bTemp-0;
    } else{
      this.r = rTemp;
      this.g = gTemp;
      this.b = bTemp;
    }

  }

  void clicked(int mouseX, int mouseY){ //Mouse interactivity
    if (type.equals("ellipse") && sq(x-mouseX) + sq(y-mouseY) <= sq((sizeX+sizeY)/4)){
      file.play();
    } else{

    }

  }
}

如果发现其他错误,请指出。一个从错误中学习!

android android-file
1个回答
0
投票

我知道这是一个古老的话题,我没有足够的声誉来发表评论。所以在这里我会介绍一下。使用Android的Processing的最简单方法是使用APDE应用程序。数据文件夹与资产文件夹不同。导出草图时可以找到最新版本,并打开创建的android文件夹。也可以使用AIDE应用代替Android Studio,并可以从那里运行草图。参见here

© www.soinside.com 2019 - 2024. All rights reserved.