使用eclipse编写一个Android实验音乐播放器代码
时间: 2023-06-14 19:03:30 浏览: 135
基于Eclipse的Android音乐播放器
5星 · 资源好评率100%
以下是一个简单的Android音乐播放器的代码示例,使用Eclipse开发:
MainActivity.java
```java
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button playButton;
private Button pauseButton;
private SeekBar seekBar;
private TextView currentSongTime;
private TextView totalSongTime;
private MediaPlayer mediaPlayer;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playButton = (Button) findViewById(R.id.play_button);
pauseButton = (Button) findViewById(R.id.pause_button);
seekBar = (SeekBar) findViewById(R.id.seek_bar);
currentSongTime = (TextView) findViewById(R.id.current_song_time);
totalSongTime = (TextView) findViewById(R.id.total_song_time);
handler = new Handler();
mediaPlayer = MediaPlayer.create(this, R.raw.song);
seekBar.setMax(mediaPlayer.getDuration());
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.start();
updateSeekBar();
}
});
pauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.pause();
}
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser){
mediaPlayer.seekTo(progress);
updateSeekBar();
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
private void updateSeekBar() {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
currentSongTime.setText(millisecondToString(mediaPlayer.getCurrentPosition()));
totalSongTime.setText(millisecondToString(mediaPlayer.getDuration()));
if(mediaPlayer.isPlaying()){
Runnable runnable = new Runnable() {
@Override
public void run() {
updateSeekBar();
}
};
handler.postDelayed(runnable, 1000);
}
}
private String millisecondToString(int millisecond){
int second = millisecond/1000;
int minute = second/60;
second = second%60;
return String.format("%02d:%02d", minute, second);
}
}
```
activity_main.xml
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/current_song_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:paddingLeft="10dp"
android:paddingRight="10dp"/>
<TextView
android:id="@+id/total_song_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:paddingLeft="10dp"
android:paddingRight="10dp"/>
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/current_song_time"
android:layout_below="@id/play_button"/>
<Button
android:id="@+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<Button
android:id="@+id/pause_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
```
在这个实现中,我们使用了Android提供的MediaPlayer来播放音乐。SeekBar用于显示歌曲播放进度,并允许用户跳转到歌曲的任意位置。我们还使用Handler来定期更新SeekBar和当前播放时间。
阅读全文