Example #1
0
  /*
   * This method is called to initialize the AVAudioPlayer object
   */
  private void initAudioPlayer(String audioFile) {

    /*
     * A NSBundle object represents location in file system. Using pathForResource(),
     * a file with specified extension and name can be looked for in a given bundle directory.
     * Here, mainBundle() returns NSBundle object corresponding to the directory where
     * application executable is residing.
     */
    NSURL url = NSURL.fileURLWithPath(NSBundle.mainBundle().pathForResource(audioFile, "mp3"));
    NSErrorHolder errorHolder = new NSErrorHolder();

    /*
     * Initialize an audio player to play the audio file
     */
    audioPlayer = AVAudioPlayer.audioPlayerWithContentsOfURL(url, errorHolder);
    if (audioPlayer == null) {
      System.out.println("Error initializing player: " + errorHolder.description());
    }

    /*
     * Set indefinite number of loops
     */
    audioPlayer.setNumberOfLoops(-1);
    audioPlayer.setDelegate(this);
  }
  private void stop() {
    if (Main.getAudioPlayer() != null) {
      Main.getAudioPlayer().stop();
    }

    if (Main.library.getMusicInfos(songInfo).size() == 0) {
      playButton.setEnabled(false);
      repeatButton.setEnabled(false);
    } else if (Main.library.getMusicInfos(songInfo).size() > 1) {
      Main.setAudioPlayer(null);
    } else {
      Main.setAudioPlayer(
          AVAudioPlayer.audioPlayerWithContentsOfURL(
              NSURL.fileURLWithPath(
                  Main.library.getMusicInfos(songInfo).get(0).getMusicPath().getPath()),
              null));
      Main.getAudioPlayer().prepareToPlay();
      Main.getAudioPlayer().setNumberOfLoops(repeat ? -1 : 0);
    }
    setToolbarItems(new ArrayList<UIBarButtonItem>(buttonsPlay));
  }