Constantly change TextView while microphone is on - android

I am trying to have my application start to record sound from the microphone when a button is pressed. I want it to print out the amplitude of the sound recorded while the microphone is on to my TextView object.
Here is my code for the RecordSound class:
import android.media.MediaRecorder;
public class RecordSound {
private MediaRecorder mRecorder = null;
public void start() {
if (mRecorder == null) {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile("/dev/null");
try {
mRecorder.prepare();
} catch (Exception e) {
e.printStackTrace();
}
mRecorder.start();
}
}
public void stop() {
if (mRecorder != null) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
}
public double getAmplitude() {
if (mRecorder != null) {
return mRecorder.getMaxAmplitude();
}
else {
return 0;
}
}
public boolean isOn() {
if (mRecorder == null) {
return false;
}
else {
return true;
}
}
}
Here is my code for the MainActivity class:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private RecordSound mRecorder = null;
private TextView tv = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button recordButton = (Button) findViewById(R.id.bRecord);
tv = (TextView)findViewById(R.id.data);
mRecorder = new RecordSound();
recordButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mRecorder.isOn()) {
mRecorder.stop();
}
else {
mRecorder.start();
}
if (mRecorder.isOn()) {
// long t= System.currentTimeMillis();
// long end = t+15000;
// while(System.currentTimeMillis() < end) {
// tv.setText(String.valueOf(mRecorder.getAmplitude()));
// }
//
tv.setText(String.valueOf(mRecorder.getAmplitude()));
}
}
});
while (mRecorder != null && mRecorder.isOn()) {
tv.setText(String.valueOf(mRecorder.getAmplitude()));
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The code that is commented out is my attempt at making the microphone record and print the amplitude for the first fifteen seconds that the microphone is on.
Currently I am able to press the button to start recording. It will then print out the amplitude value to the TextView, but it is always 0.0. I've tried a while statement several different places, both in and out of the onClick() method. I can't seem to get the TextView to update dynamically while the microphone is recording sound.

I didn't really try to run your code and I don't know much about MediaRecoder in Android. But I guess the problem of your code is that your while loop is inside onCreate() method, and so the loop only runs when the onCreate() is called (in case of you haven't understood when Android system calls onCreate() method of your app, I suggest reading the Activity Lifecycle document). Clearly, your media recorder hasn't been started when the while loop runs, so this statment:
tv.setText(String.valueOf(mRecorder.getAmplitude()));
is not runned at all.
Then when you press the button for the first time, the media recoder is started and right after that, you get the amplitude, set the TextView value also for the first and only one time. The document of MediaRecorder.getMaxAmplitude() says that it returns
the maximum absolute amplitude measured since the last call, or 0 when
called for the first time
That's why you always get 0.0 display on the TextView.
In order to update the TextView dynamically while the microphone is on, you should maintain a background thread that check the media recoder and get amplitude periodically. For this purpose, you can consider using Timer and TimerTask. But since Android system doesn't allow you to update the UI from any thread other than the main thread, you may have to use Handler. I found this answer is useful for you.

Related

How to display a notification from service when activity gets destroyed

Here my scenario in which i am starting a service from an Activity which play music in background. When i press back button on this activity, activity get destroyed. but service is still running in background. I want to show a notification to user when this activity get destroyed so that they can play/pause/stop audio from notification. but i dont want notification to be displayed when the service get started.
below is my activity code :
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class PlayBackgroundAudioActivity extends AppCompatActivity {
private AudioServiceBinder audioServiceBinder = null;
private Handler audioProgressUpdateHandler = null;
// Show played audio progress.
private ProgressBar backgroundAudioProgress;
private TextView audioFileUrlTextView;
// This service connection object is the bridge between activity and background service.
private ServiceConnection serviceConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
// Cast and assign background service's onBind method returned iBander object.
audioServiceBinder = (AudioServiceBinder) iBinder;
}
#Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_background_audio);
setTitle("dev2qa.com - Play Audio Use Background Service");
// Bind background audio service when activity is created.
bindAudioService();
final String audioFileUrl = "http://www.dev2qa.com/demo/media/test.mp3";
backgroundAudioProgress = (ProgressBar)findViewById(R.id.play_audio_in_background_service_progressbar);
// Get audio file url textview.
audioFileUrlTextView = (TextView)findViewById(R.id.audio_file_url_text_view);
if(audioFileUrlTextView != null)
{
// Show web audio file url in the text view.
audioFileUrlTextView.setText("Audio File Url. \r\n" + audioFileUrl);
}
// Click this button to start play audio in a background service.
Button startBackgroundAudio = (Button)findViewById(R.id.start_audio_in_background);
startBackgroundAudio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Set web audio file url
audioServiceBinder.setAudioFileUrl(audioFileUrl);
// Web audio is a stream audio.
audioServiceBinder.setStreamAudio(true);
// Set application context.
audioServiceBinder.setContext(getApplicationContext());
// Initialize audio progress bar updater Handler object.
createAudioProgressbarUpdater();
audioServiceBinder.setAudioProgressUpdateHandler(audioProgressUpdateHandler);
// Start audio in background service.
audioServiceBinder.startAudio();
backgroundAudioProgress.setVisibility(ProgressBar.VISIBLE);
Toast.makeText(getApplicationContext(), "Start play web audio file.", Toast.LENGTH_LONG).show();
}
});
// Click this button to pause the audio played in background service.
Button pauseBackgroundAudio = (Button)findViewById(R.id.pause_audio_in_background);
pauseBackgroundAudio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
audioServiceBinder.pauseAudio();
Toast.makeText(getApplicationContext(), "Play web audio file is paused.", Toast.LENGTH_LONG).show();
}
});
// Click this button to stop the media player in background service.
Button stopBackgroundAudio = (Button)findViewById(R.id.stop_audio_in_background);
stopBackgroundAudio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
audioServiceBinder.stopAudio();
backgroundAudioProgress.setVisibility(ProgressBar.INVISIBLE);
Toast.makeText(getApplicationContext(), "Stop play web audio file.", Toast.LENGTH_LONG).show();
}
});
}
// Bind background service with caller activity. Then this activity can use
// background service's AudioServiceBinder instance to invoke related methods.
private void bindAudioService()
{
if(audioServiceBinder == null) {
Intent intent = new Intent(PlayBackgroundAudioActivity.this, AudioService.class);
// Below code will invoke serviceConnection's onServiceConnected method.
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}
}
// Unbound background audio service with caller activity.
private void unBoundAudioService()
{
if(audioServiceBinder != null) {
unbindService(serviceConnection);
}
}
#Override
protected void onDestroy() {
// Unbound background audio service when activity is destroyed.
unBoundAudioService();
super.onDestroy();
}
// Create audio player progressbar updater.
// This updater is used to update progressbar to reflect audio play process.
private void createAudioProgressbarUpdater()
{
/* Initialize audio progress handler. */
if(audioProgressUpdateHandler==null) {
audioProgressUpdateHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
// The update process message is sent from AudioServiceBinder class's thread object.
if (msg.what == audioServiceBinder.UPDATE_AUDIO_PROGRESS_BAR) {
if( audioServiceBinder != null) {
// Calculate the percentage.
int currProgress =audioServiceBinder.getAudioProgress();
// Update progressbar. Make the value 10 times to show more clear UI change.
backgroundAudioProgress.setProgress(currProgress*10);
}
}
}
};
}
}
#Override
public void onBackPressed() {
startActivity(new Intent(PlayBackgroundAudioActivity.this,ForeGroundService.class));
finish();
}
}
below is my service code:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class AudioService extends Service {
private AudioServiceBinder audioServiceBinder = new AudioServiceBinder();
public AudioService() {
}
#Override
public IBinder onBind(Intent intent) {
return audioServiceBinder;
}
}
below is myaudio binder class:
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import java.io.IOException;
/**
* Created by Jerry on 2/15/2018.
*/
public class AudioServiceBinder extends Binder {
// Save local audio file uri ( local storage file. ).
private Uri audioFileUri = null;
// Save web audio file url.
private String audioFileUrl = "";
// Check if stream audio.
private boolean streamAudio = false;
// Media player that play audio.
private MediaPlayer audioPlayer = null;
// Caller activity context, used when play local audio file.
private Context context = null;
// This Handler object is a reference to the caller activity's Handler.
// In the caller activity's handler, it will update the audio play progress.
private Handler audioProgressUpdateHandler;
// This is the message signal that inform audio progress updater to update audio progress.
public final int UPDATE_AUDIO_PROGRESS_BAR = 1;
public Context getContext() {
return context;
}
public void setContext(Context context) {
this.context = context;
}
public String getAudioFileUrl() {
return audioFileUrl;
}
public void setAudioFileUrl(String audioFileUrl) {
this.audioFileUrl = audioFileUrl;
}
public boolean isStreamAudio() {
return streamAudio;
}
public void setStreamAudio(boolean streamAudio) {
this.streamAudio = streamAudio;
}
public Uri getAudioFileUri() {
return audioFileUri;
}
public void setAudioFileUri(Uri audioFileUri) {
this.audioFileUri = audioFileUri;
}
public Handler getAudioProgressUpdateHandler() {
return audioProgressUpdateHandler;
}
public void setAudioProgressUpdateHandler(Handler audioProgressUpdateHandler) {
this.audioProgressUpdateHandler = audioProgressUpdateHandler;
}
// Start play audio.
public void startAudio()
{
initAudioPlayer();
if(audioPlayer!=null) {
audioPlayer.start();
}
}
// Pause playing audio.
public void pauseAudio()
{
if(audioPlayer!=null) {
audioPlayer.pause();
}
}
// Stop play audio.
public void stopAudio()
{
if(audioPlayer!=null) {
audioPlayer.stop();
destroyAudioPlayer();
}
}
// Initialise audio player.
private void initAudioPlayer()
{
try {
if (audioPlayer == null) {
audioPlayer = new MediaPlayer();
if (!TextUtils.isEmpty(getAudioFileUrl())) {
if (isStreamAudio()) {
audioPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
audioPlayer.setDataSource(getAudioFileUrl());
} else {
audioPlayer.setDataSource(getContext(), getAudioFileUri());
}
audioPlayer.prepare();
// This thread object will send update audio progress message to caller activity every 1 second.
Thread updateAudioProgressThread = new Thread()
{
#Override
public void run() {
while(true)
{
// Create update audio progress message.
Message updateAudioProgressMsg = new Message();
updateAudioProgressMsg.what = UPDATE_AUDIO_PROGRESS_BAR;
// Send the message to caller activity's update audio prgressbar Handler object.
audioProgressUpdateHandler.sendMessage(updateAudioProgressMsg);
// Sleep one second.
try {
Thread.sleep(1000);
}catch(InterruptedException ex)
{
ex.printStackTrace();
}
}
}
};
// Run above thread object.
updateAudioProgressThread.start();
}
}catch(IOException ex)
{
ex.printStackTrace();
}
}
// Destroy audio player.
private void destroyAudioPlayer()
{
if(audioPlayer!=null)
{
if(audioPlayer.isPlaying())
{
audioPlayer.stop();
}
audioPlayer.release();
audioPlayer = null;
}
}
// Return current audio play position.
public int getCurrentAudioPosition()
{
int ret = 0;
if(audioPlayer != null)
{
ret = audioPlayer.getCurrentPosition();
}
return ret;
}
// Return total audio file duration.
public int getTotalAudioDuration()
{
int ret = 0;
if(audioPlayer != null)
{
ret = audioPlayer.getDuration();
}
return ret;
}
// Return current audio player progress value.
public int getAudioProgress()
{
int ret = 0;
int currAudioPosition = getCurrentAudioPosition();
int totalAudioDuration = getTotalAudioDuration();
if(totalAudioDuration > 0) {
ret = (currAudioPosition * 100) / totalAudioDuration;
}
return ret;
}
}
Basically you should reconsider the time when your notification is shown. Since the onDestroy method of an activity may not be always called, I would prefer to use another point when to display your notification. You should be also aware of the new restrictions regarding background services since android 8 (API 26), if you do not explicitly mark them as a foreground service, then they might be killed by the OS while the app is in the background.
So, for your purpose it might be an option to use the method startForeground with a notification id and call it when your activity is going to the background (e.g. at onStop), then it will display the notification (which is the requirement for keeping the service in the foreground). If you decide to go back to your activity after some time, you may call stopForeground to stop the foreground mode and dismiss your notification.

Android native media player not firing onCompletion method

Native Android Media player does not invoke onCompletion method after the video has ran it's duration.
I have an Activity that plays a single video or a list of videos (m3u8 file). Video plays file as it should, I can use the media controller as well, however, when the video runs out, after a few seconds he starts a loop. onCompletion method doesn't activate unless the user clicks (or double taps) the fast forward option on media controler.
Is there a way to make the onComplete method activate automatically when the video runs out? (I've tried using the listener, doesn't work). Pointing out what I did wrong also works.
Here is the activity that handles video playing:
public class VideoActivity extends ActionBarActivity implements MediaPlayer.OnCompletionListener {
private static final String TAG = VideoActivity.class.getSimpleName();
private VideoView mVideoViewStream1;
ProgressDialog mDialog;
private String mURL;
private ChannelPlaylist mChannelPlaylist;
private int mIndex;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
// Validate internet connection
if (!Util.isDeviceConnected(this)) {
FragmentManager fm = getSupportFragmentManager();
PopUpErrorFragment popupError = PopUpErrorFragment.newInstance(getString(R.string.errorInternetConnection));
popupError.show(fm, PopUpErrorFragment.TAG);
finish();
}
// Assign controls to their variable representations
mVideoViewStream1 = (VideoView) findViewById(R.id.vvVideo);
// Retrieve bundle data
Intent i = getIntent();
Bundle bundle = i.getExtras();
ChannelPlaylist channelPlaylist = bundle.getParcelable(KeyHelper.BUNDLE_CHANNEL_PLAYLIST);
if (channelPlaylist != null) {
this.mChannelPlaylist = channelPlaylist;
if (bundle.containsKey("index") && mIndex == 0) {
this.mIndex = bundle.getInt("index");
} else {
if (this.mIndex == 0) {
this.mIndex = this.mChannelPlaylist.getIndex();
}
}
this.mURL = this.mChannelPlaylist.getSchedule().get(mIndex).getStream_url();
} else {
this.mURL = bundle.getString("url");
}
progressDialog();
standardAndroidPlayer();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.video, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/***************************** CLASS METHODS **************************************************/
/**
* Dismissess the dialog
*/
private void dismissDialog() {
if (this.mDialog != null) {
mDialog.dismiss();
}
}
/**
* Standard android player
*/
private void standardAndroidPlayer() {
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(this);
mediacontroller.setAnchorView(mVideoViewStream1);
// Get the URL from String VideoURL
mVideoViewStream1.setMediaController(mediacontroller);
// Set media player completion listener
mVideoViewStream1.setOnCompletionListener(this);
mVideoViewStream1.setVideoURI(Uri.parse(this.mURL));
mVideoViewStream1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
//Log.i(TAG, "Duration = " + mVideoViewStream1.getDuration());
if (mp.isLooping()) mp.setLooping(false);
}
});
mVideoViewStream1.requestFocus();
mVideoViewStream1.start();
} catch (Exception e) {
dismissDialog();
Log.e(TAG, "Error" + e.getMessage());
e.printStackTrace();
}
dismissDialog();
}
#Override
public void onCompletion(MediaPlayer mp) {
this.mIndex++;
if (this.mIndex < mChannelPlaylist.getSchedule().size()) {
this.mURL = this.mChannelPlaylist.getSchedule().get(this.mIndex).getStream_url();
standardAndroidPlayer();
} else {
finish();
}
}
/**
* Creates a progress dialog
*/
private void progressDialog() {
// Create a progressbar
mDialog = new ProgressDialog(VideoActivity.this);
// Set progressbar title
mDialog.setTitle("Android Video Streaming Tutorial");
// Set progressbar message
mDialog.setMessage("Buffering...");
mDialog.setIndeterminate(false);
mDialog.setCancelable(false);
// Show progressbar
mDialog.show();
}
}
Edited:
Ok, for some strange reason, on mobile phone the method doesn't fire off, however when used on tablet, it does. I wasn't able to reproduce the effect on tablet and didn't have any success on the phone either. Does it have something to do with the versions of android? Since I didn't get any warnings about compatibility when writing the listener, I didn't think much about it.
Try this.
mVideoViewStream1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
finish();
}
});

Android YouTubePlayer - get event on certain time of playing

I need make some action after video have played 50% of its length.
So I did not see some listener with something like "onCertainMills()", I see onVideoEnded() but I need get event when current time of played video will more then length/2.
You could try to use the YoutubePlayer's property getCurrentTimeMillis() to constantly obtain the time and getDurationMillis() to get the video's total duration. Thus, you can set the desired task when the youtubePlayer.getCurrentTimeMillis() >= youtubePlayer.getDurationMillis() with an asynctask.
The code will look like this:
player.setPlaylistEventListener(playlistEventListener);
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
AsyncTask a = new AsyncTask(); //listener for time
a.execute(player, 2000,this); //player, time, and activity
if (!wasRestored) {
playVideoAtSelection();
}
setControlsEnabled(true);
At the end of the ends I have made class to make action on certain second of playing video.
Here is the code to make some action after half time of the video playing: (+ 1 sec) Hope it will help to someone.
import android.os.Handler;
import android.util.Log;
import com.clipclash.android.entities.Clip;
import com.google.android.youtube.player.YouTubePlayer;
public class YoutubeCounter {
Clip clip;
long counter;
long startCut;
long endCut;
long durationGoal;
private YouTubePlayer youTubePlayer;
boolean goal = false;
Handler h;
Runnable checkGoal = new Runnable() {
public void run() {
getProgress();
if (counter >= durationGoal) {
// GOAL !!!
//TODO MAKE SOME ACTION
goal = true;
stopSchedule();
} else {
startSchedule();
}
}
};
public YoutubeCounter(Clip clip) {
this.clip = clip;
h = new Handler();
}
public void setYouTubePlayer(YouTubePlayer youTubePlayer) {
this.youTubePlayer = youTubePlayer;
}
public void play() {
if (!goal) {
if (durationGoal == 0) {
durationGoal = this.youTubePlayer.getDurationMillis() / 2;
}
startCut = youTubePlayer.getCurrentTimeMillis();
startSchedule();
}
}
public void stop() {
if (!goal) {
getProgress();
stopSchedule();
}
}
private void startSchedule() {
long newSchedule = durationGoal - counter;
newSchedule = newSchedule + 1000; // just a little bit more - not requires
h.postDelayed(checkGoal, newSchedule);
}
public void stopSchedule() {
h.removeCallbacks(checkGoal);
}
private void getProgress() {
try {
endCut = youTubePlayer.getCurrentTimeMillis();
long cut = endCut - startCut;
if (cut < 0) {
cut = 0;
}
counter += cut;
} catch (Exception e) {
}
}
public long getCounter() {
return counter;
}
}

Making same android activity class as a service ondestroy

This is an application that measures the sound intensity and in accordance with that measured value it sets the seek bar and in turn it adjusts the ringer volume in accordance with the seek bar automatically.
I want to run this application in foreground as well as in background
when user destroys it. Because the user will definately be needing
the app even after he quits it. I have read the documentation of
creating a service but the problem is i want full code same as
activity to be run in the background after the app destroyal..So any
help would be greatly appreciated.
package com.example.soundmeter;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;
import android.content.Context;
import android.media.AudioManager;
import android.view.KeyEvent;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import com.example.soundmeter.R;
public class MainActivity extends Activity {
//public static boolean isService = false;
TextView mStatusView;
MediaRecorder mRecorder;
Thread runner;
private static double mEMA = 0.0;
static final private double EMA_FILTER = 0.6;
//a variable to store the seek bar from the XML file
public SeekBar volumeBar;
//an AudioManager object, to change the volume settings
private AudioManager amanager;
final Runnable updater = new Runnable(){
public void run(){
updateTv();
};
};
final Handler mHandler = new Handler();
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
mStatusView = (TextView) findViewById(R.id.status);
//startService(new Intent(MainActivity.this,BackGroundService.class));
if (runner == null)
{
runner = new Thread(){
public void run()
{
while (runner != null)
{
volumeChanger();
try
{
Thread.sleep(5000);
Log.i("Noise", "Tock");
} catch (InterruptedException e) { };
mHandler.post(updater);
}
}
};
runner.start();
Log.d("Noise", "start runner()");
}
}
public void onResume()
{
super.onResume();
startRecorder();
}
public void onPause()
{
super.onResume();
startRecorder();
//super.onPause();
//super.stopRecorder();
}
/*#Override
public void onBackPressed()
{
super.onResume();
startRecorder();
}*/
public void startRecorder(){
if (mRecorder == null)
{
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile("/dev/null");
try
{
mRecorder.prepare();
}catch (java.io.IOException ioe) {
android.util.Log.e("[Monkey]", "IOException: " +
android.util.Log.getStackTraceString(ioe));
}catch (java.lang.SecurityException e) {
android.util.Log.e("[Monkey]", "SecurityException: " +
android.util.Log.getStackTraceString(e));
}
try
{
mRecorder.start();
}catch (java.lang.SecurityException e) {
android.util.Log.e("[Monkey]", "SecurityException: " +
android.util.Log.getStackTraceString(e));
}
//mEMA = 0.0;
}
}
/*public void stopRecorder() {
if (mRecorder != null) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
}*/
public void updateTv(){
double amp=getAmplitudeEMA();
mStatusView.setText(Double.toString((amp)) + " dB");
}
public double soundDb(double ampl){
double intensity=20 * Math.log10(getAmplitudeEMA() / ampl);
return intensity;
}
public double getAmplitude() {
if (mRecorder != null)
return (mRecorder.getMaxAmplitude());
else
return 0;
}
public double getAmplitudeEMA() {
double amp = getAmplitude();
mEMA = EMA_FILTER * amp + (1.0 - EMA_FILTER) * mEMA;
return mEMA;
}
public void volumeChanger()
{
volumeBar = (SeekBar) findViewById(R.id.sb_volumebar);
//get the audio manager
amanager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
//seek bar settings//
//sets the range between 0 and the max volume
volumeBar.setMax(amanager.getStreamMaxVolume(AudioManager.STREAM_RING));
//set the seek bar progress to 1
//volumeBar.setKeyProgressIncrement(1);
//sets the progress of the seek bar based on the system's volume
// volumeBar.setProgress(500);
if(mEMA<(double)800.00)
{
volumeBar.setProgress((int)amanager.getStreamMaxVolume(AudioManager.STREAM_RING)/5);
}
else if(mEMA<(double)15000.00)
{
volumeBar.setProgress((int)amanager.getStreamMaxVolume(AudioManager.STREAM_RING)*2/5);
}
else if(mEMA<25000.00)
{
volumeBar.setProgress((int)amanager.getStreamMaxVolume(AudioManager.STREAM_RING)*3/5);
}
else if(mEMA<50000.00)
{
volumeBar.setProgress((int)amanager.getStreamMaxVolume(AudioManager.STREAM_RING)*4/5);
}
else
{
volumeBar.setProgress((int)amanager.getStreamMaxVolume(AudioManager.STREAM_RING));
}
//register OnSeekBarChangeListener, so that the seek bar can change the volume
volumeBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
#Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
int index=volumeBar.getProgress();
//change the volume, displaying a toast message containing the current volume and playing a feedback sound
amanager.setStreamVolume(AudioManager.STREAM_RING, index, AudioManager.FLAG_SHOW_UI);
}
});
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//if one of the volume keys were pressed
if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP)
{
//change the seek bar progress indicator position
volumeBar.setProgress(amanager.getStreamVolume(AudioManager.STREAM_RING));
}
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
//propagate the key event
return super.onKeyDown(keyCode, event);
}
}
Have the measurement code always listen on the service and let it update shared preferences.
Then all the activity needs to do is listen to changes on shared preferences and update the UI accordingly.
You can obviously use any number of mechanisms to allow the activity respond to the services changes, but I think listening for shared preference changes is probably easiest to start with.
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
//update ui if your key was updated
}
};
prefs.registerOnSharedPreferenceChangeListener(listener);
I have figured out another way of doing it.
A broadcast Receiver can be used in the main activity in syncronization with a broadcast service.

Android How to obtain media player progress bar and also how to show it on each activity top screen

Am working on certain app XYZ. In that app having a audio player module. I launch a player launch from an activity using some following code:
playButton = (ImageButton) findViewById(R.id.play_image_button);
playButton.refreshDrawableState();
playButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if (playCount % 2 == 0) {
if (Data.songProfileArrayList.size() > 0) {
SongProfileBean songData = Data.songProfileArrayList
.get(0);
if ("audio".equals(songData.getSongFileType())) {
playButton
.setBackgroundResource(R.drawable.pause_button);
playBackgroundMP3(Data.URL_BASE
+ songData.getSongName());
} else if ("video".equals(songData.getSongFileType())) {
Intent intent = new Intent(mContext,
VedioViewActivity.class);
intent.putExtra("VideoUrl", Data.URL_BASE
+ songData.getSongName());
startActivity(intent);
}
}
} else {
// stopPlay();
pauseSong();
// playerLayout.setVisibility(View.INVISIBLE);
// playButton.setImageDrawable(getResources().getDrawable(R.drawable.play_button));
playButton.setBackgroundResource(R.drawable.play_button);
}
playCount++;
}
});
To explain more about the code:
Currently I'm using an same Image Button on click on that the audio song starts from Data.songProfileArrayList an Array list and on another click of same button the song is stoped.
Now My Requirement or gist of question:
Actually I'm looking for following:
1.Is there any things available to also get the progress state of audio . I have gone through 1.developer docs 2.android docs on media
If yes Also suggest the way to have that status bar on each activity of application.
2.Should be able to progress the audio ,start,stop from each activity.
Please suggest a valid answer only after going through all details mentioned.
Only suggest answer If you have a deep knowledge over that and have done earlier so far
Valid Help and suggestions are most welcome
Regards,
Arpit
Hello After R & D over the problem I am able to get default controllers and progress state .But still want this to be on each activity and should be custom Controllers
package com.APP_NAME_XYZ.app;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.MediaController;
import android.widget.TextView;
public class AudioPlayer extends Activity implements OnPreparedListener,
MediaController.MediaPlayerControl {
private static final String TAG = "AudioPlayer";
public static final String AUDIO_FILE_NAME = "audioFileName";
private MediaPlayer mediaPlayer;
private MediaController mediaController;
private String audioFile;
private Handler handler = new Handler();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audio_player);
// audioFile = this.getIntent().getStringExtra(AUDIO_FILE_NAME);
audioFile = "Provide URL of song";
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(this);
mediaController = new MediaController(this);
try {
mediaPlayer.setDataSource(audioFile);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
Log.e(TAG, "Could not open file " + audioFile + " for playback.", e);
}
}
#Override
protected void onStop() {
super.onStop();
mediaPlayer.stop();
mediaPlayer.release();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// the MediaController will hide after 3 seconds - tap the screen to
// make it appear again
mediaController.show();
return false;
}
// --MediaPlayerControl
// methods----------------------------------------------------
public void start() {
mediaPlayer.start();
}
public void pause() {
mediaPlayer.pause();
}
public int getDuration() {
return mediaPlayer.getDuration();
}
public int getCurrentPosition() {
return mediaPlayer.getCurrentPosition();
}
public void seekTo(int i) {
mediaPlayer.seekTo(i);
}
public boolean isPlaying() {
return mediaPlayer.isPlaying();
}
public int getBufferPercentage() {
return 0;
}
public boolean canPause() {
return true;
}
public boolean canSeekBackward() {
return true;
}
public boolean canSeekForward() {
return true;
}
// --------------------------------------------------------------------------------
public void onPrepared(MediaPlayer mediaPlayer) {
Log.d(TAG, "onPrepared");
mediaController.setMediaPlayer(this);
mediaController.setAnchorView(findViewById(R.id.main_audio_view));
handler.post(new Runnable() {
public void run() {
mediaController.setEnabled(true);
mediaController.show();
}
});
}
}
The audio_player layout is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_audio_view" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_gravity="center"
android:text="Now playing:" android:textSize="25sp" android:textStyle="bold" />
<TextView android:id="#+id/now_playing_text"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="20dip" android:layout_marginLeft="10dip"
android:layout_marginRight="10dip" android:layout_gravity="center"
android:text="Now playing.." android:textSize="16sp"
android:textStyle="italic" />
</LinearLayout>
You will need to change your approach. Use fragments and have one fragment show the progressbar and remain visible throughout. You can have just one activity and change the UI using fragments depending on different states of the application. When the application goes into background use Notification bar to show the media progress bar and allow user to relaunch the application from there.
Please refer how google music app works and follow the recommended approach.

Categories

Resources