Music goes out of replay loop - java

I write in Java music player for Android. It have a special function to trim a fragment of audio with range seekbar. After replay button is turned ON this fragment is looped. It works and selected fragment with range seekbar is played but not always as it would be necessary. Sometimes it gets out of max selected duration and goes to end of song.I can't find the place why this is happening...
Preview
Many thanks for any help and suggestions.
Here is the logic responsible for playing the song, that is activated when PLAY button is pressed:
playButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(mMediaPlayer != null){
if(mMediaPlayer.isPlaying()){
mMediaPlayer.pause();
playButton.setText("PLAY");
timer.shutdown();
}else{
mMediaPlayer.start();
playButton.setText("PAUSE");
timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(new Runnable() {
#Override
public void run() {
playProgress();
if (mMediaPlayer != null) {
if (!mSeekBar.isPressed()) {
mSeekBar.setProgress(mMediaPlayer.getCurrentPosition());
}
}
}
},10,10, TimeUnit.MILLISECONDS);
}
}
}
});
A function that regulates the playback of a fragment in rangeSeekbar
private void playProgress () {
if (mMediaPlayer.getCurrentPosition() == max) {
if(isRepeat == true) {
mMediaPlayer.seekTo(min);
}else{
mMediaPlayer.seekTo(min);
mMediaPlayer.pause();
}
}
if (mMediaPlayer.isPlaying()) {
mRunnable = new Runnable() {
#Override
public void run() {
playProgress();
}
};
mHandler.postDelayed(mRunnable, 0);
}
}
The logic responsible for "creating" the player that will form when the song is opened:
public void createMediaPlayer(Uri uri){
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioAttributes(
new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_MEDIA)
.build()
);
try {
mMediaPlayer.setDataSource(getApplicationContext(), uri);
mMediaPlayer.prepare();
title.setText(getNameFromUri(uri));
playButton.setEnabled(true);
mRangeSeekBar.setNotifyWhileDragging(true);
max = mMediaPlayer.getDuration();
mRangeSeekBar.setRangeValues(0, mMediaPlayer.getDuration());
mSeekBar.setMax(mMediaPlayer.getDuration());
long total_secs = TimeUnit.SECONDS.convert(max, TimeUnit.MILLISECONDS);
long mins = TimeUnit.MINUTES.convert(total_secs, TimeUnit.SECONDS);
long secs = total_secs - (mins*60);
duration = mins + ":" + secs;
elapse.setText("00:00 / " + duration);
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
releaseMediaPlayer();
}
});
} catch (IOException e){
title.setText(e.toString());
}
}
Replay function activated by the REPLAY button
replayButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//repeat = false
if(isRepeat){
isRepeat = false;
mMediaPlayer.setLooping(false);
replayButton.setText("Powtórka wyłączona");
Toast.makeText(PlayerActivity.this, "Tryb powtórki jest wyłączony", Toast.LENGTH_SHORT).show();
}else{
isRepeat = true;
mMediaPlayer.setLooping(true);
replayButton.setText("Powtórka włączona");
Toast.makeText(PlayerActivity.this, "Tryb powtórki jest włączony", Toast.LENGTH_SHORT).show();
}
//mediaPlayer.setLooping(true);
// Toast.makeText(PlayerActivity.this, "Repeat if ON", Toast.LENGTH_SHORT).show();
}
});
Here is code for this double slider that is supposed to set the beginning and end:
mRangeSeekBar.setOnRangeSeekBarChangeListener(new RangeSeekBar.OnRangeSeekBarChangeListener<Integer>() {
#Override
public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) {
//mRangeSeekBar.setNotifyWhileDragging(true);
mMediaPlayer.seekTo(minValue);
max = maxValue;
min = minValue;
String infoMax = String.valueOf(max);
Log.i("MAX", infoMax);
}
});
And here for this green bar with one point representing the duration of the song:
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (mMediaPlayer != null){
int millis = mMediaPlayer.getCurrentPosition();
long total_secs = TimeUnit.SECONDS.convert(millis, TimeUnit.MILLISECONDS);
long mins = TimeUnit.MINUTES.convert(total_secs, TimeUnit.SECONDS);
long secs = total_secs - (mins*60);
elapse.setText(mins + ":" + secs + " / " + duration);

Related

how to synchronize 2 data generated from 2 different devices

I have a 2 Insole (left and right insole), then 2 data generated from the left and right insole will be sent to firebase every 1 second. To send data per second I use 2 timers namely left_timer.scheduleAtFixedRate and right_timer.scheduleAtFixedRate but the data generated from these 2 insoles is still out of sync. because the resulting data is messy. the reason the resulting data is messy is that if we walk only one foot will step on the insole. but with this code, two feet will be on the ground when we walk. I suspect this delay is due to the process of sending data. is there any solution for this problem
Here My code:
connectLeftBtn:
connectLeftBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked)
{
connectDevice(L_insole_mac);
}
else
{
if (is_L_insole_connected)
if(is_L_insole_started)
left_insole_device_interface.stopInsole();
startLeftBtn.setText("Start Left");
is_L_insole_started = false;
bluetoothManager.closeDevice(left_insole_device_interface);
is_L_insole_connected = false;
Toast.makeText(Visualization.this, "Left Insole Disconnected.", Toast.LENGTH_SHORT).show();
}
}
private void connectDevice(String mac) {
bluetoothManager.openSerialDevice(mac)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::onConnected, this::onError);
}
#RequiresApi(api = Build.VERSION_CODES.N)
private void onConnected(BluetoothSerialDevice connectedDevice) {
// You are now connected to this device!
// Here you may want to retain an instance to your device:
left_insole_device_interface = connectedDevice.toSimpleDeviceInterface();
// Listen to bluetooth events
left_insole_device_interface.setListeners(message -> onMessageReceived(message), this::onMessageSent, this::onError);
left_insole_device_interface.stopInsole();
is_L_insole_connected = true;
Toast.makeText(getApplication(), "Connected to Left Insole.", Toast.LENGTH_SHORT).show();
}
#RequiresApi(api = Build.VERSION_CODES.N)
private void onMessageReceived(String message) {
//store incoming bytes temporarily
if(!is_L_insole_started){
left_temp_bytes+=message+" ";
}
//check whether the start_bytes exits in the temporary buffer
if(!is_L_insole_started && left_temp_bytes.contains(start_bytes)){
is_L_insole_started = true;
left_temp_bytes ="";
}
//if the start_bytes are found in the temporary buffer, start storing the incoming messages in the actual buffer
if(is_L_insole_started){
left_data_len++;
if(left_data_len>15) {
left_sensor_data_count++;
if (!non_sensor_indeces.contains(left_sensor_data_count)) {
l_data_double_arr[left_data_index] = Double.parseDouble(message);
// System.out.println("NON SENSOR INDEX:" + left_data_index + " "+ message);
left_data_index++;
}
}
Date date = new Date();
leftDataDict.put(String.valueOf(formatter.format(date)), Arrays.toString(l_data_double_arr));
//if the data length reach the max_data_length, release the buffer and invert the start flag
if(left_data_len>=max_data_len+15){
heatMapLeft.clearData();
for(int i=0; i<x_L.length; i++) {
HeatMap.DataPoint point = new HeatMap.DataPoint(x_L[i], y[i], l_data_double_arr[i]);
heatMapLeft.addData(point);
heatMapLeft.forceRefresh();
}
left_package_count++;
left_data_index= 0;
left_sensor_data_count = 0;
left_data_len=0;
is_L_insole_started=false;
}
}
}
private void onMessageSent(String message) {
// We sent a message! Handle it here.
// Toast.makeText(getApplication(), "Sent a message! Message was: " + message, Toast.LENGTH_LONG).show(); // Replace context with your context instance.
}
private void onError(Throwable error) {
// Handle the error
}
});
connectRightBtn:
connectRightBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked) {
connectDevice(R_insole_mac);
} else {
if (is_R_insole_connected) {
if (is_R_insole_started) {
right_insole_device_interface.stopInsole();
startRightBtn.setText("Start Right");
is_R_insole_started = false;
}
bluetoothManager.closeDevice(right_insole_device_interface);
is_R_insole_connected = false;
Toast.makeText(Visualization.this, "Right Insole Disconnected.", Toast.LENGTH_SHORT).show();
}
}
}
private void connectDevice (String mac){
bluetoothManager.openSerialDevice(mac)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::onConnected, this::onError);
}
#RequiresApi(api = Build.VERSION_CODES.O)
private void onConnected (BluetoothSerialDevice connectedDevice){
// You are now connected to this device!
// Here you may want to retain an instance to your device:
right_insole_device_interface = connectedDevice.toSimpleDeviceInterface();
// Listen to bluetooth events
right_insole_device_interface.setListeners(message -> onMessageReceived(message), this::onMessageSent, this::onError);
right_insole_device_interface.stopInsole();
is_R_insole_connected = true;
Toast.makeText(getApplication(), "Connected to Right Insole.", Toast.LENGTH_SHORT).show();
}
private void onMessageSent (String message){
// We sent a message! Handle it here.
}
#RequiresApi(api = Build.VERSION_CODES.N)
private void onMessageReceived (String message){
//store incoming bytes temporarily
if(!is_R_insole_started){
right_temp_bytes+=message+" ";
}
//check whether the start_bytes exits in the temporary buffer
if(!is_R_insole_started && right_temp_bytes.contains(start_bytes)){
is_R_insole_started = true;
right_temp_bytes ="";
}
//if the start_bytes are found in the temporary buffer, start storing the incoming messages in the actual buffer
if(is_R_insole_started){
right_data_len++;
if(right_data_len>15) {
right_sensor_data_count++;
if (!non_sensor_indeces.contains(right_sensor_data_count)) {
r_data_double_arr[right_data_index] = Double.parseDouble(message);
// System.out.println("NON SENSOR INDEX:" + right_data_index + " "+ message);
right_data_index++;
}
}
Date date = new Date();
rightDataDict.put(String.valueOf(formatter.format(date)),Arrays.toString(r_data_double_arr));
// if the data length reach the max_data_length, release the buffer and invert the start flag
if(right_data_len>=max_data_len+15){
heatMapRight.clearData();
for(int i=0; i<x_R.length; i++) {
HeatMap.DataPoint point = new HeatMap.DataPoint(x_R[i], y[i], r_data_double_arr[i]);
heatMapRight.addData(point);
heatMapRight.forceRefresh();
}
right_data_index= 0;
right_sensor_data_count = 0;
right_data_len=0;
is_R_insole_started=false;
}
}
}
private void onError(Throwable error) {
// Handle the error
}
});
startLeftBtn:
startLeftBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (is_L_insole_connected) {
if (is_L_insole_started) {
left_insole_device_interface.stopInsole();
is_L_insole_started = false;
startLeftBtn.setText("Start Left");
left_timer.cancel();
} else {
left_insole_device_interface.startInsole();
is_L_insole_started = true;
startLeftBtn.setText("Stop Left");
left_timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#RequiresApi(api = Build.VERSION_CODES.N)
public void run() {
// Log.d("TAG", "Hashmap: "+ leftDataDict);
sendToFirebase(leftDataDict, "Left_Insole");
}
});
}
},1000, 1000);
Toast.makeText(Visualization.this, "Left Insole Started.", Toast.LENGTH_SHORT).show();
}
}else
{
Toast.makeText(Visualization.this, "Left Insole Not Connected!", Toast.LENGTH_SHORT).show();
}
}
});
startRightBtn:
startRightBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(is_R_insole_connected) {
if (is_R_insole_started) {
right_insole_device_interface.stopInsole();
is_R_insole_started = false;
startRightBtn.setText("Start Right");
right_timer.cancel();
} else {
right_insole_device_interface.startInsole();
is_R_insole_started = true;
startRightBtn.setText("Stop Right");
right_timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
sendToFirebase(rightDataDict, "Right_Insole");
}
});
}
},1000, 1000);
Toast.makeText(Visualization.this, "Right Insole Started.", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(Visualization.this, "Right Insole Not Connected!", Toast.LENGTH_SHORT).show();
}
}
});
SendToFirebase:
public void sendToFirebase(Map<String,Object> data, String of_insole){
Date date = new Date();
db.collection("Experiments").document(PATIENT_NAME).set(patientRecord);
db.collection("Experiments")
.document(PATIENT_NAME)
.collection(of_insole)
.document(String.valueOf(formatter2.format(date)))
.set(data)
.addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void unused) {
data.clear();
Toast.makeText(Visualization.this, "Succesfully saved to Firebase", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(Visualization.this, "Failed to saved to Firebase", Toast.LENGTH_SHORT).show();
}
});
}

Start and end of playback in music player

Is it possible to set the beginning and the end of an open song in the android audio player?
So far I'am tried to get this with a range seekbar (two blue dots on a line that can be adjust) on which user mark the beginning and the end to play and when replay mode is enabled the player will play that marked section over and over again.
Preview
Many thanks for any help.
Here is the logic responsible for playing the song, that is activated when PLAY button is pressed:
playButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(mMediaPlayer != null){
if(mMediaPlayer.isPlaying()){
mMediaPlayer.pause();
playButton.setText("PLAY");
timer.shutdown();
}else{
mMediaPlayer.start();
playButton.setText("PAUSE");
timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(new Runnable() {
#Override
public void run() {
//playProgress();
if (mMediaPlayer != null) {
if (!mSeekBar.isPressed()) {
mSeekBar.setProgress(mMediaPlayer.getCurrentPosition());
}
}
}
},10,10, TimeUnit.MILLISECONDS);
}
}
}
});
When button OPEN FILE is pressed:
openButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("audio/*");
startActivityForResult(intent, PICK_FILE);
}
});
The logic responsible for "creating" the player that will form when the song is opened:
public void createMediaPlayer(Uri uri){
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioAttributes(
new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_MEDIA)
.build()
);
try {
mMediaPlayer.setDataSource(getApplicationContext(), uri);
mMediaPlayer.prepare();
title.setText(getNameFromUri(uri));
playButton.setEnabled(true);
mRangeSeekBar.setNotifyWhileDragging(true);
mSeekBar.setMax(mMediaPlayer.getDuration());
mRangeSeekBar.setRangeValues(0, mMediaPlayer.getDuration());
max = mMediaPlayer.getDuration();
mSeekBar.setMax(mMediaPlayer.getDuration());
//mSeekBar.setProgress(0);
long total_secs = TimeUnit.SECONDS.convert(max, TimeUnit.MILLISECONDS);
long mins = TimeUnit.MINUTES.convert(total_secs, TimeUnit.SECONDS);
long secs = total_secs - (mins*60);
duration = mins + ":" + secs;
elapse.setText("00:00 / " + duration);
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
releaseMediaPlayer();
}
});
} catch (IOException e){
title.setText(e.toString());
}
}
Here is code for this double slider that is supposed to set the beginning and end:
mRangeSeekBar.setOnRangeSeekBarChangeListener(new RangeSeekBar.OnRangeSeekBarChangeListener<Integer>() {
#Override
public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) {
mRangeSeekBar.setNotifyWhileDragging(true);
mMediaPlayer.seekTo(minValue);
max = maxValue;
String infoMax = String.valueOf(max);
Log.i("MAX", infoMax);
}
});
And here for this green bar with one point representing the duration of the song:
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (mMediaPlayer != null){
int millis = mMediaPlayer.getCurrentPosition();
long total_secs = TimeUnit.SECONDS.convert(millis, TimeUnit.MILLISECONDS);
long mins = TimeUnit.MINUTES.convert(total_secs, TimeUnit.SECONDS);
long secs = total_secs - (mins*60);
elapse.setText(mins + ":" + secs + " / " + duration);

SeekBar can work but then it can't auto run the seekbar progress

My SeekBar can work but then it can't auto-run the SeekBar progress
when I clicked on the SeekBar I will auto move to the position with an overlay player. and the runnable thread does not on the SeekBar
the line below is the code of my SeekBar
positionBar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser && progress != 100) {
player.getDuration();
player.seekTo(progress);
positionBar.setProgress(progress);
Toast.makeText(getApplicationContext(), "seekbar progress: " + progress, Toast.LENGTH_SHORT).show();
}else {
progress = 0;
positionBar.setProgress(progress);
Song nextSong = songCollection.getNextSong(songId);
if(nextSong != null){
songId = nextSong.getId();
title = nextSong.getTitle();
artist = nextSong.getArtist();
fileLink = nextSong.getFileLink();
coverArt = nextSong.getCoverArt();
url = BASE_URL + fileLink;
displaySong(title,artist,coverArt);
stopActivities();
}
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
//
Toast.makeText(getApplicationContext(),"seekbar touch started!", Toast.LENGTH_SHORT).show();
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// player.seekTo(musicPostion);
Toast.makeText(getApplicationContext(),"seekbar touch stop!", Toast.LENGTH_SHORT).show();
}
}
);
new Thread(new Runnable() {
#Override
public void run() {
while(player != null){
try{
Message msg = new Message();
msg.what = player.getCurrentPosition();
handler.sendMessage(msg);
positionBar.setProgress(player.getCurrentPosition());
Thread.sleep(1000);
}catch (InterruptedException e){
}
}
}
}).start();
}
private Handler handler = new Handler(){
#Override
public void handleMessage(Message msg) {
int currentPosition = msg.what;
positionBar.setProgress(currentPosition);
}
};`

How to set a minimum amount of time between steps for a Step Counter?

I created a step counter and I noticed that you are able to make it count six steps in under a second by shaking the device.
How could you set a minimum amount of time between each step to make it more accurate? For example make it so that there has to be at least a quarter of a second that went by before a next step can be counted as a step. If this isn't a good solution to making it more accurate then please let me know.
Below is the code, it includes a timer that just counts the total amount of steps every ten minutes, so just ignore that.
public class StepCounterManager implements SensorEventListener{
boolean timerStarted = false;
private float initCount, finalCount, currentCount;
public Activity activity;
private boolean activityRunning;
private SensorManager sensorManager;
public StepCounterManager(Activity activity){
this.activity = activity;
}
public void stepCounterInit(){
sensorManager = (SensorManager) activity.getSystemService(Context.SENSOR_SERVICE);
timer();
timerStarted = true;
}
//timer starts at the start of app and restarts every 10 minutes
public void timer(){
Timer t = new Timer(false);
Toast.makeText(this.activity, "timer started", Toast.LENGTH_SHORT).show();
t.schedule(new TimerTask() {
#Override
public void run() {
activity.runOnUiThread(new Runnable() {
public void run() {
getFinalStepCount();
}
});
}
}, 600000);
}
public void register(){
activityRunning = true;
Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
if(countSensor != null){
sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
} else {
Toast.makeText(this.activity, "Count sensor not available", Toast.LENGTH_SHORT).show();
}
}
public void unRegister(){
activityRunning = false;
}
#Override
public void onSensorChanged(SensorEvent event) {
if(activityRunning){
currentCount = event.values[0];
System.out.println(currentCount);
}
if(timerStarted){
resetInitialStepCount();
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
//a new initial count has to be made because the step count can only reset after a reboot
public void resetInitialStepCount(){
initCount = currentCount;
Toast.makeText(this.activity, "initial count is: " + initCount, Toast.LENGTH_SHORT).show();
timerStarted = false;
}
public void getFinalStepCount(){
finalCount = currentCount-initCount;
Toast.makeText(this.activity, "final count is: " + finalCount, Toast.LENGTH_SHORT).show();
resetInitialStepCount();
timer();
//todo: send finalcount to database
}
}
public class StepCounterManager implements SensorEventListener{
boolean timerStarted = false;
private float initCount, finalCount, currentCount;
public Activity activity;
private boolean activityRunning;
private boolean hasRecorded;
private SensorManager sensorManager;
private int storedSteps;
public StepCounterManager(Activity activity){
this.activity = activity;
this.hasRecorded = false;
this.storedSteps = 0;
}
public void stepCounterInit(){
sensorManager = (SensorManager) activity.getSystemService(Context.SENSOR_SERVICE);
timer();
timerStarted = true;
}
//timer starts at the start of app and restarts every 10 minutes
public void timer(){
Timer t = new Timer(false);
Toast.makeText(this.activity, "timer started", Toast.LENGTH_SHORT).show();
t.schedule(new TimerTask() {
#Override
public void run() {
activity.runOnUiThread(new Runnable() {
public void run() {
getFinalStepCount();
}
});
}
}, 600000);
}
public void register(){
activityRunning = true;
Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
if(countSensor != null){
sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
} else {
Toast.makeText(this.activity, "Count sensor not available", Toast.LENGTH_SHORT).show();
}
}
public void unRegister(){
activityRunning = false;
}
#Override
public void onSensorChanged(SensorEvent event) {
if(hasRecorded == false){
hasRecorded = true;
BlockRecording();
if(activityRunning){
int TempCurrent = event.values[0] - storedSteps;
currentCount = currentCount + (event.values[0] - TempCurrent);
System.out.println(currentCount);
}
if(timerStarted){
resetInitialStepCount();
}
storedSteps = event.values[0];
}else{
}
}
public void BlockRecording(){
Timer t = new Timer(false);
t.schedule(new TimerTask() {
#Override
public void run() {
activity.runOnUiThread(new Runnable() {
public void run() {
hasRecorded = false;
}
});
}
}, 250);
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
//a new initial count has to be made because the step count can only reset after a reboot
public void resetInitialStepCount(){
initCount = currentCount;
Toast.makeText(this.activity, "initial count is: " + initCount, Toast.LENGTH_SHORT).show();
timerStarted = false;
}
public void getFinalStepCount(){
finalCount = currentCount-initCount;
Toast.makeText(this.activity, "final count is: " + finalCount, Toast.LENGTH_SHORT).show();
resetInitialStepCount();
timer();
//todo: send finalcount to database
}
}

Android - SeekBar and MediaPlayer

I needed to connect my SeekBar with my MediaPlayer in my App.
I set up the SeekBar via xml like this:
<SeekBar
android:id="#+id/song_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"/>
and followed this SO answer to implement it.
This is my code:
public class Song_main extends AppCompatActivity {
private final int SONG_REQUEST_CODE = 1;
private Uri song;
private TextView selectSong;
private SeekBar seekBar;
private Handler handler;
private MediaPlayer mediaPlayer;
private boolean repeatPressedTwice = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_bar_song_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.song_main_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
seekBar = (SeekBar) findViewById(R.id.song_seekbar);
handler = new Handler();
notSelected();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.song, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.song_plus) {
Intent selectIntent = new Intent(Intent.ACTION_GET_CONTENT);
selectIntent.setType("audio/*");
startActivityForResult(selectIntent, SONG_REQUEST_CODE);
}
return true;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SONG_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
if ((data != null) && (data.getData()!=null)) {
song = data.getData();
setup();
}
}
}
private void notSelected() {
selectSong = (TextView) findViewById(R.id.select_song_textview);
selectSong.setText(getResources().getString(R.string.song_not_selected));
}
public void onPlayButtonClicked(View v) {
ImageButton pb = (ImageButton) findViewById(R.id.song_play_button);
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
pb.setImageResource(R.drawable.pause);
updateSeekBar();
} else {
mediaPlayer.pause();
pb.setImageResource(R.drawable.ic_play_arrow_white_24dp);
}
}
public void onControlsClicked(View v) {
if (v.getId() == R.id.fast_forward) {
int pos = mediaPlayer.getCurrentPosition();
pos += 1500;
mediaPlayer.seekTo(pos);
}
else if (v.getId() == R.id.fast_backward) {
int pos = mediaPlayer.getCurrentPosition();
pos -= 1500;
mediaPlayer.seekTo(pos);
}
else if (v.getId() == R.id.skip_backward) {
mediaPlayer.seekTo(0);
}
}
public void onRepeatClicked(View v) {
if (!repeatPressedTwice) {
// TODO: change visual color of repeat button
mediaPlayer.setLooping(true);
Toast.makeText(this, "repeat enabled", Toast.LENGTH_SHORT).show();
repeatPressedTwice = true;
} else {
mediaPlayer.setLooping(false);
}
}
private void setup() {
/* the song has been select setup the interface */
/* displays song name in title */
TextView titleView = (TextView) findViewById(R.id.song_appbar_title);
String songName;
ContentResolver contentResolver = this.getContentResolver();
Cursor cursor = contentResolver.query(song, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
songName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
titleView.setText(songName);
}
/* removes the notSelected String */
selectSong.setVisibility(View.GONE);
/* setup media player */
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(getApplicationContext(), song);
mediaPlayer.prepareAsync();
} catch (IOException e) {
Toast.makeText(this, "file not found", Toast.LENGTH_SHORT).show();
}
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
/* show media player layout */
RelativeLayout mpl = (RelativeLayout) findViewById(R.id.media_player_layout);
mpl.setVisibility(View.VISIBLE);
mediaPlayer.start();
ImageButton pb = (ImageButton) findViewById(R.id.song_play_button);
pb.setImageResource(R.drawable.pause);
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
ImageButton pb = (ImageButton) findViewById(R.id.song_play_button);
pb.setImageResource(R.drawable.ic_play_arrow_white_24dp);
}
});
seekBar = (SeekBar) findViewById(R.id.song_seekbar);
seekBar.setMax(mediaPlayer.getDuration());
updateSeekBar();
}
private void updateSeekBar() {
seekBar.setProgress(mediaPlayer.getCurrentPosition()/1000);
handler.postDelayed(runnable, 1000);
}
Runnable runnable = new Runnable() {
#Override
public void run() {
updateSeekBar();
}
};
#Override
public void onStop() {
super.onStop();
if (mediaPlayer!=null)
mediaPlayer.stop();
}
}
The process starts from the onOptionsItemSelected method.
The seekBar behaves correctly, it increments every second. The problem now is that it finishes way before the song finishes.
I tried adding
seekBar.setMax(mediaPlayer.getDuration());
in the setup method, but that causes the bar not to move at all.
You need to define separate Runnable and trigger it every x miliseconds (depends on you) once MediaPlayer starts.
Define a function updateSeekbar like,
private void updateSeekBar() {
audioSeek.setProgress(player.getCurrentPosition());
txtCurrentTime.setText(milliSecondsToTimer(player.getCurrentPosition()));
seekHandler.postDelayed(runnable, 50);
}
And Runnable
Runnable runnable = new Runnable() {
#Override
public void run() {
updateSeekBar();
}
};
Now you just have to call updateSeekbar once when playing starts. In your case:
public void onPlayButtonClicked(View v) {
ImageButton pb = (ImageButton) findViewById(R.id.song_play_button);
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
pb.setImageResource(R.drawable.pause);
updateSeekBar();
} else {
mediaPlayer.pause();
pb.setImageResource(R.drawable.ic_play_arrow_white_24dp);
}
}
FYI
Function milliSecondsToTimer works as follows
private String milliSecondsToTimer(long milliseconds) {
String finalTimerString = "";
String secondsString = "";
// Convert total duration into time
int hours = (int) (milliseconds / (1000 * 60 * 60));
int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60);
int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000);
// Add hours if there
if (hours > 0) {
finalTimerString = hours + ":";
}
// Prepending 0 to seconds if it is one digit
if (seconds < 10) {
secondsString = "0" + seconds;
} else {
secondsString = "" + seconds;
}
finalTimerString = finalTimerString + minutes + ":" + secondsString;
// return timer string
return finalTimerString;
}
UPDATE
You have called setMax at the wrong place. Update setup() function as follows
private void setup() {
/* the song has been select setup the interface */
/* displays song name in title */
TextView titleView = (TextView) findViewById(R.id.song_appbar_title);
String songName;
ContentResolver contentResolver = this.getContentResolver();
Cursor cursor = contentResolver.query(song, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
songName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
titleView.setText(songName);
}
/* removes the notSelected String */
selectSong.setVisibility(View.GONE);
/* setup media player */
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(getApplicationContext(), song);
mediaPlayer.prepareAsync();
} catch (IOException e) {
Toast.makeText(this, "file not found", Toast.LENGTH_SHORT).show();
}
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
/* show media player layout */
seekBar.setMax(mediaPlayer.getDuration());
RelativeLayout mpl = (RelativeLayout) findViewById(R.id.media_player_layout);
mpl.setVisibility(View.VISIBLE);
mediaPlayer.start();
updateSeekBar();
ImageButton pb = (ImageButton) findViewById(R.id.song_play_button);
pb.setImageResource(R.drawable.pause);
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
ImageButton pb = (ImageButton) findViewById(R.id.song_play_button);
pb.setImageResource(R.drawable.ic_play_arrow_white_24dp);
}
});
}
You have implement OnSeekBarChangeListener and in onCreate() add the below line:-
seekBar = (SeekBar) findViewById(R.id.seekBar);
And override the onProgressChanged() method , in this method you can set the progress in the seekbar using the below line:
mPlayer.seekTo(progress);
seekBar.setProgress(progress);
or
After you initialise your MediaPlayer and for example press the play button, you should create an handler and post runnable so you can update your SeekBar (in the UI thread itself) with the current position of your MediaPlayer like this :
private Handler mHandler = new Handler();
//Make sure you update Seekbar on UI thread
MainActivity.this.runOnUiThread(new Runnable(){
#Override
public void run() {
if(mMediaPlayer != null){
int mCurrentPosition = mMediaPlayer.getCurrentPosition() / 1000;
mSeekBar.setProgress(mCurrentPosition);
}
mHandler.postDelayed(this, 1000);
}
});
and update that value every second.
If you need to update the MediaPlayer's position while user drag your SeekBar you should add OnSeekBarChangeListener to your SeekBar and do it there :
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(mMediaPlayer != null && fromUser){
mMediaPlayer.seekTo(progress * 1000);
}
}
});
you need to update your Seek bar when you play a song
public void updateProgressBar() {
runOnUiThread(new Runnable() {
#Override
public void run() {
mHandler.postDelayed(mUpdateTimeTask, 100);
}
});
}
Below Runnable method to update seekbar
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
if (MusicService.isRunning()) {
duration = MusicService.getDur();
long currSongPosition = MusicService.getPosn();
totTime.setText(Utility.milliSecondsToTimer(duration));
fromTime.setText(Utility.milliSecondsToTimer(currSongPosition));
int progress = Utility.getProgressPercentage(currSongPosition, duration);
songProgressBar.setProgress(progress);
updateProgressBar();
}
}
};
Using this below function you can get progress percentage from song current position and song duration
public static int getProgressPercentage(long currentDuration, long totalDuration) {
Double percentage;
long currentSeconds = (int) (currentDuration / 1000);
long totalSeconds = (int) (totalDuration / 1000);
percentage = (((double) currentSeconds) / totalSeconds) * 100;
return percentage.intValue();
}

Categories