I am a newbie.
I'm developing an android (4.2) app to record some audio.
The code seems to run fine the first time, and it even saves the files on the SD Card.
But, if I try to record again, my app crashes.
This is my code:
StringVoiceFile = PathImagePage + "/"+book_id+"-"+pagenumber+".mp3";
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(StringVoiceFile);
this is my function
public void start(){
String PathImagePage = AppDirectory+"/content/"+userid+"/books/"+book_id+"/";
StringVoiceFile = PathImagePage + "/"+book_id+"-"+pagenumber+".mp3";
File VoiceFile = new File(PathImagePage, +"-"+pagenumber+".mp3");
MediaPlayer m = new MediaPlayer();
if (VoiceFile.exists()){
try {
myAudioRecorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
m.setDataSource(StringVoiceFile);
} 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();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
myAudioRecorder.start();
} catch (IllegalStateException e) {
Log.e(TAG, "start() failed = " + e );
}
stop.setEnabled(true);
stop.setVisibility(View.VISIBLE);
time.setEnabled(true);
time.setVisibility(View.VISIBLE);
time.setText(formatTime(m.getDuration()));
play.setEnabled(false);
play.setVisibility(View.INVISIBLE);
long now = System.currentTimeMillis();
startRecordingTimer(now);
Alert.ToastAlert(FlipActivity.this, getResources().getString(R.string.record_start_again));
} else {
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
} catch (IllegalStateException e) {
Alert.ToastAlert(FlipActivity.this, getResources().getString(R.string.record_nostart));
} catch (IOException e) {
Alert.ToastAlert(FlipActivity.this, getResources().getString(R.string.record_nostart));
}
stop.setEnabled(true);
stop.setVisibility(View.VISIBLE);
time.setVisibility(View.VISIBLE);
time.setText(formatTime(m.getDuration()));
play.setEnabled(false);
play.setVisibility(View.INVISIBLE);
long now = System.currentTimeMillis();
startRecordingTimer(now);
Alert.ToastAlert(FlipActivity.this, getResources().getString(R.string.record_start));
}
}
Please help me to solve this issue
Related
I'm trying to play live stream mp3 audio by media player.
The problem is that when I am using URL, it blocks UI thread for sometime then it works fine. But I need that it plays the song with the buffering as well.
Thanks in Advance.Please help.
try {
audioPlayer = new MediaPlayer();
audioPlayer.setOnBufferingUpdateListener(this);
audioPlayer.setOnCompletionListener(this);
audioPlayer.setAudioStreamType(useFrontSpeaker ? AudioManager.STREAM_VOICE_CALL : AudioManager.STREAM_MUSIC);
try {
audioPlayer.reset();
audioPlayer.setDataSource(mSongDetail.getPath());
audioPlayer.prepare();
} 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();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
audioDuration = audioPlayer.getDuration();
audioPlayer.start();
startProgressTimer();
} catch (Exception e) {
if (audioPlayer != null) {
audioPlayer.reset();
audioPlayer.release();
audioPlayer = null;
isPaused = false;
MusicPreferance.playingSongDetail = null;
}
return false;
}
Try to use audioPlayer.prepareAsync() + asyncHandler for it, or use a separate Thread.
I am getting a duration of my audio file , convert it to int , then convert int to string and then string to something like this 00:32 . Converted string is correct, but then is a problem with convert that string to data
Here is my code:
File file = new File(Environment.getExternalStorageDirectory()+"/MyImages/.audio1.wav");
MediaPlayer mp = new MediaPlayer();
FileInputStream fs = null;
FileDescriptor fd = null;
try {
fs = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fd = fs.getFD();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.setDataSource(fd);
} 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();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int length = mp.getDuration()/1000;
mp.release();
String audiotime = String.valueOf(length);
SimpleDateFormat sdf = new SimpleDateFormat("mmss");
try {
Date d = sdf.parse(audiotime);
textView1.setText(audiotime);
} catch (ParseException ex) {
}
Two things, most important, you use the original String as text for your TextView. You should do something like textView1.setText(d.toString());
Secondly, you should use "mm:ss" as your format, instead of "mmss".
Note you could also use the format function of SimpleDateFormat. This will return a StringBuffer, which might be more appropriate since you only need the String representation
source: android documentation
I'm trying to save a custom object when I click a menu item. The problem is its crashing. Before we start, when I try to read that data in another activity, that also crashes. Here is my code where I'm trying to save the data:
Here's a pastebin link.
my write function in the menu
myInfo.setOnMenuItemClickListener(new OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem item)
{
Intent ourIntent = new Intent(Results.this, listTimes.class);
ourIntent.putExtra("Meeting", meetingObj);
try {
fos = new FileOutputStream(filename);
oos = new ObjectOutputStream(fos);
oos.writeObject(businesses.get(positionChecked));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
oos.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//startActivity(ourIntent);
return true;
}
// My read function
private void getData()
{
try {
fis = openFileInput(filename);
ois = new ObjectInputStream(fis);
business = (Business) ois.readObject();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (StreamCorruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
ois.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
error :
Couldn't open file on client side, trying server side
Unable to to create media player
protected void onListItemClick(ListView l, View v, int position, long id) {
//get selected items
String selectedValue = (String) getListAdapter().getItem(position);
try {
MediaPlayer objMediaPlayer = new MediaPlayer();
objMediaPlayer = new MediaPlayer();
objMediaPlayer.setDataSource("http://192.168.1.3:3000/songs/WakaWaka.mp3");
objMediaPlayer.prepare();
objMediaPlayer.start();
} 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();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Check the Android Media Player State Diagram.
You have to call reset() e prepare() before calling start().
Try this:
private MediaPlayer objMediaPlayer = new MediaPlayer();
objMediaPlayer = new MediaPlayer();
try {
objMediaPlayer.setDataSource(path);
objMediaPlayer.prepare();
objMediaPlayer.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
This may help some people.. in order to stream a video from online, you must
add to 'AndroidManifest.xml'
<uses-permission android:name="android.permission.INTERNET" />
i went thru your problem. But couldn't get much of it. One thing for sure is, the url what you have specified here doesn't exist. So I made use of my own and did a sample. Check it out,
mediaPlayer mp=new MediaPlayer();
try {
mp.setDataSource("http://182.71.230.252/developers/blind_willie.mp3");
mp.prepare();
} 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();
}
mp.start();
I use the following code to play audio from sdcard. But it doesn't play, even doesn't throw error. My file is in the following path /sdcard/media/blueeye.mp3. Any suggestions.
public void audioPlayer(){
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(mp.setDataSource(getExternalFilesDir() + "/media/blueeye.mp3"); );
} 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();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
}
I think it should be /mnt/sdcard/media/blueeye.mp3 and please use getExternalFilesDir():
mp.setDataSource(getExternalFilesDir() + "/media/blueeye.mp3");
Also, If you'd set your own onErrorListener, you will be able to see easily what the problem is.