How to record sound with music playing in background - android

In my application, i have to record sound with music. When i am clicking on record button , music starts and recording will starting too. User will sing the song and this song will record with music .
In my code song is not recording with music. it records only sound of user. Kindly check my code and suggest me.
recorder = new MediaRecorder();
record_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
mPlayer = MediaPlayer.create(RecordVoiceActivity.this, R.raw.intro);
mPlayer.start();
long dtMili = System.currentTimeMillis();
primarySongName=""+dtMili;
// Date dt = new Date(dtMili);
Toast.makeText(RecordVoiceActivity.this,
"Recording starting", Toast.LENGTH_LONG).show();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
currentDateTimeString = DateFormat.getDateInstance()
.format(new Date());
Log.d("##################################", "" + dtMili);
recorder.setOutputFile("/sdcard/Recordedsong_" + dtMili + ".3gp");
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
stop_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
// TODO Auto-generated method stub
Toast.makeText(RecordVoiceActivity.this,
"Recording stopped", Toast.LENGTH_LONG).show();
mPlayer.stop();
recorder.stop();
recorder.release();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
replay_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
// TODO Auto-generated method stub
Log.d("############################", currentDateTimeString);
Uri path = Uri.parse(Environment.getExternalStorageDirectory()
+ "/" + "Recordedsong_" + primarySongName
+ ".3gp");
MediaPlayer player = MediaPlayer.create(
RecordVoiceActivity.this, path);
player.setLooping(false);
player.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
In this code , M recording the song and saving in my Sd card. Then replaying from Sdcard .
But song is recording without music which i am playing.

Related

3 audios at the same time with Android MediaPlayer

I am trying to play 3 audios width Android but not at the same time, my code is the next one:
for (Word w : wordsList){
String path = Parameters.PATH_AUDIO + "/" + w.getAudioPath();
Uri audioPath = Uri.parse(path);
MediaPlayer audio1 = MediaPlayer.create(context, audioPath);
audio1.start();
}
But all audios start to play at least at the same time. I need something like audio1.wait() or audio1.sleep or someone like this.
You might want to consider using setOnCompletionListener:
audio1.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
public void onCompletion(MediaPlayer player) {
//do something when audio is finished
}});
Here's an example (didn't have the chance to try it though):
Decalre these in the class level:
int index = 0;
MediaPlayer audio1;
and then:
try {
audio1 = MediaPlayer.create(this, Uri.parse(yourList.get(index)));
audio1.start();
index++;
audio1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer player) {
if (index < yourList.size()) {
audio1.reset();
audio1 = MediaPlayer.create(MainActivity.this,
Uri.parse(yourList.get(index)));
audio1.start();
index++;
}
else
{
//release it
}
}
});
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Streaming audio from website to app

I'm in the process of building a prototype for a project. I want to be able to stream music from my website to the app.
Here is what I have so far. Trying it with an m3u because I honestly have no idea what else to use for this.
Button buttonPlay;
Button buttonStop;
MediaPlayer mPlayer;
String url = "http://www.*******.com/*****/files.m3u";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonPlay = (Button) findViewById(R.id.play);
buttonPlay.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mPlayer.setDataSource(url);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Illegal Argument", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Security ", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Illegal Statement", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Prepare Illegal State", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Prepare IO", Toast.LENGTH_LONG).show();
}
mPlayer.start();
}
});
buttonStop = (Button) findViewById(R.id.stop);
buttonStop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(mPlayer!=null && mPlayer.isPlaying()){
mPlayer.stop();
}
}
});
Them problem is it throws IOexception after prepare. Now I read somewhere you have to download m3u files and then read them line by line. If that is the case, is there a better way? I'm attempting to make sort of a radio app for testing purposes that will play songs at random, not in order, so I don't want to hard code them MP3 files,
you cant just prepare a stream because it needs to download asynchronously. you will need to do this
Button buttonPlay;
Button buttonStop;
MediaPlayer mPlayer;
String url = "http://www.*******.com/*****/files.m3u";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonPlay = (Button) findViewById(R.id.play);
buttonPlay.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mPlayer.setDataSource(url);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Illegal Argument", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Security ", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Illegal Statement", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.prepareAsync();
mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mPlayer.start();
}
};);
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Prepare Illegal State", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Prepare IO", Toast.LENGTH_LONG).show();
}
}
});
buttonStop = (Button) findViewById(R.id.stop);
buttonStop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(mPlayer!=null && mPlayer.isPlaying()){
mPlayer.stop();
}
}
});

how to disable sound in my game app?

Now i develop a game application and as all games on google play, they have a toggle button to switch on/off the sound.
Is there any technique i can use to disable all sounds in the game with one step or i have to disable every sound separately ??
I use MediaPlayer for playing sounds
private int playSound(int file) {
int duration = 1000;
sound = new MediaPlayer();
AssetFileDescriptor fd = getResources().openRawResourceFd(file);
try {
sound.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
sound.prepare();
sound.start();
sound.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// Do the work after completion of audio
Toast.makeText(GameActivity.this, "good", Toast.LENGTH_SHORT).show();
mp.release();
}
});
duration = sound.getDuration();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return duration;
}
try this
media.setVolume(0,0);

unable to stop Audio Recording,Thread not working properly and application stops unfortunately

I am making a sample program for recording audio.I have a button for starting and stopping the recording.I am running a thread so that i can record until 30 sec.My problem is when before reaching to 30sec ,if i click on stopButton ,my application stops unfortunately.Now i am not getting the problem.
Following is my code:
public class AudioRecordingActivity extends Activity {
private Button RecordButton;
boolean recording;
MediaRecorder recorder ;
Thread RecordThread;
Handler recordHandler;
private TextView timeText;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timeText=(TextView)findViewById(R.id.textView1);
RecordButton=(Button)findViewById(R.id.btnRecord);
RecordButton.setText("Start Recording");
RecordButton.getBackground().setColorFilter(new LightingColorFilter(-16711936, 0));
recording=false;
RecordButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (recording)
{
recording=false;
stopRecording();
RecordButton.setText("Start Recording");
RecordButton.getBackground().setColorFilter(new LightingColorFilter(-16711936, 0));
// Toast.makeText(getApplicationContext(), "Recording Stopped", Toast.LENGTH_LONG).show();
}//if(recording)
else if(!recording) {
recording=true;
startRecording();
RecordButton.setText("Stop Recording");
RecordButton.getBackground().setColorFilter(new LightingColorFilter(-65536, 0));
// Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
recordHandler = new Handler();
RecordThread=new Thread(new Runnable() {
int j;
#Override
public void run() {
// TODO Auto-generated method stub
for( j=30;j>=0;j--){
if(recording){
recordHandler.post(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
setTimer(j);
}//handler run
});//post
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//catch
}else if (!recording)
{
stopRecording();
}
}//for
}//run
});
RecordThread.start();
}
}
});
}//onCreate
protected void startRecording() {
// TODO Auto-generated method stub
Random generator = new Random();
int n = 1000;
n = generator.nextInt(n);
File folder = new File(Environment.getExternalStorageDirectory().toString()+"/AudioRecord");
if(!folder.exists())
{
folder.mkdirs();
}
String path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/AudioRecord/audiorecord"+n+".3gp";
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setOutputFile(path);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
Toast.makeText(getApplicationContext(),"Saving Audio as"+ path,
Toast.LENGTH_LONG).show();
try {
recorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "IllegalStateException called", Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "prepare() failed", Toast.LENGTH_LONG).show();
}
recorder.start();
}
private void stopRecording() {
recorder.stop();
recorder.release();
recorder = null;
}
private void setTimer(int j) {
// TODO Auto-generated method stub
((TextView)findViewById(R.id.textView1)).setText(String.valueOf(j) + "sec");
if(j==0){
recording=false;
}
}
}//class end
Audio is been recorded perfectly.I guess the problem is with stopping the audio and thread.I don't know where the correction are to be made.
THanks for the help in advance.
Problem solved: Following corrections were made..
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (recording)
{
if(recorder!=null){
recorder.stop();
recorder.release();
recorder = null;
}
recording=false;
RecordButton.setText("Start Recording");
RecordButton.getBackground().setColorFilter(new LightingColorFilter(-16711936, 0));
// Toast.makeText(getApplicationContext(), "Recording Stopped", Toast.LENGTH_LONG).show();
}
...
private void setTimer(int j) {
// TODO Auto-generated method stub
timeText.setText(String.valueOf(j) + "sec");
if ((recording) && (j==1)){
recorder.stop();
recorder.release();
recorder = null;
recording=false;
RecordButton.setText("Start Recording");
RecordButton.getBackground().setColorFilter(new LightingColorFilter(-16711936, 0));
}
}

How can I repeat audio without stopping it?

I want to repeat playing audio. so if first clicking button audio play, and second clicking same button repeat same audio without stopping. How can I do this? Here's what I've tried:
if (mp != null){
mp.stop();
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.seekTo(0);
mp = null;
}
else {
mp = MediaPlayer.create(Takbiratul_Ihram.this, R.raw.takbir_cowok);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener(){
public void onCompletion (MediaPlayer arg0){
mp = null;
}
});
mediaPlayer.setLooping(true);
More information here here

Categories

Resources