i'm trying to develop an android voice recorder.i am able to record and play back the file but the problem is i can't get the decibel of the voice recording.the reason i need it is because i want to show a sound meter that when the app is recording it show's the sound decibel meanwhile.
any idea how can i do it?
Here is my code :
btRecord.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
try {
recorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
recorder.start();
/**** i need a method like recorder.Volume(); ***/
}catch(Exception ex)
{}
Related
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);
Require develop streaming audio playback with mediaplayer on android.
Any ideas ?!
Thanks
I try the following code:
play.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String url = "http://stream.radiosai.net:8002/"; // your URL here
MP.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
MP.setDataSource(url);
} 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();
}
MP.prepareAsync();
MP.start();
}
});
The above does not work Error deploying
MP.start();
Should be called after the mediaplager is ready, in setOnPreparedListener.
When you create MediaPlayer object, add prepared listener
MP.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
The German computer magazine C't recently published an article with the source code for an Android alarm clock radio. You can download the project files here. I think this contains everything that you need.
I have added two buttons to my app, one to play and one to start. As soon as i start the app the songs starts playing automatically. when i press on stops it stops but when i press on play it doesnt starts again
To stop I have used mediaplayer.stop() which is working fine
To start I have used
mediaplayer.start()
which is not working
According to flow diagram its given we need to use
prepare()
and then
OnPreparedListener.onPrepared()
and then
start()
I don't know how to use these functions. Please help me
Don't call stop() for stopping the mediaplayer use reset() instead. I dont have good reason for that. But, it'll work for sure.
// for stopping it call below statement
if(mediaPlayer.isPlaying())
mediaPlayer.reset();
//for playing it again
mediaPlayer.prepare();
mediaPlayer.start();
For starting media player again use following code.
if (mediaplayer != null) {
mediaplayer.start();
}
and one more thing, instead of using
mediaplayer.stop()
use
mediaplayer.pause();
so it will pause the current song instead of stop.
Just check the condition whether it is null or not.
Try following code on Play Button
mp.reset();
mp.setDataSource(song path);
mp.prepare();
mp.start();
mp.setDataSource(song path); not necessary if you playing same sound again
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.singleitemview);
btnsound=(Button)findViewById(R.id.play_sound);
btnstopsound=(Button)findViewById(R.id.stop_sound);
mp=new MediaPlayer();
btnsound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
mp.setDataSource(Sound);
} 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 {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
mp.setLooping(true);
}
});
btnstopsound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.setLooping(false);
mp.stop();
}
});
}
I'm trying to get this to work the way I want and it just won't I think I had it at some point but I was trying to fix something else and changed a few things and messed it up. Basically I'm this button is a play button. It is supposed to run media player through another thread so that it reacts and buffers faster. Issue is after it plays through to the end and I click play it is ended and keeps running the onCompletion. When I would like it to start over and play from the beginning again.
public void onClick(View v) {
if(playClick%2==0){
song1.setBackgroundResource(R.drawable.button_pause);
try{
if(playClick==0){
playThread();
if(lastPlayedExists){
lastPlayed.release();
}
}
mp.start();
mpExists = true;
playClick++;
lastPlayed = mp;
lastPlayedExists=true;
mp.setOnCompletionListener(new OnCompletionListener(){
#Override
public void onCompletion(MediaPlayer media) {
mp.reset();
song1.setBackgroundResource(R.drawable.button_play);
}
});
}catch (NullPointerException e){
}
}
else if (playClick%2==1){
song1.setBackgroundResource(R.drawable.button_play);
mp.pause();
playClick++;
}
}
}
});
And this is the thread for media player
private void playThread(){
try{
new Thread(new Runnable() {
public void run() {
try {
mp.setDataSource(song);
} 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) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}catch(IllegalStateException e){
}
}
I have tried it with the onCompletion having mp.reset(), mp.stop(), and mp.release() along with trying lastPlayed.release() all in different tests. As well as none of them being there and it just being the button change and the progress bar reset. If you know how to fix this and make it replay as it should I would greatly appreciate it. Or if you know how to make the thread stop and restart some how so that I can just make it recall and do the thread over again that would work too (atm when that has been tried it crashes because the thread has already been started). Thanks and I appreciate any input.
mp.setOnCompletionListener(new OnCompletionListener(){
#Override
public void onCompletion(MediaPlayer media) {
playClick=2;
song1.setBackgroundResource(R.drawable.button_play);
}
});
Hi
How to play media file in an application.I am trying it with the following code but do not know why it is not working for me
player= new MediaPlayer().create(context, R.raw.lonely);
player.start();
player.release();
Help me.
Thanks in advance.
I haven't played with MediaPlayer, but I would try without the release() call. This example doesn't use it. And the docs say it's a cleanup method to be called after the playback:
Releases resources associated with
this MediaPlayer object. It is
considered good practice to call this
method when you're done using the
MediaPlayer.
I think you're messing up the with constructors. You can instantiate the MediaPlayer statically: MediaPlayer.create(Context context, int resid), which is the easiest way, cause you just need to call play(). Also you need a valid context, it's to say, if you're creating rhe MediaPlayer within an Activity or a Service, just pass "this" as a context.
You can also use the "normal" constructor MediaPlayer(), but then you would have to explicity call to setDataSource() and prepare() before play().
Besides, as Grzegorz wrote, calling release() just after play() is not a good idea.
you can folow this sample :
public void audioPlayer(String path, String fileName){
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(path+"/"+fileName);
} 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();
}