package com.free.study;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class StudyAndroidActivity extends Activity {
Button btn1;
Button btn2;
SoundPool soundPool;
HashMap<Integer, Integer> soundMap ;
float leftVolume =1.0f, rightVolume =1.0f;
int priority =1, loop = 0;
float rate =1.0f;//正常速率
Button btn3;
Button btn4;
MediaPlayer mediaPlayer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button)findViewById(R.id.button1);
btn2 = (Button)findViewById(R.id.button2);
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundMap = new HashMap<Integer, Integer>();
soundMap.put(1, soundPool.load(this, R.raw.aaa, 1));
AssetFileDescriptor descriptor;
try {
descriptor = this.getResources().getAssets().openFd("abc.wav");
soundMap.put(2, soundPool.load(descriptor, 1 ));
} catch (IOException e) {
e.printStackTrace();
}
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(1);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(2);
}
});
btn3 = (Button)findViewById(R.id.button3);
btn4 = (Button)findViewById(R.id.button4);
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playMedia(3);
}
});
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playMedia(4);
}
});
}
private void playSound(int soundID){
// AudioManager am = (AudioManager)this.getSystemService(this.AUDIO_SERVICE);
// float audioMaxVolumn = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
// float audioCurrentVolumn = am.getStreamVolume(AudioManager.STREAM_MUSIC);
// float volumnRatio = audioCurrentVolumn/audioMaxVolumn;
int streamID =soundPool.play(soundMap.get(soundID), leftVolume, rightVolume, priority, loop, rate);
if(streamID == 0){
//播放失败
Toast.makeText(this, "fail", Toast.LENGTH_SHORT).show();
}else{
//播放成功
Toast.makeText(this, "ok", Toast.LENGTH_SHORT).show();
}
}
private void playMedia(int soundID){
if(mediaPlayer!=null){
mediaPlayer.stop();
mediaPlayer.reset();
}
if(soundID == 3){
mediaPlayer = MediaPlayer.create(this,
R.raw.aaa);
mediaPlayer.start();
}
else if(soundID == 4){
try {
AssetManager assetManager = this.getAssets();
AssetFileDescriptor fileDesc = assetManager.openFd("abc.wav");
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(fileDesc.getFileDescriptor(),
fileDesc.getStartOffset(),
fileDesc.getLength()
);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//TODO 资源释放
}
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class StudyAndroidActivity extends Activity {
Button btn1;
Button btn2;
SoundPool soundPool;
HashMap<Integer, Integer> soundMap ;
float leftVolume =1.0f, rightVolume =1.0f;
int priority =1, loop = 0;
float rate =1.0f;//正常速率
Button btn3;
Button btn4;
MediaPlayer mediaPlayer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button)findViewById(R.id.button1);
btn2 = (Button)findViewById(R.id.button2);
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundMap = new HashMap<Integer, Integer>();
soundMap.put(1, soundPool.load(this, R.raw.aaa, 1));
AssetFileDescriptor descriptor;
try {
descriptor = this.getResources().getAssets().openFd("abc.wav");
soundMap.put(2, soundPool.load(descriptor, 1 ));
} catch (IOException e) {
e.printStackTrace();
}
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(1);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(2);
}
});
btn3 = (Button)findViewById(R.id.button3);
btn4 = (Button)findViewById(R.id.button4);
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playMedia(3);
}
});
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playMedia(4);
}
});
}
private void playSound(int soundID){
// AudioManager am = (AudioManager)this.getSystemService(this.AUDIO_SERVICE);
// float audioMaxVolumn = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
// float audioCurrentVolumn = am.getStreamVolume(AudioManager.STREAM_MUSIC);
// float volumnRatio = audioCurrentVolumn/audioMaxVolumn;
int streamID =soundPool.play(soundMap.get(soundID), leftVolume, rightVolume, priority, loop, rate);
if(streamID == 0){
//播放失败
Toast.makeText(this, "fail", Toast.LENGTH_SHORT).show();
}else{
//播放成功
Toast.makeText(this, "ok", Toast.LENGTH_SHORT).show();
}
}
private void playMedia(int soundID){
if(mediaPlayer!=null){
mediaPlayer.stop();
mediaPlayer.reset();
}
if(soundID == 3){
mediaPlayer = MediaPlayer.create(this,
R.raw.aaa);
mediaPlayer.start();
}
else if(soundID == 4){
try {
AssetManager assetManager = this.getAssets();
AssetFileDescriptor fileDesc = assetManager.openFd("abc.wav");
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(fileDesc.getFileDescriptor(),
fileDesc.getStartOffset(),
fileDesc.getLength()
);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//TODO 资源释放
}