I am writing an application that uses MediaRecorder for recording audio. ( I use Android 2.1 )
Before start recording I want to know the microphone's state (is it busy or no)?
If the microphone busy, can I get exclusive access to it?
Thanks in advance!
Answer to 1 question:
After reading many articles I realized that it was impossible.
It's an old question but I show the way I solve this problem. Maybe someone will need this help. Note: it's a dirty way but it worked for me.
You can try/catch MediaRecorderObject.start(). You will get an exception if the mic is busy
MediaRecorder myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputFile);
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
}
catch (Exception exception) {
Toast.makeText(getApplicationContext(), "Mic in use", Toast.LENGTH_LONG).show();
}
Related
In my application there is a feature to record voice calls and it works perfectly well. But when tested on (Samsung s7, s8 ) it doesn’t work well. The application is able to record only callers voice not the voice from the other end. Below is my code to check please suggest a solution
MediaRecd = new MediaRecorder();
MediaRecd.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL
);
MediaRecd.setAudioChannels(ConstantVariables.audioChannels);//monoRecording
MediaRecd.setAudioEncodingBitRate(64);
MediaRecd.setAudioSamplingRate(44100);
MediaRecd.setOutputFormat(output_formats[pos]);//.mp3
MediaRecd.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//I already try with all possible CAMCORDER , MIC , Default etc etc but none was working
MediaRecd.setOutputFile(Currentfilename);
try {
MediaRecd.prepare();
MediaRecd.start();
} catch (Exception e) {
MediaRecd.reset();
MediaRecd.release();
MediaRecd = null;
}
Please help
AudioSource.VOICE_CALL is not working in some android devices so
instead of VOICE_CALL use below
First try MediaRecorder.AudioSource.CAMCORDER
MediaRecd = new MediaRecorder();
MediaRecd.setAudioSource(MediaRecorder.AudioSource.CAMCORDER
);
If above is not working than use MediaRecorder.AudioSource.MIC
MediaRecd = new MediaRecorder();
MediaRecd.setAudioSource(MediaRecorder.AudioSource.MIC
);
I want to develop an application through which if the caller calls you, the call should be answered automatically without user's involvement and the caller could hear a pre-recorded voice which is already recorded and saved.The audio file should be in .wav format.I searched for help in google but i came to know that it is not possible in Android but there are some android applications which have the same functionality.So i think there is some possibility for this.Excuse me if the question is wrong.I would be grateful if some one help me.I am using eclipse Helios with ADT Plugin. I've tried the below code but it didn't work out.If someone know the answer please help me out. I've used broadcast receiver to read the phone state changes.In CALL_STATE_OFFHOOK, i wrote the following code.
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(context, "Call Picked..", Toast.LENGTH_LONG) .show();
Log.d("received", "Call Picked....");
final MediaPlayer mPlayer = MediaPlayer.create(context, R.raw.music_file);
mPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
mPlayer.prepareAsync();
mPlayer.start();
mPlayer.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method
mPlayer.reset();
return true;
}
});
AudioManager am=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_IN_CALL);
am.setSpeakerphoneOn(false);
am.setMicrophoneMute(true);
Log.d("in call","sent audio");
mPlayer.reset();
mPlayer.release();
break;
Piping audio into a phone call is completely unsupported by Android hardware.
"Auto Call Answer" by DevIndia Infoway is on the playstore. It works by playing back an mp3 while switching the handsfree on. So the other party will hear what is played (and also if there is other noise in the room). Not really rocket-science.
VOICE_CALL, VOICE_DOWNLINK ,VOICE_UPLINK
not working on android 4.0 but working on android 2.3 (Actual Device),I have uploaded a dummy project to record all outgoing call so that you can see it for your self
http://www.mediafire.com/?img6dg5y9ri5c7rrtcajwc5ycgpo2nf
you just have to change audioSource = MediaRecorder.AudioSource.MIC; to audioSource = MediaRecorder.AudioSource.VOICE_CALL; on line 118 in TService.java
If you come across any error, tell me. Any suggestion related to it will be accepted.
After a lot of search I Found that Some Manufactures have closed the access to such function because call recording is not allowed in some countries. If anyone finds such question and get the solution some other way then post it over here it may be helpful to many because many people are have the same issue.
Try to use MediaRecorder.AudioSource.VOICE_RECOGNITION. I had the same problem - ASUS Transformer uses microphone near the back camera by default and audio is very silent in this case. VOICE_CALL doesn't work on this tablet and I have tried VOICE_RECOGNITION - in that case it uses front microphone and audio volume is OK.
OK, in my case this code (thank you eyal!) worked for Samsung Galaxy Note 6:
String manufacturer = Build.MANUFACTURER;
if (manufacturer.toLowerCase().contains("samsung")) {
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
} else {
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
}
you try to add this,it may be
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
mMediaRecorder.start();
}
}, 1000);
I am using the following library to stream YouTube videos to an Android application.
http://code.google.com/p/android-youtube-player/source/browse/trunk/OpenYouTubeActivity/src/com/keyes/youtube/OpenYouTubePlayerActivity.java?r=3
I am successfully able to play videos on HTC and Motorola phones over 3G and Wifi. However, on Samsung Galaxy (Epic 4G) and Samsung Galaxy II phones I am only able to play using Wifi. 3G gives me this error: "Cannot play video. Sorry this video cannot be played."
I have tried forcing low quality YouTube streaming, but this did not help. I see in my log that Start() is being called in both cases (3G/Wifi). Is this an issue with VideoView? Is there a workaround?
Edit 2
The videos are coming from YouTube API. I have attempted using embedded and normal streams, as well as lowest quality stream available (varying per video). Also, I do not think it is an encoding issue since the same videos play correctly using Wifi.
Edit 1
I also receive the following output regardless of wether video plays using Wifi or does not using 3G.
01-30 15:22:38.305: E/MediaPlayer(3831): error (1, -1)
01-30 15:22:38.305: E/MediaPlayer(3831): callback application
01-30 15:22:38.305: E/MediaPlayer(3831): back from callback
01-30 15:22:38.309: E/MediaPlayer(3831): Error (1,-1)
According to this Link, these errors means the following (I think):
/*
Definition of first error event in range (not an actual error code).
*/
const PVMFStatus PVMFErrFirst = (-1);
/*
Return code for general failure
*/
const PVMFStatus PVMFFailure = (-1);
/*
/*
Return code for general success
*/
const PVMFStatus PVMFSuccess = 1;
/*
Further adding confusion.
Yes, as you are thinking, this is a issue in VideoView, similar issues also appear in MediaPlayer, and I've encountered similar and strange issues as you did, I had problems when the video was played only on 3G and not on Wi-Fi. This usually happens on 2.1 and some 2.2 devices, but not on higher API levels as I've seen so far.
So what I can recommend is do the following :
First check if the running device may be one that can have issues, something like this :
//Define a static list of known devices with issues
static List sIssueDevices=Arrays.asList(new String[]{"HTC Desire","LG-P500","etc"});
if(Build.VERSION.SDK_INT<9){
if(sIssueDevices.contains(Build.Device){
//This device may have issue in streaming, take appropriate actions
}
}
So this was the simplest part, to detect if the running device may have issues in streaming the video. Now, what I did and may also help you, is buffer the video from Youtube in a file on the SDCard and set that file as the source for your VideoView. I will write some code snippets to see how my approach was :
private class GetYoutubeFile extends Thread{
private String mUrl;
private String mFile;
public GetYotubeFile(String url,String file){
mUrl=url;
mFile=file;
}
#Override
public void run() {
super.run();
try {
File bufferingDir=new File(Environment.getExternalStorageDirectory()
+"/YoutubeBuff");
File bufferFile=new File(bufferingDir.getAbsolutePath(), mFile);
//bufferFile.createNewFile();
BufferedOutputStream bufferOS=new BufferedOutputStream(
new FileOutputStream(bufferFile));
URL url=new URL(mUrl);
URLConnection connection=url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla");
connection.connect();
InputStream is=connection.getInputStream();
BufferedInputStream bis=new BufferedInputStream(is,2048);
byte[] buffer = new byte[16384];
int numRead;
boolean started=false;
while ((numRead = bis.read(buffer)) != -1 && !mActivityStopped) {
//Log.i("Buffering","Read :"+numRead);
bufferOS.write(buffer, 0, numRead);
bufferOS.flush();
mBuffPosition += numRead;
if(mBuffPosition>120000 &&!started){
Log.e("Player","BufferHIT:StartPlay");
setSourceAndStartPlay(bufferFile);
started=true;
}
}
Log.i("Buffering","Read -1?"+numRead+" stop:"+mActivityStopped);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void setSourceAndStartPlay(File bufferFile) {
try {
mPlayer.setVideoPath(bufferFile.getAbsolutePath());
mPlayer.prepare();
mPlayer.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Another issue will arise when the VideoView has stopped playing before the end of file, because not enough was buffered in the file. For this you need to set an onCompletionListener() and if you are not at the end of the video, you should start again the video playback from the last position :
public void onCompletion(MediaPlayer mp) {
mPlayerPosition=mPlayer.getCurrentPosition();
try {
mPlayer.reset();
mPlayer.setVideoPath(
new File("mnt/sdcard/YoutubeBuff/"+mBufferFile).getAbsolutePath());
mPlayer.seekTo(mPlayerPosition);
mPlayer.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
In the end, of course the GetYoutubeFile thread is started in the onCreate() method :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//init views,player,etc
new GetYoutubeFile().start();
}
Some modifications and adaptation I think will have to be done for this code, and it may not be the best approach, but it helped me, and I couldn't find any alternative.
I have tackle with this problem in my own way.First every time read your log cat.If you got
Error (1,-1)
that means you will get sorry,this video can not play message.So in this case finish that activity, give custom progress bar and download video.Then after downloading save it in temporary folder then play it.After playing delete that folder.
To reading log cat---
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
}
} catch (IOException e) {
}
Read this answer too. although it is just user experience not linked to app.but it happens sometimes in default application also
Heaps of videos, even if I have full 3G network coverage, will say "Sorry, this video cannot be played". One day, I got so pissed off with it, I just kept pressing 'Okay' to dismiss the message, and then pressed the video again only to see the "Sorry, this video cannot be played" message again. I repeated this process (in my blind anger), but eventually, after about 5 tries, the video decided to miraculously play!
This method pretty much works for me every time. Most videos won't want to play the first time, but eventually if I am just persistent, and keep telling it to play even though it tells me it 'can't' play the video, it will play! Although, some videos i've had to press 'Okay', press the video, press 'Okay', press the video etc... for like 20 times before it actually decided to play. Those times, I have been incredibly close to getting my phone and throwing it down on the floor because of how shitty I am with how youtube won't work.
I wish there was a way to fix this problem. No one seems to have come up with a solution. Everyone just says "oh yeah I have the same problem" but no one contributes anything. GOOGLE, SOLVE THIS PROBLEM ON YOUR PHONES. THIS SEEMS TO BE HAPPENING WORLDWIDE, ON A RANGE OF ANDROID PHONES.
This message often cames from the inappropriate encoding of the video ("Cannot play video. Sorry this video cannot be played.") I was struggling with videoview for a while , now the correct encoded videos play on all tested devices, even when using Wifi or 3G. Let em know if you want to know how to encode the videos. And for streaming the videos I used the demo from android sdk apis and it works flawless.
I've been having some problems while trying to fix a simple video recording app*. I think I followed the sequence of steps correctly. The following is a simplification of the part of the code that is giving me problems. This code is executed only as a callback once a button is pressed:
if ( mRecorder != null){
mRecorder.reset();
mRecorder.release();
}
mRecorder = new MediaRecorder();
if(mViewer.hasSurface){
mRecorder.setPreviewDisplay(mViewer.holder.getSurface());
Log.d(TAG,"Surface has been set");
}
try {
Log.d(TAG,"Sleeping for 4000 mili");
Thread.sleep(4000);
Log.d(TAG,"Waking up");
} catch (InterruptedException e) {
Log.e(TAG,"InterruptedException");
e.printStackTrace();
}
mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setVideoFrameRate(12);
mRecorder.setVideoSize(176, 144);
mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setMaxDuration(MAX_DURATION_TEST);
String targetFile = "/sdcard/webcamera/temp.mp4";
File localFile = new File(targetFile);
if(localFile.exists()){
Log.d(TAG,"Local file exists");
}else{
Log.d(TAG,"Local file does not exist");
}
mRecorder.setOutputFile(targetFile);
try {
mRecorder.prepare();
bPrepared = true;
Log.i(TAG,"prepared");
return;
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
Log.e(TAG ,"IOException");
Log.e(TAG,"Message: "+e.getMessage());
StackTraceElement[] array = e.getStackTrace();
for(StackTraceElement element : array){
Log.e(TAG,""+element.toString());
}
}
The important thing which I don't understand here is that whenever I put the video encoder to be MPEG_4_S it works. On the other hand whenever I put the encoder to be H264 it just does not. The problem is that this piece of code is just part of a bigger project, and the rest of it kind of expects this video to be encoded with h264.
I'm testing on a samsung Galaxy I-7500, running on froyo by the way. And I think the Galaxy I-9000 has the same problem.
The puzzling thing for me is that according to this documentation right here:
http://developer.android.com/guide/appendix/media-formats.html, MPEG_4_SP encoding should not be supported at all, while H264 is supported since honeycomb. So why is it working with MPEG_4_SP at all? and is it possible to make it work with H264?
The error I get is not really clear.
07-11 00:01:40.626: ERROR/MediaSource(1386): Message: prepare failed.
07-11 00:01:40.766: ERROR/MediaSource(1386): android.media.MediaRecorder._prepare(Native Method)
07-11 00:01:40.766: ERROR/MediaSource(1386): android.media.MediaRecorder.prepare(MediaRecorder.java:508)
07-11 00:01:40.766: ERROR/MediaSource(1386): com.appdh.webcamera.MediaSource.prepareOutput(MediaSource.java:74)
07-11 00:01:40.766: ERROR/MediaSource(1386): com.appdh.webcamera.MainActivity.startDetectCamera(MainActivity.java:312)
*Actually, the app is a little more complicated than just that, as it also does stream the video over LAN, but the part which I am concerned here has nothing to do with that. You can check this interesing project out here: http://code.google.com/p/ipcamera-for-android/
As you already wrote H.264 encoding support can be only expected from devices running honeycomb and later, which currently means only tablets. If you need H.264 you should test for prepare failed and either tell the user that the device is not supported or better block devices without H.264 using market filters. Or you can compile ffmpeg for android - like several other projects do. Have a look at these links:
http://odroid.foros-phpbb.com/t338-ffmpeg-compiled-with-android-ndk
http://bambuser.com/opensource
FFmpeg on Android
You also can use JCodec
It supports Android and have few samples for it.
The best way to compile it with Gradle is:
compile 'com.github.jcodec:jcodec:0.2.0-vg4'
but for latest improvements and bug fixes you need to compile from latest commits (there is still no new release from 2016)