Do not play audio if audio service is not ready

Currently, audio is playing without any checking audio service.
This causes a crash if audio service is not ready yet.

Add checking audio service before playing audio to avoid this crash.

Bug: 232943444
Test:
Manual testing of Cherrypick-To-Main as follows:
Tweak the clockwork boot animation to have audio:
$ cd ~/main/device/google/clockwork/bootanimations/square_280
$ mv bootanimation.zip bootanimation_orig.zip
$ unzip -d newanimation bootanimation.zip
$ cd newanimation
$ cp ~/main/frameworks/base/data/sounds/newwavelabs/Miami_Twice.wav part1/audio.wav
$ zip -0qry -i \*.txt \*.png \*.wav @ ../bootanimation.zip *.txt part*
$ cd ..
$ unzip -l bootanimation.zip  | grep files
  1494650                     197 files
$ unzip -l bootanimation_orig.zip  | grep files
   770910                     196 files
Confirmed initAudioThread is a "no-op" (early exits without
calling audioplay::create) when audio service is not available.
Confirmed the playPart() and shutdown() AudioAnimationCallbacks
are safe "no-ops" if they're called when audioplay::create()
has not been called.  See ag/21777853 for details and b/272124709
to track "clarifying they're safe" enhancement.
After confirming this all works on main, created a cherrypick
of the original CL.

Cherrypick-To-Main observation:
Considered moving the code added to InitAudioThread::threadLoop()
to the top of AudioAnimationCallbacks::init() to eliminate the
overhead of running a "no-op" thread when the audio service is
not available.
For Wear-On-Main initiative, our primary goal is to move
wear-dev CLs directly to main – happy to file an enhancement
to track this consideration if it would be an improvement.

Change-Id: Ica3525fa8bf8534778db388211e8a7c2ade6db9f
(cherry picked from commit ca5110fc1083706c021b28e8c524e92f8fd34349)
diff --git a/cmds/bootanimation/audioplay.cpp b/cmds/bootanimation/audioplay.cpp
index da85a1c..9b95b04 100644
--- a/cmds/bootanimation/audioplay.cpp
+++ b/cmds/bootanimation/audioplay.cpp
@@ -20,6 +20,8 @@
 #define CHATTY ALOGD
 #define LOG_TAG "audioplay"
 
+#include <binder/IServiceManager.h>
+
 #include "audioplay.h"
 
 #include <string.h>
@@ -316,8 +318,13 @@
         : Thread(false),
           mExampleAudioData(exampleAudioData),
           mExampleAudioLength(exampleAudioLength) {}
+
 private:
     virtual bool threadLoop() {
+        if (defaultServiceManager()->checkService(String16("audio")) == nullptr) {
+            ALOGW("Audio service is not ready yet, ignore creating playback engine");
+            return false;
+        }
         audioplay::create(mExampleAudioData, mExampleAudioLength);
         // Exit immediately
         return false;
@@ -406,14 +413,14 @@
 }
 
 bool playClip(const uint8_t* buf, int size) {
-    // Parse the WAV header
-    const ChunkFormat* chunkFormat;
-    if (!parseClipBuf(buf, size, &chunkFormat, &nextBuffer, &nextSize)) {
+    if (!hasPlayer()) {
+        ALOGE("cannot play clip %p without a player", buf);
         return false;
     }
 
-    if (!hasPlayer()) {
-        ALOGD("cannot play clip %p without a player", buf);
+    // Parse the WAV header
+    const ChunkFormat* chunkFormat;
+    if (!parseClipBuf(buf, size, &chunkFormat, &nextBuffer, &nextSize)) {
         return false;
     }
 
@@ -438,11 +445,9 @@
 void setPlaying(bool isPlaying) {
     if (!hasPlayer()) return;
 
-    SLresult result;
-
     if (nullptr != bqPlayerPlay) {
         // set the player's state
-        result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay,
+        (*bqPlayerPlay)->SetPlayState(bqPlayerPlay,
             isPlaying ? SL_PLAYSTATE_PLAYING : SL_PLAYSTATE_STOPPED);
     }