Share with you the problems encountered in the project
Development Environment: Android Studio
Classes used in the project (key): SoundPool
Project github address: https://github.com/zw21544182/BtMusicDemo
Key knowledge used in the project:
1. Base Class Extraction (abstract)BaseActivity
2. Singleton mode - SoundPoolUtil
3.SoundPool class Api application
The end result is not stickable because it involves sound
Let's start with SoundPool
Constructor: SoundPool (int maxStreams, int streamType, int srcQuality)
Explanation of parameters:
maxStreams | streamType | srcQuality |
---|---|---|
Maximum number of streams | Stream type recommendation AudioManager.STREAM_SYSTEM | Frequency quality, 0 by default, currently unaffected |
Load audio: load(Context context, int resId, int priority)
Explanation of parameters:
context | resId | priority |
---|---|---|
Context object (not bb) | Resource ID (used to specify audio file location) | Priority: All short audio has no effect set to 1 |
Play audio: play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)
Explanation of parameters:
soundID | leftVolume | rightVolume | priority | loop | rate |
---|---|---|---|---|---|
Audio Sequence Number (order of load to SoundPool, starting from 1) | Volume control for left\right channel, 0.0 to 1.0 | Priority, 0 is the lowest priority | Priority, 0 is the lowest priority | Whether to play in a loop, 0 is not loop, -1 is loop | Play ratio, from 0.5 to 2, generally 1, indicating normal play |
Because the layout is simple, no code will be pasted
We can initialize the SoundPool class with the singleton mode:The code is as follows:
package zw.btmusicdemo.util;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;
import zw.btmusicdemo.R;
/**
* Created by zhongwang on 2017/9/8.
*/
public class SoundPoolUtil {
private static SoundPoolUtil soundPoolUtil;
private SoundPool soundPool;
//Singleton mode
public static SoundPoolUtil getInstance(Context context) {
if (soundPoolUtil == null)
soundPoolUtil = new SoundPoolUtil(context);
return soundPoolUtil;
}
private SoundPoolUtil(Context context) {
soundPool = new SoundPool(3, AudioManager.STREAM_SYSTEM, 0);
//Load audio files
soundPool.load(context, R.raw.music1, 1);
soundPool.load(context, R.raw.music2, 1);
soundPool.load(context, R.raw.music3, 1);
}
public void play(int number) {
Log.d("tag", "number " + number);
//Play Audio
soundPool.play(number, 1, 1, 0, 0, 1);
}
}
The second step extracts the base class BaseActivity
1. Restrict audio playback by enumeration
2. Let click methods be implemented in subclasses through abstract
3. Control which audio to play in the subclass click method
package zw.btmusicdemo.base;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Toast;
import zw.btmusicdemo.util.SoundPoolUtil;
/**
* Created by zhongwang on 2017/9/8.
* You can set the base class activity for button click sound effect
*/
public abstract class BaseActivity extends Activity implements View.OnClickListener {
private SoundPoolUtil soundPoolUtil;
private int soundId = 0;
//Play music 1 by default
private MusicType musicType = MusicType.FIRST;
/**
* Set click button music type
*
* @param musicType FIRST SECOND THIRD Three parameters
*/
public void setMusicType(MusicType musicType) {
this.musicType = musicType;
}
/**
* Define enumeration to limit button music type
*/
public enum MusicType {
FIRST, SECOND, THIRD
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
soundPoolUtil = SoundPoolUtil.getInstance(this);
}
/**
* This method calls setMusicType to control clicking music type
* @param v
*/
public abstract void click(View v);
@Override
public void onClick(View v) {
click(v);
switch (musicType) {
case FIRST:
soundId = 1;
break;
case SECOND:
soundId = 2;
break;
case THIRD:
soundId = 3;
break;
}
soundPoolUtil.play(soundId);
}
public void toast(String content) {
Toast.makeText(this, content, Toast.LENGTH_SHORT).show();
}
}
Finally, the application in subclass MainActivity
package zw.btmusicdemo.activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import zw.btmusicdemo.R;
import zw.btmusicdemo.base.BaseActivity;
public class MainActivity extends BaseActivity {
private Button button1;
private Button button2;
private Button button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
@Override
public void click(View v) {
switch (v.getId()) {
case R.id.button1:
setMusicType(MusicType.FIRST);
toast("Button 1 clicked");
break;
case R.id.button2:
setMusicType(MusicType.SECOND);
toast("Button 2 clicked");
break;
case R.id.button3:
setMusicType(MusicType.THIRD);
toast("Button 3 clicked");
break;
}
}
}