Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
@Andre: regarding OOM, I don't have much advise. Mainly avoid new (which I see you don't use, that's great). Next, avoid copies. Encoder encoder = Encoder(); could just be Encoder encoder;. (Yes, we have guaranteed copy elision in modern C++, but nobody really knows what C++ version Android actually supports)
@MyICQ: note that I accidentally calculated with kbits/s instead of bit/s, so I was off by a factor of 1000. OMG! See the new calculation. The whole data is sent at around the 3 o'clock mark
@MyICQ: accepting the other answer is ok. I am not overly happy with the digitalRead() decision of that answer - probably because I had too many issues with digitalRead() not working reliably enough in my projects. Anyway, if the suggested code is roughly what you need, that's a fine answer.
Is there a difference between &myClass.onMsg and MyClass::onMsg? I don't think this provides an instance for the function to call. Otherwise you could call member functions and it needn't be static. (Side note: myClass with lower case m is not a class, it's an object; same for libraryClass)
@Andre: your design looks more like a C# or Java design. Be careful with that on Arduino. You may run out of memory if your designs are very sophisticated and your code is too PC-like.
@Andre: you're welcome. Once you have tried the suggestion and tested that it actually works for you, you may mark this question as solved by checking the checkmark beneath an answer so that others know they don't need to spend time on this any more. No need to hurry, take your time to check that it's really the answer you need.
Why is no resistor needed? I don't exactly know OP's speaker, but my speaker says 8 Ohm. 8 Ohm at 5V is much more than an Arduino can handle, isn't it?