আমি অ্যান্ড্রয়েডে একটি সাধারণ সঙ্গীত প্লেয়ার তৈরি করেছি। প্রতিটি গানের দর্শনটিতে একটি সিকবার রয়েছে, এটি এভাবে প্রয়োগ করা হয়েছে:
public class Song extends Activity implements OnClickListener,Runnable {
private SeekBar progress;
private MediaPlayer mp;
// ...
private ServiceConnection onService = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder rawBinder) {
appService = ((MPService.LocalBinder)rawBinder).getService(); // service that handles the MediaPlayer
progress.setVisibility(SeekBar.VISIBLE);
progress.setProgress(0);
mp = appService.getMP();
appService.playSong(title);
progress.setMax(mp.getDuration());
new Thread(Song.this).start();
}
public void onServiceDisconnected(ComponentName classname) {
appService = null;
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.song);
// ...
progress = (SeekBar) findViewById(R.id.progress);
// ...
}
public void run() {
int pos = 0;
int total = mp.getDuration();
while (mp != null && pos<total) {
try {
Thread.sleep(1000);
pos = appService.getSongPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
progress.setProgress(pos);
}
}
এটি কাজ করে। এখন আমি একটি টাইমার গানের অগ্রগতি সেকেন্ড / মিনিট গণনা করতে চান। তাই আমি একটি করা TextView
লেআউট সঙ্গে এটি পেতে findViewById()
মধ্যে onCreate()
, এবং এই করা run()
পর progress.setProgress(pos)
:
String time = String.format("%d:%d",
TimeUnit.MILLISECONDS.toMinutes(pos),
TimeUnit.MILLISECONDS.toSeconds(pos),
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(
pos))
);
currentTime.setText(time); // currentTime = (TextView) findViewById(R.id.current_time);
তবে সেই শেষ লাইনটি আমাকে ব্যতিক্রম দেয়:
android.view.ViewRoot $ CalledFromWrongThreadException: কেবলমাত্র মূল থ্রেড যা একটি ভিউ হায়ারার্কি তৈরি করেছে কেবল তার দর্শনগুলিকে স্পর্শ করতে পারে।
তবুও আমি এখানে মূলত একই জিনিসটি করছি SeekBar
- যেমনটি তৈরি করছি - ভিউ তৈরি করা onCreate
, তারপরে স্পর্শ করা run()
- এবং এটি আমাকে এই অভিযোগ দেয় না।
error.setText(res.toString());
রান () পদ্ধতির অভ্যন্তরে একটি কাজ করতে চেয়েছিলাম , তবে আমি