CINXE.COM

Resonance Audio - Android Studio

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en-us"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Resonance Audio - Android Studio</title> <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto+Condensed"> <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto:light,regular,medium,thin,italic,mediumitalic,bold" title="roboto"> <link rel="shortcut icon" type="image/png" href="/resonance-audio/images/favicon.png"> <link rel="stylesheet" href="/resonance-audio/css/default.css"> <link rel="stylesheet" href="/resonance-audio/css/side-menu.css"> <link rel="stylesheet" href="/resonance-audio/css/github-markdown.css"> <link rel="stylesheet" href="/resonance-audio/css/syntax.css"> <link rel="stylesheet" href="/resonance-audio/css/pure-min.css"> <!--[if lte IE 8]> <link rel="stylesheet" href="/resonance-audio/third_party/pure-layout-side-menu/side-menu-old-ie.css"> <![endif]--> <!--[if gt IE 8]><!--> <link rel="stylesheet" href="/resonance-audio/third_party/pure-layout-side-menu/side-menu.css"> <!--<![endif]--> </head> <body> <div id="layout"> <!-- Collapsed menu toggle --> <a href="#menu" id="menuLink" class="menu-link"> <span></span> </a> <!-- Expanded menu --> <div id="menu"> <div class="pure-menu"> <a class="pure-menu-heading" href="/resonance-audio/">Resonance Audio</a> <ul class="pure-menu-list"> <!-- Do nothing --> <li class="pure-menu-item"><a href="/resonance-audio/discover/overview.html" class="pure-menu-link">Discover Resonance Audio</a></li> <li class="pure-menu-item"><a href="/resonance-audio/discover/concepts.html" class="pure-menu-link">Fundamental Concepts</a></li> <li class="pure-menu-item"><a href="/resonance-audio/develop/downloads.html" class="pure-menu-link">Resonance Audio Downloads</a></li> <li class="pure-menu-item"><a href="/resonance-audio/develop/overview.html" class="pure-menu-link">Developing with Resonance Audio</a></li> <li class="pure-menu-item"><a href="/resonance-audio/develop/design-tips.html" class="pure-menu-link">Resonance Audio Design Tips</a></li> </ul> <ul class="pure-menu-list"> <li class="pure-menu-item"> <h2>Getting Started</h2> <ul class="pure-menu-list"> <li class="pure-menu-item pure-menu-selected"><a href="/resonance-audio/develop/android-studio/getting-started.html" class="pure-menu-link">Android Studio</a></li> <li class="pure-menu-item"><a href="/resonance-audio/develop/fmod/getting-started.html" class="pure-menu-link">FMOD</a></li> <li class="pure-menu-item"><a href="/resonance-audio/develop/ios/getting-started.html" class="pure-menu-link">Resonance Audio SDK for iOS</a></li> <li class="pure-menu-item"><a href="/resonance-audio/develop/unity/getting-started.html" class="pure-menu-link">Unity</a></li> <li class="pure-menu-item"><a href="/resonance-audio/develop/unreal/getting-started.html" class="pure-menu-link">Unreal</a></li> <li class="pure-menu-item"><a href="/resonance-audio/develop/vst-monitor/getting-started.html" class="pure-menu-link">Audio Monitor VST</a></li> <li class="pure-menu-item"><a href="/resonance-audio/develop/web/getting-started.html" class="pure-menu-link">Resonance Audio SDK for Web</a></li> <li class="pure-menu-item"><a href="/resonance-audio/develop/wwise/getting-started.html" class="pure-menu-link">Wwise</a></li> </ul> </li> </ul> <ul class="pure-menu-list"> <li class="pure-menu-item"> <h2>Reference</h2> <ul class="pure-menu-list"> <li class="pure-menu-item"><a href="//developers.google.com//vr/reference/android/com/google/vr/sdk/audio/package-summary" class="pure-menu-link">Android SDK Reference</a></li> <li class="pure-menu-item"><a href="/resonance-audio/reference/fmod" class="pure-menu-link">FMOD Reference</a></li> <li class="pure-menu-item"><a href="//developers.google.com/vr/ios/reference" class="pure-menu-link">iOS SDK Reference</a></li> <li class="pure-menu-item"><a href="/resonance-audio/reference/unity" class="pure-menu-link">Unity Reference</a></li> <li class="pure-menu-item"><a href="/resonance-audio/reference/web/ResonanceAudio" class="pure-menu-link">Web Reference</a></li> <li class="pure-menu-item"><a href="/resonance-audio/reference/web/Source" class="pure-menu-link">Web Source Reference</a></li> <li class="pure-menu-item"><a href="/resonance-audio/reference/web/Utils" class="pure-menu-link">Web Utils Reference</a></li> <li class="pure-menu-item"><a href="/resonance-audio/reference/wwise" class="pure-menu-link">Wwise Reference</a></li> </ul> </li> </ul> </div> </div> <!-- Main content --> <div class="markdown-body"> <article> <h1>Android Studio</h1> <p><img srcset="/resonance-audio/images/android/android_hero_image.png 2x" /></p> <p>Get started adding Resonance Audio to your Daydream and Cardboard apps in Android Studio.</p> <p>This guide helps you set up your development environment and tour the Resonance Audio features used in a sample virtual reality app.</p> <p>Note: Resonance Audio is packaged as “Google VR Audio” in the Google VR SDK for Android. Access Resonance Audio capabilities using <code class="language-plaintext highlighter-rouge">GvrAudio</code> components, as shown in this guide.</p> <h2 id="set-up-your-development-environment">Set up your development environment</h2> <p>Hardware requirements:</p> <ul> <li> <p><strong>Daydream:</strong> You’ll need a <a href="//vr.google.com/daydream/smartphonevr/phones/" class="external">Daydream-ready phone</a> and a <a href="//madeby.google.com/vr/" class="external">Daydream View</a>.</p> </li> <li> <p><strong>Cardboard:</strong> You’ll need an Android device running Android 4.4 ‘Kit Kat’ (API level 19) or higher and a <a href="//vr.google.com/cardboard/get-cardboard/" class="external">Cardboard viewer</a>.</p> </li> <li> <p>You’ll need headphones to experience the sample app’s spatial audio.</p> </li> </ul> <p>Software requirements:</p> <ul> <li> <p><a href="//developer.android.com/studio/index.html" class="external">Android Studio</a> version 2.3.3 or higher.</p> </li> <li> <p>Android SDK 7.1.1 ‘Nougat’ (API level 25) or higher.<br />In Android Studio, go to <strong>Preferences</strong> &gt; <strong>Appearance and Behavior ** &gt; **System Settings</strong> &gt; <strong>Android SDK</strong> to review or update installed SDKs.</p> </li> <li> <p><a href="//github.com/googlevr/gvr-android-sdk/releases" class="external">Google VR SDK for Android</a> version 1.80.0 or higher.<br /></p> </li> </ul> <h2 id="open-the-google-vr-sdk-project-in-android-studio">Open the Google VR SDK project in Android Studio</h2> <ol> <li> <p>Extract the downloaded Google VR SDK into a convenient location.</p> </li> <li> <p>Open Android Studio and select <strong>Open an existing Android Studio project</strong>.<br /> Select the directory where you extracted the Google VR SDK.</p> </li> <li> <p>In the <strong>Project</strong> window, find the <strong>sdk-treasurehunt</strong> module in <strong>gvr-android-sdk</strong> &gt; <strong>samples</strong>.</p> <figure> <img src="/resonance-audio/images/android-treasure-hunt-sample.png" width="400" /> </figure> </li> </ol> <h2 id="build-and-run-the-sample-app">Build and run the sample app</h2> <ol> <li> <p>Connect your phone to your machine using a USB cable.</p> </li> <li> <p>In Android Studio, select <strong>Run</strong> &gt; <strong>Run…</strong> and select the <strong>samples-sdk-treasurehunt</strong> target. Android Studio compiles and runs the application on your phone.</p> </li> <li> <p>Put your phone into your viewer and use the app. Make sure to wear headphones to experience the app’s spatial audio.</p> <ul> <li>Look around for the large cube.</li> <li><strong>Daydream:</strong> Point the controller at the cube and press the touchpad button to collect it.<br /> <strong>Cardboard:</strong> Look at the cube and press the Cardboard button to collect it.</li> <li>The cube moves to a new location after a button press.</li> </ul> </li> </ol> <h2 id="how-resonance-audio-works-in-the-sample-app">How Resonance Audio works in the sample app</h2> <p>The Treasure Hunt app uses Resonance Audio to render realistic spatial audio.</p> <p>You can walk through the <code class="language-plaintext highlighter-rouge">TreasureHuntActivity</code> code in the sample app to explore the Resonance Audio API.</p> <h3 id="open-the-source-code-in-android-studio">Open the source code in Android Studio</h3> <ol> <li>To start exploring the code, navigate to <strong>sdk-treasure-hunt</strong> &gt; <strong>src</strong> &gt; <strong>main</strong> &gt; <strong>java</strong> &gt; <strong>com.google.vr.sdk.samples.treasurehunt</strong> &gt; <code class="language-plaintext highlighter-rouge">TreasureHuntActivity</code></li> </ol> <h3 id="initializing-the-audio-engine">Initializing the audio engine</h3> <p>The Resonance Audio engine is initialized in the <code class="language-plaintext highlighter-rouge">TreasureHuntActivity</code> <code class="language-plaintext highlighter-rouge">onCreate()</code> method.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>gvrAudioEngine = new GvrAudioEngine(this, GvrAudioEngine.RenderingMode.BINAURAL_HIGH_QUALITY); </code></pre></div></div> <p>Note that the <a href="https://developers.google.com/vr/android/reference/com/google/vr/sdk/audio/GvrAudioEngine.html#GvrAudioEngine(android.content.Context, int)">audio engine constructor</a> lets you specify a rendering quality mode.</p> <h3 id="pausing-and-resuming-audio">Pausing and resuming audio</h3> <p><code class="language-plaintext highlighter-rouge">TreasureHuntActivity</code> handles app pause or resume events in <code class="language-plaintext highlighter-rouge">onPause()</code> and <code class="language-plaintext highlighter-rouge">onResume</code>. Within these methods, the Resonance Audio engine also pauses and resumes.</p> <h3 id="preloading-sound-files">Preloading sound files</h3> <p>Sound files can be streamed during playback or preloaded into memory before playback.</p> <p>Perform preloading in a separate thread in order to avoid blocking the main thread.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>new Thread( new Runnable() { @Override public void run() { // Preload the sound file gvrAudioEngine.preloadSoundFile(SOUND_FILE); } }) .start(); </code></pre></div></div> <h3 id="creating-positioning-and-playing-audio-for-sound-objects">Creating, positioning, and playing audio for sound objects</h3> <p>In <code class="language-plaintext highlighter-rouge">TreasureHuntActivity</code>, a sound object represents spatial audio for the moving cube that users look for and collect.</p> <p>The same thread used for preloading the sound file in <code class="language-plaintext highlighter-rouge">TreasureHuntActivity</code> is used for setting up the sound object:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>public void onSurfaceCreated(EGLConfig config) { [...] new Thread( new Runnable() { @Override public void run() { // Preload the sound file gvrAudioEngine.preloadSoundFile(SOUND_FILE); // Create the 'sourceId' sound object. // You can create multiple sound objects using the same sound file. sourceId = gvrAudioEngine.createSoundObject(SOUND_FILE); // Set the sound object position relative to the room model. gvrAudioEngine.setSoundObjectPosition( sourceId, modelPosition[0], modelPosition[1], modelPosition[2]); // Start audio playback of the sound object sound file at the // cube model position. You can update the sound object position // whenever the cube moves during run time. gvrAudioEngine.playSound(sourceId, true /* looped playback */); } }) .start(); } </code></pre></div></div> <h3 id="making-the-sound-object-respond-to-model-and-user-head-movements">Making the sound object respond to model and user head movements</h3> <p>In <code class="language-plaintext highlighter-rouge">TreasureHuntActivity</code>, when the cube moves during run time, the sound object that represents the cube audio moves with it.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>protected void updateModelPosition() { [...] // Update the sound location to match the new cube position. if (sourceId != GvrAudioEngine.INVALID_ID) { gvrAudioEngine.setSoundObjectPosition( sourceId, modelPosition[0], modelPosition[1], modelPosition[2]); } </code></pre></div></div> <p>On each frame update, <code class="language-plaintext highlighter-rouge">TreasureHuntActivity</code> gets the latest user head position quaternion and passes it to the audio engine for rendering the sound object’s spatial audio.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>public void onNewFrame(HeadTransform headTransform) { [...] // Update the 3d audio engine with the most recent head rotation. headTransform.getQuaternion(headRotation, 0); gvrAudioEngine.setHeadRotation( headRotation[0], headRotation[1], headRotation[2], headRotation[3]); //Update the GVR Audio engine on each frame update. gvrAudioEngine.update() } </code></pre></div></div> <h2 id="using-the-resonance-audio-sdk-in-your-own-projects">Using the Resonance Audio SDK in your own projects</h2> <p>To use Resonance Audio in your own projects, set up dependencies.</p> <p>If you are using <a href="https://developer.android.com/studio/build/shrink-code.html" class="external">ProGuard</a> in your app, add rules to ensure that it does not obfuscate any SDK code.</p> <h3 id="setting-up-resonance-audio-dependencies">Setting up Resonance Audio dependencies</h3> <ol> <li>Configure your project level <em>build.gradle</em> file: <ul> <li>Make sure that the default <code class="language-plaintext highlighter-rouge">jcenter()</code> repository location is declared.</li> <li> <p>Declare an Android Gradle plugin dependency:<br /> <strong>Google VR SDK projects</strong>: Use <code class="language-plaintext highlighter-rouge">gradle:2.3.3</code> or higher.<br /> <strong>Google VR NDK projects</strong>: Use <code class="language-plaintext highlighter-rouge">gradle-experimental:0.9.3</code> or higher.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> allprojects { repositories { google() jcenter() } } dependencies { // The Google VR SDK requires version 2.3.3 or higher. classpath 'com.android.tools.build:gradle:2.3.3' // The Google VR NDK requires experimental version 0.9.3 or higher. // classpath 'com.android.tools.build:gradle-experimental:0.9.3' } </code></pre></div> </div> </li> </ul> </li> <li> <p>Add Resonance Audio to other dependencies in your module level <em>build.gradle</em> files. As an example, review the <code class="language-plaintext highlighter-rouge">dependencies</code> declared for the Treasure Hunt app in <strong>gvr-android-sdk</strong> &gt; <strong>samples</strong> &gt; <strong>sdk-treasurehunt</strong> &gt; <em>build.gradle</em>.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> dependencies { // Adds Google VR spatial audio support compile 'com.google.vr:sdk-audio:1.80.0' // Required for all Google VR apps compile 'com.google.vr:sdk-base:1.80.0' } </code></pre></div> </div> <p>For more information, see <a href="//developer.android.com/studio/build/dependencies.html" class="external">Add Build Dependencies</a> in the Android Studio guide.</p> </li> </ol> <h3 id="configure-proguard">Configure ProGuard</h3> <p>If you are using <a href="https://developer.android.com/studio/build/shrink-code.html" class="external">ProGuard</a> to minimize your app’s APK file, make sure that ProGuard does not obfuscate any Google VR SDK or NDK code. This makes it easier to debug stack traces in release builds.</p> <p>Add the Google VR ProGuard <a href="https://github.com/googlevr/gvr-android-sdk/blob/master/proguard-gvr.txt" class="external">proguard-gvr.txt</a> rules to your module level <em>build.gradle</em> file:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>android { ... buildTypes { release { minifyEnabled true proguardFiles.add(file('../../proguard-gvr.txt')) } } } </code></pre></div></div> <h2 id="next-steps">Next Steps</h2> <ul> <li> <p>Review the <a href="https://developers.google.com/vr/android/reference/com/google/vr/sdk/audio/package-summary">Resonance Audio API reference documentation</a> for complete details on Resonance Audio capabilities that you can use in your own projects.</p> </li> <li> <p>Get <a href="/resonance-audio/develop/design-tips">design tips for working with Resonance Audio</a>.</p> </li> </ul> </article> </div> </div> <!-- Javascript for collapsing/expanding the menu --> <script src="/resonance-audio/third_party/pure-layout-side-menu/ui.js"></script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10