Reputation: 108
I have made an app which uses a MediaRecorder and a MediaPlayer, but whenever I record something and play it, it kinda loops back on itself.
e.g I say, one, two, three, four, five, six, seven, eight, nine, ten
but it plays back as one, two, three three, four four, five five five etc. it gets worse if the recording is longer?
It might be a simple bug, but I don't see it.
Here is my code :
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.CountDownTimer;
import android.os.Environment;
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.CompoundButton;
import android.widget.ProgressBar;
import android.widget.Switch;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
Switch switchPlay;
Button btnRecord, btnDelete;
ProgressBar pBarMetronome;
MediaRecorder mRecorder1, mRecorder2, mRecorder3, mRecorder4, mRecorder5, mRecorder6;
MediaPlayer mp1, mp2, mp3, mp4, mp5, mp6;
String sdPath = "/sdcard/Looper Recordings/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switchPlay = (Switch)findViewById(R.id.switchPlay);
btnRecord = (Button)findViewById(R.id.buttonRecord);
btnDelete = (Button)findViewById(R.id.buttonDelete);
pBarMetronome = (ProgressBar)findViewById(R.id.progressBarMetronome);
clearFile();
createFile();
lineOne();
}
private void playOne(){
mp1 = new MediaPlayer();
Uri myUri = Uri.parse("/sdcard/Looper Recordings/one.mp3");
mp1.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mp1.setDataSource(getApplicationContext(), myUri);
} catch (IOException e) {
e.printStackTrace();
}
try {
mp1.prepare();
Toast.makeText(MainActivity.this, "preparing", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
mp1.start();
Toast.makeText(MainActivity.this, "play!", Toast.LENGTH_SHORT).show();
}
private void recordOne(){
mRecorder1 = new MediaRecorder();
mRecorder1.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder1.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder1.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mRecorder1.setOutputFile(sdPath+"one.mp3");
//mRecorder1.setAudioEncoder(MediaRecorder.getAudioSourceMax());
mRecorder1.setAudioEncodingBitRate(16);
mRecorder1.setAudioSamplingRate(44100);
try {
mRecorder1.prepare();
Toast.makeText(MainActivity.this, "preparing...", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(MainActivity.this, "starting...", Toast.LENGTH_SHORT).show();
mRecorder1.start();
new CountDownTimer(5000,1000){
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
mRecorder1.stop();
Toast.makeText(MainActivity.this, "Stopped!", Toast.LENGTH_SHORT).show();
}
}.start();
}
private void lineOne(){
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recordOne();
}
});
btnRecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new CountDownTimer(4000,1000){
@Override
public void onTick(long millisUntilFinished) {
btnRecord.setText(""+(millisUntilFinished/1000));
playOne();
}
@Override
public void onFinish() {
}
}.start();
}
});
switchPlay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
//ON
}else{
//OFF
}
}
});
}
private void clearFile(){
File dir = new File(Environment.getExternalStorageDirectory()+"/sdcard/Looper Recordings");
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
new File(dir, children[i]).delete();
}
}
}
private void createFile(){
File recordingDirectory = new File(Environment.getExternalStorageDirectory()+ File.separator+"Looper Recordings");
if(!recordingDirectory.exists() && !recordingDirectory.isDirectory())
{
// create empty directory
if (recordingDirectory.mkdirs())
{
Toast.makeText(MainActivity.this, "Folder for recordings created!", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this, "Folder for recordings could not be created?", Toast.LENGTH_SHORT).show();
}
}
else
{
}
}
}
I know it's very messy but I'm still prototyping
Thanks to anyone who is willing to help!
Upvotes: 1
Views: 240
Reputation: 1455
Did you try set looping false.
private void playOne(){
if(mp1 != null && mp1.isPlaying()){
{
mp1.stop();
}
mp1 = new MediaPlayer();
mp1.setLooping(false);
Uri myUri = Uri.parse("/sdcard/Looper Recordings/one.mp3");
mp1.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mp1.setDataSource(getApplicationContext(), myUri);
} catch (IOException e) {
e.printStackTrace();
}
try {
mp1.prepare();
Toast.makeText(MainActivity.this, "preparing", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
mp1.start();
Toast.makeText(MainActivity.this, "play!", Toast.LENGTH_SHORT).show();
}
Upvotes: 1
Reputation: 56
It seems you are initializing MediaPlayer multiple times and play them simeltenously
Upvotes: 1