また置いときます。
http://sattva.asasvata.net/Entry/18/
一応修正版
http://sattva.asasvata.net/Entry/19/
下記の内容は修正前の物です。
修正後は、handlerは、Activityと同じファイルに移動してます。
やってることは同じです。まあ、さまよってるのも何かの参考になれば。
今回の主な追加はhandlerです。
package net.asasvata.sdmediaplayer;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
public class TickHandler extends Handler {
private boolean playFlag;
private MediaPlayer mp;
private TextView textView;
public TickHandler(MediaPlayer mp, TextView textView) {
super();
this.mp = mp;
this.textView = textView;
}
public void setPlayFlag(boolean flag) {
playFlag = flag;
}
@Override
public void handleMessage(Message msg) {
if (playFlag) {
int duration = mp.getDuration();
int currentPosition = mp.getCurrentPosition();
String durationTime = timeText(duration);
String currentTime = timeText(currentPosition);
textView.setText(currentTime + "/" + durationTime);
} else {
textView.setText("00:00/00:00");
}
if (this != null) this.sleep(100);
}
public void sleep(long delayMills) {
removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMills);
}
private String timeText(int time) {
int m = (int)(time / 60000);
int s = (int)(time % 60000 / 1000);
String str = "00:00";
if (m > 10 && s > 10) str = "" + m + ":" + s;
else if (m < 10 && s >= 10) str = "0" + m + ":" + s;
else if (m >= 10 && s < 10) str = "" + m + ":" + "0" + s;
else if (m < 10 && s < 10) str = "0" + m + ":" + "0" + s;
return str;
}
}
まあ、本に乗ってたのを少し変えただけですが。
timeTextは、durationとかがミリ秒で、かえされるので
(例 3分なら180000)それを、修正してます。
ただstopしたときに、なんかぴくぴくするので、
ちょっと修正が必要かも。
エミュレーターだと大丈夫なんですが、is01はぴくぴく。
まあ、ぼちぼち。
前のでは、chronometerを使ってごまかしたんですが、
handlerも、使ってみるかと。
どうやってごまかしたかは、書きませんが。
その時のを元にstopwatchっぽいものを、作ったので
次に載せときます。
秒単位でしか表示されない、ぽいだけのですが。
修正後のhandlerを、さらに下記のものに変更すると、
忙しい感じになります。
public class TickHandler extends Handler {
@Override
public void handleMessage(Message msg) {
if (musicPlayer.isPlaying() || pauseFlag) {
int duration = musicPlayer.getDuration();
int currentPosition = musicPlayer.getCurrentPosition();
String durationTime = timeText(duration);
String currentTime = timeCountDown(duration, currentPosition);
timeText.setText(currentTime + "/" + durationTime);
} else {
timeText.setText(timeCountDown(0, 0) + "/" + timeCountDown(0, 0));
}
if (this != null) this.sleep(10);
}
public void sleep(long delayMills) {
removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMills);
}
private String timeText(int time) {
int m = (int)(time / 60000);
int s = (int)(time % 60000 / 1000);
String str = "";
if (m > 10 && s > 10) str = "" + m + ":" + s;
else if (m < 10 && s >= 10) str = "0" + m + ":" + s;
else if (m >= 10 && s < 10) str = "" + m + ":" + "0" + s;
else if (m < 10 && s < 10) str = "0" + m + ":" + "0" + s;
return str;
}
private String timeCountDown(int duration, int currentPosition) {
int time = duration - currentPosition;
int m = (int)(time / 60000);
int s = (int)(time % 60000 / 1000);
int cs = (int)((time - m*60000 - s*1000) / 10);
String str = "";
if (m >= 10 && s >= 10 && cs >= 10) str = "" + m + ":" + s + ":" + cs;
else if (m < 10 && s >= 10 && cs >= 10) str = "0" + m + ":" + s + ":" + cs;
else if (m >= 10 && s < 10 && cs >= 10) str = "" + m + ":" + "0" + s + ":" + cs;
else if (m < 10 && s < 10 && cs >= 10) str = "0" + m + ":" + "0" + s + ":" + cs;
else if (m >= 10 && s >= 10 && cs < 10) str = "" + m + ":" + s + ":" + "0" + cs;
else if (m < 10 && s >= 10 && cs < 10) str = "0" + m + ":" + s + ":" + "0" + cs;
else if (m >= 10 && s < 10 && cs < 10) str = "" + m + ":" + "0" + s + ":" + "0" + cs;
else if (m < 10 && s < 10 && cs < 10) str = "0" + m + ":" + "0" + s + ":" + "0" + cs;
return str;
}
}
修正後のhandlerを、さらに下記のものに変更すると、
忙しい感じになります。
public class TickHandler extends Handler {
@Override
public void handleMessage(Message msg) {
if (musicPlayer.isPlaying() || pauseFlag) {
int duration = musicPlayer.getDuration();
int currentPosition = musicPlayer.getCurrentPosition();
String durationTime = timeText(duration);
String currentTime = timeCountDown(duration, currentPosition);
timeText.setText(currentTime + "/" + durationTime);
} else {
timeText.setText(timeCountDown(0, 0) + "/" + timeCountDown(0, 0));
}
if (this != null) this.sleep(10);
}
public void sleep(long delayMills) {
removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMills);
}
private String timeText(int time) {
int m = (int)(time / 60000);
int s = (int)(time % 60000 / 1000);
String str = "";
if (m > 10 && s > 10) str = "" + m + ":" + s;
else if (m < 10 && s >= 10) str = "0" + m + ":" + s;
else if (m >= 10 && s < 10) str = "" + m + ":" + "0" + s;
else if (m < 10 && s < 10) str = "0" + m + ":" + "0" + s;
return str;
}
private String timeCountDown(int duration, int currentPosition) {
int time = duration - currentPosition;
int m = (int)(time / 60000);
int s = (int)(time % 60000 / 1000);
int cs = (int)((time - m*60000 - s*1000) / 10);
String str = "";
if (m >= 10 && s >= 10 && cs >= 10) str = "" + m + ":" + s + ":" + cs;
else if (m < 10 && s >= 10 && cs >= 10) str = "0" + m + ":" + s + ":" + cs;
else if (m >= 10 && s < 10 && cs >= 10) str = "" + m + ":" + "0" + s + ":" + cs;
else if (m < 10 && s < 10 && cs >= 10) str = "0" + m + ":" + "0" + s + ":" + cs;
else if (m >= 10 && s >= 10 && cs < 10) str = "" + m + ":" + s + ":" + "0" + cs;
else if (m < 10 && s >= 10 && cs < 10) str = "0" + m + ":" + s + ":" + "0" + cs;
else if (m >= 10 && s < 10 && cs < 10) str = "" + m + ":" + "0" + s + ":" + "0" + cs;
else if (m < 10 && s < 10 && cs < 10) str = "0" + m + ":" + "0" + s + ":" + "0" + cs;
return str;
}
}
stopwatchに使えそう。普通に表示させたら。
0 件のコメント:
コメントを投稿