1/*
2 * Copyright (C) 2007 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
17package com.android.tests.soundtest;
18
19import android.app.Activity;
20import android.content.Context;
21import android.os.Bundle;
22import android.os.HandlerInterface;
23import android.os.Message;
24import android.view.View;
25import android.view.ViewGroup;
26import android.view.Window;
27import android.view.View.OnClickListener;
28import android.view.ViewGroup.LayoutParams;
29import android.widget.Button;
30import android.widget.LinearLayout;
31
32import java.io.InputStream;
33
34import javax.sound.midi.MidiSystem;
35import javax.sound.midi.Sequencer;
36import javax.sound.sampled.AudioFormat;
37import javax.sound.sampled.AudioInputStream;
38import javax.sound.sampled.AudioSystem;
39import javax.sound.sampled.DataLine;
40import javax.sound.sampled.SourceDataLine;
41
42
43public class SoundTest extends Activity implements HandlerInterface {
44
45    public Context mContext;
46    private LinearLayout mLinearLayout;
47    public LayoutParams mParams;
48    Button button, button2;
49
50    public void onCreate(Bundle icicle) {
51        super.onCreate(icicle);
52
53        Window wp = getWindow();
54        mContext = wp.getContext();
55        mParams = wp.getAttributes();
56
57        mLinearLayout = new LinearLayout(this);
58        mLinearLayout.setOrientation(LinearLayout.VERTICAL);
59        setContentView(mLinearLayout);
60
61        button = new Button(mContext);
62        button.setMinimumWidth(300);
63        button.setMinimumHeight(70);
64        button.setTextSize(14);
65        button.setText("Play sample");
66        button.setOnClickListener(buttonListener);
67
68        mLinearLayout.addView(button, new LinearLayout.LayoutParams(
69            ViewGroup.LayoutParams.WRAP_CONTENT,
70            ViewGroup.LayoutParams.WRAP_CONTENT));
71
72        button2 = new Button(mContext);
73        button2.setMinimumWidth(300);
74        button2.setMinimumHeight(70);
75        button2.setTextSize(14);
76        button2.setText("Play MIDI");
77        button2.setOnClickListener(buttonListener2);
78
79        mLinearLayout.addView(button2, new LinearLayout.LayoutParams(
80            ViewGroup.LayoutParams.WRAP_CONTENT,
81            ViewGroup.LayoutParams.WRAP_CONTENT));
82
83    }
84
85    private OnClickListener buttonListener = new OnClickListener() {
86        public void onClick(View v) {
87            try {
88                button.setText(button.getText() + ".");
89
90                int RENDER_BUFF_SIZE = 1024*48;
91
92                InputStream is = getAssets().open("fx_foghorn.mp3");
93
94                AudioInputStream ais = null;
95
96                ais = AudioSystem.getAudioInputStream(is);
97
98                AudioFormat af = ais.getFormat();
99                SourceDataLine sdl = null;
100                DataLine.Info dli = new DataLine.Info(SourceDataLine.class, af);
101                sdl = (SourceDataLine)AudioSystem.getLine(dli);
102
103                sdl.open(af);
104                sdl.start();
105
106                int bytesReaded = 0;
107                byte samplesBuff[] = new byte[RENDER_BUFF_SIZE];
108
109                while (bytesReaded != -1) {
110                    bytesReaded = ais.read(samplesBuff, 0, samplesBuff.length);
111                    if (bytesReaded > 0) {
112                        sdl.write(samplesBuff, 0, bytesReaded);
113                    }
114                }
115
116                sdl.drain();
117                sdl.close();
118            } catch (Exception ee) {
119                ee.printStackTrace();
120            }
121        }
122    };
123
124    private OnClickListener buttonListener2 = new OnClickListener() {
125        public void onClick(View v) {
126            try {
127                button2.setText(button2.getText() + ".");
128
129                int RENDER_BUFF_SIZE = 1024*48;
130
131                InputStream is = getAssets().open("Dancing_Queen.mid");
132
133                Sequencer s = MidiSystem.getSequencer();
134                s.open();
135                s.setSequence(is);
136                s.setLoopCount(1);
137                s.start();
138
139            } catch (Exception ee) {
140                ee.printStackTrace();
141            }
142        }
143    };
144
145    public void handleMessage(Message arg0) {
146    }
147}
148