# Playing audio once on Processing Android

**URL:** <https://discourse.processing.org/t/playing-audio-once-on-processing-android/15032>\
**Category:** Processing for Android\
**Created:** [October 29, 2019, 8:38pm UTC](https://discourse.processing.org/t/playing-audio-once-on-processing-android/15032 "2019-10-29T20:38:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anony](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anony](https://discourse.processing.org/u/anony)\
**Post date:** [October 29, 2019, 8:38pm UTC](https://discourse.processing.org/t/playing-audio-once-on-processing-android/15032/1 "2019-10-29T20:38:11Z")

</div>

Hi guys! I was trying to play an audio file only once, not il loop. I found Cassette as an android audio library, but it contains only file.loop() and file.stop(). I was trying to create a function which can do this, but i didn’t find any file.duration or things like that. Help!

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [October 30, 2019, 10:25am UTC](https://discourse.processing.org/t/playing-audio-once-on-processing-android/15032/2 "2019-10-30T10:25:42Z")

</div>

@anony ===  
why not using android mediaPlayer, it has a lot of methods for duration and so on…  
more details here: [https://developer.android.com/reference/android/media/MediaPlayer](https://developer.android.com/reference/android/media/MediaPlayer)

---

<div class="post-metadata">

**Author:** ![anony](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anony](https://discourse.processing.org/u/anony)\
**Post date:** [November 6, 2019, 6:08pm UTC](https://discourse.processing.org/t/playing-audio-once-on-processing-android/15032/3 "2019-11-06T18:08:16Z")

</div>

Thank you so much for the help! I found my reply after a lot of attempts, but i dit it! Then, the function that I used to play a Sound once in processing android is:

```auto

MediaPlayer snd = new MediaPlayer();
AssetFileDescriptor fd;
Context context;
Activity act;

void setup(){
  act = this.getActivity();
  context = act.getApplicationContext();
}

void play(){ // name function is not important
  try {
    fd = context.getAssets().openFd("filename.extension");
    snd.setDataSource(fd.getFileDescriptor(),fd.getStartOffset(), fd.getLength());
    snd.prepare();
    snd.start();
  }
  catch (IllegalArgumentException e) {
    e.printStackTrace();
  }
  catch (IllegalStateException e) {
    e.printStackTrace();
  } 
  catch (IOException e) {
    e.printStackTrace();
  }
}

void draw(){
  // code lines...
  if(mousePressed){
    snd.reset(); // THIS IS THE KEY, YOU MUST CALL THIS FUNCTION TO MAKE IT WORK
    play(); // IMPORTANT
  }
}

```

If you know a more comfortable way, please tell me!  
I hope this can help someone.

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [November 7, 2019, 11:36am UTC](https://discourse.processing.org/t/playing-audio-once-on-processing-android/15032/4 "2019-11-07T11:36:11Z")

</div>

@anony ===  
no better solution; you have only to release your player onPause(); you can also recreate it with onResume() (if it is !null)
