1
私は曲が終わったときに自動的に次の曲に切り替えるつもりです、それは自動的に再生する必要があります着メロに従います。メディアプレーヤーを次のトラックに自動的に切り替える方法は?
:現在時刻の曲が動作しないtexеview setOnCompletionListenerの-doesで出力する必要があり、それが現在は//変数
int[] soundsRawResIds = new int[]{R.raw.belarus, R.raw.russian, R.raw.japan,
R.raw.litva, R.raw.england, R.raw.finlandia, R.raw.france};
private final Handler handler = new Handler();
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
initViews();
currenttime=(TextView)findViewById(R.id.timeSound);
buttonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
}
private void initViews() {
mediaPlayer = MediaPlayer.create(this, soundsRawResIds[0]);
seekBar = (SeekBar) findViewById(R.id.seekBar);
seekBar.setMax(mediaPlayer.getDuration());
seekBar.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
seekChange(v);
return false;
}
});
}
private void seekChange(View v){
if(mediaPlayer.isPlaying()){
seekBar = (SeekBar)v;
mediaPlayer.seekTo(seekBar.getProgress());
}
}
public void playAndStop(View v){
if (buttonPlayStop.getText() == getString(R.string.play_str)) {
buttonPlayStop.setText(getString(R.string.pause_str));
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
index++;
mp.reset();
if (index < soundsRawResIds.length) {
try {
AssetFileDescriptor afd = getResources().openRawResourceFd(soundsRawResIds[index]);
if (afd != null) {
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mp.prepare();
}
} catch (Exception ex) {
// report a crash
}
} else {
// done with media player
mp.release();
mp = null;
}
}
});
mediaPlayer.start();
startPlayProgressUpdater();
}else {
buttonPlayStop.setText(getString(R.string.play_str));
mediaPlayer.pause();
}
}
public void startPlayProgressUpdater() {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
if (mediaPlayer.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
startPlayProgressUpdater();
if(seekBar.getProgress()==100){
for(int index=0;index<soundsRawResIds.length;index++){
index++;
}
}
}
};
handler.postDelayed(notification,1000);
}else{
mediaPlayer.pause();
buttonPlayStop.setText(getString(R.string.play_str));
seekBar.setProgress(0);
}
}
}
私のXMLコードを終了した次のトラックに行きません
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SeekBar
android:layout_width="fill_parent"
android:progress="0"
android:max="100"
android:layout_height="wrap_content"
android:id="@+id/seekBar"/>
<Button
android:text="@string/play_str"
android:textSize="15pt"
android:textStyle="bold"
android:onClick="playAndStop"
android:id="@+id/ButtonPlayStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="-3dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/timeSound"
android:textSize="30sp"
android:text="00:00"
/>
</LinearLayout>
私はこのメソッドを追加しましたが、私の曲は停止したときに繰り返します。 私は配列から音楽を聴こうとしています – upward
私は答えを更新しました。助けてくれますか? –
新しいプレーヤーMediaPlayerを作成する erro -38 – upward