Sound lost at restart (e.g. orientation change) - official sound library

Processing 3.5.4 (in Android Mode)

Problem:
Using the official Processing Foundation “sound” library, the sound is lost after screen rotation.

  1. Could anybody confirm the behaviour:
    If the “sound” library is installed, run e.g. the “SineWave” example. (File → Example → Libraries → Sound → Oscillators → SineWave)
    It works properly on my android devices, but if the screen is rotated the audio is lost (not recreated).

(Actually the same occurs also for many other examples.)

  1. How to fix this?
    I am aware of the Activity life-cycle basics, and checked that “setup()” is run after the rotation - other variable initializations therein are excecuted properly, it is just the sound that is lost/not recreated…

Fixing the screen orientation is not an option for me (and in fact the same problem appears also in other restart cases…)

Maybe someone has a solution?

@Hans_342 === you have to handle configChanges; that can be done through Manifest and in the code.

Hi,

can you confirm that it also happens on your system? So I don’t have to look further for a bug in my system. Actually I can’t believe I am the first to run into this problem if this is really the standard behaviour.

As said, I am aware of the life-cycles… and I am aware that one can declare to handle configChanges in Manifest, and then define handling functions… But I haven’t found a way in this case to keep or recreate the sound.

Technically I don’t understand why a re-run of the setup() function, does not start the sound again. I also tried separate initialization(*) on a push of a button - all works fine until such a “conficChange”.

Maybe I am looking at the wrong places, hints are welcome.


(*) To me it seems, that the following code should be selfcontained, i.e. requires nothing special to be carried over a “configChange” (but maybe this is the problem?)

SinOsc sine;
sine = new SinOsc(this);
sine.play();
sine.amp(1.0);
sine.freq(261);
delay(500);
sine.stop();

In addition here a complete minimal example:

import processing.sound.*;

void setup() {
}

void draw() {
}

void mousePressed() {
  println("mouse pressed");
  SinOsc sine = new SinOsc(this);
  sine.play(261, 1.0);
  delay(500);
  sine.stop();
}

@Hans_342 === your problem is not yours; i can reproduce it withs my system; and i can (tested) solve ite as i have explained, handle configChanges. That is not weird, that is android and thinks to x // y position for images to understand that a configchange must be handled in a lot of cases.

Thank you very much for your confirmation. Then it seems I have some misconception, and I would like to understand and learn…
Which part of “sound” in the above example has to be carried over? There seems something within the library that gets destroyed and can not be recreated/initialized by the above lines?
I guess your example of x-y positions is only for illustration or are there actually x-y positions attached to the sound?


For those looking for a solution…
add to your sketch:

import android.app.Activity; 
import android.content.res.Configuration;

public void onConfigurationChanged(Configuration newConfig) {
  super.getActivity().onConfigurationChanged(newConfig);
}

and in AndroidManifest.xml add to android:name=".MainActivity" e.g. android:configChanges="keyboard|keyboardHidden|orientation|screenSize".

@Hans_342 === that is exactly what i suggested; 2 hints: a) super.getActivity is not good; i think that it fires error (as for me it fires one but is is easy to solve); b) in your Manifest it is not necessary to handle the keyboard, but why not? - As for x & y yes, it was only to explain & it has of course no sense for a sound; as for a sound it could be the moment for replaying (supposing that this library has ome method for knowing that: as i dont use it, i dont know. Last question (but depends on the project) could be when and how stop the sound.

just setup the orientation in your sketch every think works fin then

@jafal === yes but that is not answering and is against android principles: a phone is not a desktop computer

hi

with out initializing orientation in any sketch when rotating the device the sketch stop we can chose the orientation in many ways

@jafal === and if you dont want to set the orientation??? - The code i have put is the answer to that which was the question from @Hans_342

1 Like

yes true sir :smile: :heart: