package com.Android.Jiapaiqi;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
import java.util.Timer;
import java.util.TimerTask;
public class ActivityMain extends Activity {
Button button_start;
Button button_stop;
EditText edit_sudu;
EditText edit_paishu;
TextView result;
SeekBar seekbar;
public float tempo;
public int section;
public int pp;
Handler handler;
Timer mytimer;
private SoundPool sndHigh;
private SoundPool sndLow;
private int hitOfHigh;
private int hitOfLow;
//声音控制
private AudioManager audioManager;
//声音变量
private int volume=0;
//声音模式
//private int mode;
//是否有声音
private int flag=1;
//内部类
class MyTimerTask extends TimerTask{
@Override
public void run() {
Message message=new Message();
message.what=1;
handler.sendMessage(message);
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pp=1;
setContentView(R.layout.main);
final EditText edit_sudu = (EditText)this.findViewById(R.id.edit_sudu);
final EditText edit_paishu = (EditText)this.findViewById(R.id.edit_paishu);
final TextView result = (TextView)findViewById(R.id.result);
handler = new Handler()
{
public void handleMessage(Message msg)
{
switch(msg.what)
{
case 1:
result.setText(String.valueOf(pp));
if(pp==1)
// play (int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)
//播放音频,可以对左右音量分别设置,还可以设置优先级,循环次数以及速率
sndHigh.play(hitOfHigh, 1, 1, 0, 0, 1);
else
sndLow.play(hitOfLow, 1, 1, 0, 0, 1);
if(pp!=section)
{
pp++;
}
else
{
pp=1;
}
break;
}
super.handleMessage(msg);
}
};
button_start = (Button) findViewById(R.id.button_start);
button_start.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tempo=Float.parseFloat(edit_sudu.getText().toString());
section=Integer.parseInt(edit_paishu.getText().toString());
mytimer=new Timer();
float tempFloat=60/tempo*1000;
mytimer.schedule(new MyTimerTask(),0, (long)tempFloat);
}
});
button_stop = (Button) findViewById(R.id.button_stop);
button_stop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mytimer.cancel();
sndHigh.pause(hitOfHigh);
sndLow.pause(hitOfLow);
}
});
final SeekBar seekbar = (SeekBar) findViewById(R.id.seekbar);
//设置拖动条的初始值和文本框的初始值
seekbar.setMax(7);
seekbar.setProgress(5);
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
audioManager = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
//通过getStreamVolume获得当前音量大小
volume=audioManager.getStreamVolume(AudioManager.STREAM_RING);
//把当前音量的值 设置给进度条
//seekbar.setProgress(volume);
sndHigh = new SoundPool(10, AudioManager.STREAM_SYSTEM,5);
//载入音频流
hitOfHigh = sndHigh.load(getBaseContext(), R.drawable.high, 0);
sndLow = new SoundPool(10, AudioManager.STREAM_SYSTEM,5);
//载入音频流
hitOfLow = sndLow.load(getBaseContext(), R.drawable.low, 0);
}
}