Last active
August 8, 2016 13:40
-
-
Save danhanfry/b971d486b266a7d301e62382dc42839b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.audiomp3; | |
import android.media.MediaPlayer; | |
import android.os.Debug; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.SeekBar; | |
import java.io.IOException; | |
public class MainActivity extends AppCompatActivity { | |
private SeekBar seekBar; | |
private int indexSelect = 0; | |
private int progress[] = new int[2]; | |
MediaPlayer medias[] = new MediaPlayer[2]; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
initializeVariables(); | |
//mediaPlayer.setLooping(true); | |
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { | |
@Override | |
public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) { | |
if(fromUser == true) { | |
progress[indexSelect] = progresValue; | |
final float volume = (float) (1 - (Math.log(100 - progresValue) / Math.log(100))); | |
if (medias[indexSelect] != null) { | |
medias[indexSelect].setVolume(volume, volume); | |
} | |
} | |
} | |
@Override | |
public void onStartTrackingTouch(SeekBar seekBar) {} | |
@Override | |
public void onStopTrackingTouch(SeekBar seekBar) {} | |
}); | |
} | |
private void initializeVariables() { | |
seekBar = (SeekBar) findViewById(R.id.seekBar); | |
} | |
private void audioStop(int index) { | |
if(medias[index]!=null) { | |
if(medias[index].isPlaying()) | |
medias[index].stop(); | |
medias[index].reset(); | |
medias[index].release(); | |
medias[index]=null; | |
} | |
} | |
private void audioStart(int index, int audioFile) { | |
seekBar.setProgress(progress[index]); | |
if (medias[index] == null || !medias[index].isPlaying()){ | |
this.audioStop(index); | |
indexSelect = index; | |
medias[index] = MediaPlayer.create(this, audioFile); | |
medias[index].start(); | |
}else{ | |
this.audioStop(index); | |
} | |
} | |
public void btnStartClick(View view) throws IOException {this.audioStart(0, R.raw.iromi); } | |
public void btnDosClick(View view) throws IOException {this.audioStart(1, R.raw.nicolo); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment