• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.nativeaudio;
18 
19 import android.app.Activity;
20 import android.content.res.AssetManager;
21 import android.os.Bundle;
22 import android.view.View;
23 import android.view.View.OnClickListener;
24 import android.widget.Button;
25 
26 public class NativeAudio extends Activity {
27 
28     static final int CLIP_NONE = 0;
29     static final int CLIP_HELLO = 1;
30     static final int CLIP_ANDROID = 2;
31     static final int CLIP_SAWTOOTH = 3;
32     static final int CLIP_PLAYBACK = 4;
33 
34     static final String URI = "http://upload.wikimedia.org/wikipedia/commons/6/6d/Banana.ogg";
35     static AssetManager assetManager;
36 
37     static boolean isPlayingAsset = false;
38     static boolean isPlayingUri = false;
39 
40     /** Called when the activity is first created. */
41     @Override
onCreate(Bundle icicle)42     protected void onCreate(Bundle icicle) {
43         super.onCreate(icicle);
44         setContentView(R.layout.main);
45 
46         assetManager = getAssets();
47 
48         // initialize native audio system
49 
50         createEngine();
51         createBufferQueueAudioPlayer();
52 
53         // initialize button click handlers
54 
55         ((Button) findViewById(R.id.hello)).setOnClickListener(new OnClickListener() {
56             public void onClick(View view) {
57                 // ignore the return value
58                 selectClip(CLIP_HELLO, 5);
59             }
60         });
61 
62         ((Button) findViewById(R.id.android)).setOnClickListener(new OnClickListener() {
63             public void onClick(View view) {
64                 // ignore the return value
65                 selectClip(CLIP_ANDROID, 7);
66             }
67         });
68 
69         ((Button) findViewById(R.id.sawtooth)).setOnClickListener(new OnClickListener() {
70             public void onClick(View view) {
71                 // ignore the return value
72                 selectClip(CLIP_SAWTOOTH, 1);
73             }
74         });
75 
76         ((Button) findViewById(R.id.reverb)).setOnClickListener(new OnClickListener() {
77             boolean enabled = false;
78             public void onClick(View view) {
79                 enabled = !enabled;
80                 if (!enableReverb(enabled)) {
81                     enabled = !enabled;
82                 }
83             }
84         });
85 
86         ((Button) findViewById(R.id.embedded_soundtrack)).setOnClickListener(new OnClickListener() {
87             boolean created = false;
88             public void onClick(View view) {
89                 if (!created) {
90                     created = createAssetAudioPlayer(assetManager, "background.mp3");
91                 }
92                 if (created) {
93                     isPlayingAsset = !isPlayingAsset;
94                     setPlayingAssetAudioPlayer(isPlayingAsset);
95                 }
96             }
97         });
98 
99         ((Button) findViewById(R.id.uri_soundtrack)).setOnClickListener(new OnClickListener() {
100             boolean created = false;
101             public void onClick(View view) {
102                 if (!created) {
103                     created = createUriAudioPlayer(URI);
104                 }
105                 if (created) {
106                     isPlayingUri = !isPlayingUri;
107                     setPlayingUriAudioPlayer(isPlayingUri);
108                 }
109              }
110         });
111 
112         ((Button) findViewById(R.id.record)).setOnClickListener(new OnClickListener() {
113             boolean created = false;
114             public void onClick(View view) {
115                 if (!created) {
116                     created = createAudioRecorder();
117                 }
118                 if (created) {
119                     startRecording();
120                 }
121             }
122         });
123 
124         ((Button) findViewById(R.id.playback)).setOnClickListener(new OnClickListener() {
125             public void onClick(View view) {
126                 // ignore the return value
127                 selectClip(CLIP_PLAYBACK, 3);
128             }
129         });
130 
131     }
132 
133     /** Called when the activity is about to be destroyed. */
134     @Override
onPause()135     protected void onPause()
136     {
137         // turn off all audio
138         selectClip(CLIP_NONE, 0);
139         isPlayingAsset = false;
140         setPlayingAssetAudioPlayer(false);
141         isPlayingUri = false;
142         setPlayingUriAudioPlayer(false);
143         super.onPause();
144     }
145 
146     /** Called when the activity is about to be destroyed. */
147     @Override
onDestroy()148     protected void onDestroy()
149     {
150         shutdown();
151         super.onDestroy();
152     }
153 
154     /** Native methods, implemented in jni folder */
createEngine()155     public static native void createEngine();
createBufferQueueAudioPlayer()156     public static native void createBufferQueueAudioPlayer();
createAssetAudioPlayer(AssetManager assetManager, String filename)157     public static native boolean createAssetAudioPlayer(AssetManager assetManager, String filename);
setPlayingAssetAudioPlayer(boolean isPlaying)158     public static native void setPlayingAssetAudioPlayer(boolean isPlaying);
createUriAudioPlayer(String uri)159     public static native boolean createUriAudioPlayer(String uri);
setPlayingUriAudioPlayer(boolean isPlaying)160     public static native void setPlayingUriAudioPlayer(boolean isPlaying);
selectClip(int which, int count)161     public static native boolean selectClip(int which, int count);
enableReverb(boolean enabled)162     public static native boolean enableReverb(boolean enabled);
createAudioRecorder()163     public static native boolean createAudioRecorder();
startRecording()164     public static native void startRecording();
shutdown()165     public static native void shutdown();
166 
167     /** Load jni .so on initialization */
168     static {
169          System.loadLibrary("native-audio-jni");
170     }
171 
172 }
173