I have just started to develop my first Android app, and I am having a hard time figuring out how to start the microphone and have it listen, which is a main feature of my app.
I've searched the Android docs and I can't find much info on this.
Thanks in advance.
Maybe this can help (actually from the Android docs):
Audio Capture
Create a new instance of android.media.MediaRecorder.
Set the audio source using MediaRecorder.setAudioSource(). You will probably want to use MediaRecorder.AudioSource.MIC.
Set output file format using MediaRecorder.setOutputFormat().
Set output file name using MediaRecorder.setOutputFile().
Set the audio encoder using MediaRecorder.setAudioEncoder().
Call MediaRecorder.prepare() on the MediaRecorder instance.
To start audio capture, call MediaRecorder.start().
To stop audio capture, call MediaRecorder.stop().
When you are done with the MediaRecorder instance, call MediaRecorder.release() on it. Calling MediaRecorder.release() is always recommended to free the resource immediately.
or:
Android Audio Recording Tutorial
You can use custom recorder:
final static int RQS_RECORDING = 1;
Uri savedUri;
Button buttonRecord;
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
buttonRecord = (Button) findViewById(R.id.record);
buttonRecord.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(
MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(intent, RQS_RECORDING);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == RQS_RECORDING) {
savedUri = data.getData();
Toast.makeText(MainActivity.this,
"Saved: " + savedUri.getPath(), Toast.LENGTH_LONG).show();
}
}
Related
I am developing app related to music player, its having equalizer settings, i know every device having a default equalizer. code for getting the default equalizer
equilizer.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL);
i.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, mp.getAudioSessionId());
startActivityForResult(i, 11113);
}
});
}
In onActivityResult:
protected void onActivityResult(int requestCode,int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
return;
}
}
Using the above code we can get default equalizer, but my requirement is design a equalizer with different UI and add some more effects to the equalizer.
Can any one give me an idea how can I do that?
There is an Equalizer effect. You can pass the audio session ID of the MediaPlayer to its constructor:
// Given a MediaPlayer mp
Equalizer eq = new Equalizer(0, mp.getAudioSessionId());
Here is an example program that uses it. I haven't built it to see, but it at least hooks up the equalizer correctly.
I want to start camera and also to automatically start recording just by clicking an app in android. I have the code to start the camera but I do not know how to start auto capture of the video. Please help.
the code I have for launching camera-
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c1_main);
Intent intent = new Intent("android.media.action.VIDEO_CAPTURE");
StartActivityForResult(intent,CAPTURE_VIDEO_ACTIVITY);
}
I found about view.performclick but do not know how to use for camera
Use MediaRecorder for this purpose. Though it will require more work but will give you much more control. Follow this link http://android-er.blogspot.tw/2011/04/start-video-recording-using.html. Since you don't reuire any button click for recording, keep a delay before starting the camera. Do it like this
myButton.setPressed(true); //you won't click the button
myButton.invalidate();
myButton.postDelayed(new Runnable() {
public void run() {
myButton.setPressed(false);
myButton.invalidate();
releaseCamera(); //release camera from preview before MediaRecorder starts
if(!prepareMediaRecorder()){
Toast.makeText(AndroidVideoCapture.this,"could not prepare MediaRecorder",Toast.LENGTH_LONG).show();
finish();
}
mediaRecorder.start();
}
},5000); //causes delay of 5 seconds befor recording starts
Ok, Make following, changes in your code.
Button play;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c1_main);
play = findViewById ( R.id.btnPlay ); // assuming you have this button in your .xml file.
play.setOnClickListener ( new OnClickListener()
{
#Override
public void onClick ( View view )
{
Intent intent = new Intent("android.media.action.VIDEO_CAPTURE");
StartActivityForResult(intent,CAPTURE_VIDEO_ACTIVITY);
}
});
}
I am developing an application in which i want to play a short 5 seconds video at the startup. which is the best format 3gp, mpg or something else? i have generated a title activity. I wanted to play the video before title. Help please!!! Below is the code of my title activity.
public class Title extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.title);
setTitle("M.I.S.T");
this.setTitleColor(Color.BLUE);
View title = getWindow().findViewById(android.R.id.title);
View titleBar = (View) title.getParent();
titleBar.setBackgroundColor(Color.YELLOW);
Thread timer = new Thread(){
public void run(){
try{
sleep(3000);
}catch (InterruptedException e){
e.printStackTrace();
}finally{
Intent open= new Intent("com.congestion6.asad.MENU");
startActivity(open);
}
}
};
timer.start();
}
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
mpeg can be compressing the video over a range of different formats/algorithms/codecs and some are supported some are not. 3gp is just one and it is supported (although a very poor format).
Try encoding a video yourself that you'll see all different options. Usually mp4 on H264 works flawlessly on mobiles.
I want to stream and play itunes preview urls like http://a2.mzstatic.com/us/r1000/044/Music/e9/40/ec/mzm.evyxvimp.aac.p.m4a. I tried to use AAC Decoder Android Library. By which I can stream and play AAC stream urls like http://http.yourmuze.com:8000/play/paradise/l.aac. But it dnt stream m4a urls(Logcat says java.io.FileNotFoundException: http://a2.mzstatic.com/us/r1000/044/Music/e9/40/ec/mzm.evyxvimp.aac.p.m4a).
How I can stream the .m4a links?
My Code:
public class AACPlayerActivity extends Activity implements OnClickListener,PlayerCallback{
private Button btnPlay;
private ProgressBar progress;
private Handler uiHandler;
private MultiPlayer multiPlayer;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnPlay = (Button)findViewById(R.id.playButton);
progress = (ProgressBar)findViewById(R.id.progress);
progress.setVisibility(View.INVISIBLE);
btnPlay.setOnClickListener(this);
uiHandler = new Handler();
}
public void playerException(Throwable arg0) {
// TODO Auto-generated method stub
}
public void playerMetadata(String arg0, String arg1) {
// TODO Auto-generated method stub
}
public void playerPCMFeedBuffer(boolean arg0, final int audioBufferSizeMs, final int audioBufferCapacityMs) {
// TODO Auto-generated method stub
uiHandler.post(new Runnable() {
public void run() {
// TODO Auto-generated method stub
progress.setProgress( audioBufferSizeMs * progress.getMax() / audioBufferCapacityMs );
}
});
}
public void playerStarted() {
uiHandler.post(new Runnable() {
public void run() {
// TODO Auto-generated method stub
progress.setProgress( 0 );
progress.setVisibility( View.VISIBLE );
}
});
}
public void playerStopped(int arg0) {
// TODO Auto-generated method stub
uiHandler.post(new Runnable() {
public void run() {
// TODO Auto-generated method stub
progress.setVisibility( View.INVISIBLE );
}
});
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(btnPlay.getText().equals("Play")){
start();
btnPlay.setText("stop");
}else{
stop();
}
}
private void start() {
stop();
// we cannot do it in playerStarted() - it is too late:
multiPlayer = new MultiPlayer(this,MultiPlayer.DEFAULT_AUDIO_BUFFER_CAPACITY_MS, MultiPlayer.DEFAULT_DECODE_BUFFER_CAPACITY_MS);
multiPlayer.playAsync("http://a2.mzstatic.com/us/r1000/044/Music/e9/40/ec/mzm.evyxvimp.aac.p.m4a");
}
private void stop() {
if (multiPlayer != null) {
multiPlayer.stop();
multiPlayer = null;
}
}
Updates
I try to use ServeStream it says the url could not be opened.
I try to use Vitamio SDK it also fails to play the itunes preview Urls.
Is possible to play itunes preview urls in android?
Take a look at ServeStream:
HTTP media server browser and stream player for Android.
Features:
Supports Android 2.2+, 3.0+ (No support for < 2.2)
Plays Android supported media files
Additional support for m3u, m3u8, pls and asx playlists
Supports multitasking/playing audio in the background
Repeat and shuffle modes
Alarm clock support
Home screen widget
Utilizes HTML parsing to allow navigation of HTTP media servers that serve HTML pages
M4A and AAC are supported by Android. I would recommend to read this page: http://developer.android.com/guide/appendix/media-formats.html
Just couple of thing which you should be aware of
a) Older devices may have no particular coded or format support (especially, if you go below OS 2.2)
b) Some cheap devices may have quite strange things integrated in audio/video stack. I saw it on couple of cheap Chinese devices.
So, you can try to use MediaPlayer API to play it:
http://developer.android.com/reference/android/media/MediaPlayer.html
You can take a look at example here:
http://blog.endpoint.com/2011/03/api-gaps-android-mediaplayer-example.html
It has nothing to do with media formats etc. Apple simply prevents non-Apple clients to access previews. You can spoof User-Agent header and access to previews.
Read my relevant blog post here: http://ahmetalpbalkan.com/blog/how-to-bypass-itunes-music-previews-protection/ Thats why you get 404 while playing music on Android. this should answer your question.
Below is my code and I'm gonna integrate with the application. My problem is when the button clicked video recorder invoked and video recoded smoothly but I want to get the response from the camera when the video recording is done every time.
public class AndroidVideoActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnVideoRecorder = (Button)findViewById(R.id.buttonClick);
btnVideoRecorder.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("android.media.action.VIDEO_CAMERA");
startActivity(intent);
}
});
}
}
Can someone please guide me.
Thanks for your time in advance.
Try changing
startActivity(intent);
to
startActivityForResult(intent);
Then when the video is done recordng, it will return to your app and you will be able to do something with the video