1: Play the music in the application
Basic Layout Documents
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/seekBar"
android:layout_weight="1" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ib_main_imageButton"
android:src="@android:drawable/ic_media_play"/>
java code
public class MainActivity extends AppCompatActivity {
private SeekBar seekBar;
private ImageButton ib_main_imageButton;
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBar);
ib_main_imageButton = (ImageButton) findViewById(R.id.ib_main_imageButton);
//Setting events for progress bars
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//Get dragged events
int progress=seekBar.getProgress();
mediaPlayer.seekTo(progress);
}
});
ib_main_imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer == null) {
//Instantiate MediaPlayer
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.music);
mediaPlayer.start();
//Modify icon to pause icon
ib_main_imageButton.setImageResource(android.R.drawable.ic_media_pause);
//How long it took to get music
int duration=mediaPlayer.getDuration();
//Set the maximum value for the progress bar
seekBar.setMax(duration);
new MyThread().start();
}else if(mediaPlayer.isPlaying()) {
mediaPlayer.pause();
//Modify the icon to play icon
ib_main_imageButton.setImageResource(android.R.drawable.ic_media_play);
}else{
mediaPlayer.start();
//Modify icon to pause icon
ib_main_imageButton.setImageResource(android.R.drawable.ic_media_pause);
}
}
});
}
class MyThread extends Thread{
@Override
public void run() {
super.run();
while(seekBar.getProgress()<=seekBar.getMax()){
//Get the current location of the playing music
int currentPosition=mediaPlayer.getCurrentPosition();
//Get the progress bar moving
seekBar.setProgress(currentPosition);
}
}
}
}
2 Play sd Card Files
You need to configure permissions in the manifest file
<! - The right to read memory cards - > <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
You can't use Mediaplay. create to specify paths as you would in-app playback.
Here we have to prepare, because we have to find the way.
You also need to set up ready listening
The code is as follows:
mediaPlayer=new MediaPlayer();
//Setting the type of audio stream
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//Setting sound source
//Play audio files in memory card
mediaPlayer.setDataSource(this, Uri.parse("file://mnt/sdcard/Music/f.mp3"));
//Get ready (memory card)
mediaPlayer.prepare();
//Setting up ready listening
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
imageButton.setImageResource(android.R.drawable.ic_media_pause);
//How long does it take to get the music playing?
int duration=mediaPlayer.getDuration();
//Set the maximum value of the progress bar: the playing time of the music
seekBar.setMax(duration);
new MyThread().start();
}
});
}else if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
imageButton.setImageResource(android.R.drawable.ic_media_play);
}else{
mediaPlayer.start();
imageButton.setImageResource(android.R.drawable.ic_media_pause);
}
}
3 Play files on the network
Also configure the permissions of networking in the manifest file:
<! - Internet access - > <uses-permission android:name="android.permission.INTERNET"/>
Just prepare to change to asynchrony
The code is as follows:
mediaPlayer=new MediaPlayer();
//Setting the type of audio stream
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//Playing Network Music
mediaPlayer.setDataSource(this, Uri.parse("http://192.168.43.135/internet.mp3"));
//Prepare (Asynchronous Prepare)
mediaPlayer.prepareAsync();
//Setting up ready listening
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
imageButton.setImageResource(android.R.drawable.ic_media_pause);
//How long does it take to get the music playing?
int duration=mediaPlayer.getDuration();
//Set the maximum value of the progress bar: the playing time of the music
seekBar.setMax(duration);
new MyThread().start();
}
});
}else if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
imageButton.setImageResource(android.R.drawable.ic_media_play);
}else{
mediaPlayer.start();
imageButton.setImageResource(android.R.drawable.ic_media_pause);
}
}
4 Audio pool
It can play more than one music, but not more than one Mega each.
Layout file
<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Sound effects KFC" android:onClick="playKFC"/>
<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Sound effects Two" android:onClick="playTwo"/>
<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Sound effects Three" android:onClick="playThree"/>
<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Sound effects Four" android:onClick="playFour"/>
<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Sound effects Dog" android:onClick="playDog"/>
Note the parameters created and played.
Created parameters
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC,0);
The first parameter is how many sounds to play at most.
The second parameter is the type of sound.
The third parameter is playback quality (usually 0)
Play parameters
soundPool.play(i,1,1,1,-1,1);
id of the first counselor's voice
The second is the left vocal tract (1 is voiced, 0 is silent).
The third parameter is the right vocal tract (generally 1)
The fourth parameter is priority (generally set to 1)
The fifth parameter is the cycle (- 1 cycle, 0 does not cycle, 0-n is the number of cycles)
The sixth parameter is the rate.
The code is as follows:
public class SoundPoolActivity extends AppCompatActivity {
private SoundPool soundPool;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sound_pool);
//Instantiate audio pool
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC,0);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int i, int i1) {
//play
soundPool.play(i,1,1,1,-1,1);
}
});
}
public void playKFC(View view){
soundPool.load(this,R.raw.rooster,1);
}
public void playTwo(View view){
soundPool.load(this,R.raw.chimp,1);
}
public void playThree(View view){
soundPool.load(this,R.raw.crickets,1);
}
public void playFour(View view){
soundPool.load(this,R.raw.roar,1);
}
public void playDog(View view){
soundPool.load(this,R.raw.dogbark,1);
}
@Override
protected void onDestroy() {
super.onDestroy();
if(soundPool!=null){
soundPool.release();
soundPool=null;
}
}
}