예제 #1
0
  public synchronized long BackgroundMusicCreateFromFile(String fileName, int[] error) {
    if (fileName.equals("")) {
      error[0] = GAUDIO_CANNOT_OPEN_FILE;
      return 0;
    }

    AssetFileDescriptor fd = null;

    if (fileName.startsWith("/")) {
      File file = new File(fileName);
      if (!file.isFile()) {
        error[0] = GAUDIO_CANNOT_OPEN_FILE;
        return 0;
      }
    } else {
      if (patchFile_ != null) fd = patchFile_.getAssetFileDescriptor(fileName);
      if (fd == null && mainFile_ != null) fd = mainFile_.getAssetFileDescriptor(fileName);
      if (fd == null)
        try {
          fd = WeakActivityHolder.get().getAssets().openFd("assets/" + fileName);
        } catch (IOException e) {
        }

      if (fd == null) {
        error[0] = GAUDIO_CANNOT_OPEN_FILE;
        return 0;
      }
    }

    MediaPlayer player = new MediaPlayer();

    int duration = 0;

    try {
      if (fd != null) {
        player.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
        fd.close();
      } else {
        player.setDataSource(fileName);
      }
      player.prepare();
      duration = player.getDuration();
      player.release();
      player = null;
    } catch (Exception e) {
      error[0] = GAUDIO_UNRECOGNIZED_FORMAT;
      return 0;
    }

    long gid = nextgid();

    sounds_.put(gid, new Sound(fileName, duration));

    return gid;
  }
예제 #2
0
  public synchronized long BackgroundMusicPlay(long backgroundMusic, boolean paused, long data) {
    Sound sound2 = sounds_.get(backgroundMusic);

    if (sound2 == null) return 0;

    String fileName = sound2.fileName;

    AssetFileDescriptor fd = null;

    if (fileName.startsWith("/")) {
      File file = new File(fileName);
      if (!file.isFile()) return 0;
    } else {
      if (patchFile_ != null) fd = patchFile_.getAssetFileDescriptor(fileName);
      if (fd == null && mainFile_ != null) fd = mainFile_.getAssetFileDescriptor(fileName);
      if (fd == null)
        try {
          fd = WeakActivityHolder.get().getAssets().openFd("assets/" + fileName);
        } catch (IOException e) {
        }

      if (fd == null) return 0;
    }

    MediaPlayer player = new MediaPlayer();

    try {
      if (fd != null) {
        player.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
        fd.close();
      } else {
        player.setDataSource(fileName);
      }
      player.prepare();
    } catch (Exception e) {
      return 0;
    }

    long gid = nextgid();

    player.setOnCompletionListener(new OnCompletionListener(this, gid));

    Channel channel = new Channel(gid, player, sound2, data);

    sound2.channels.add(channel);

    channels_.put(gid, channel);

    channel.paused = paused;
    if (!paused) channel.player.start();

    return gid;
  }