1、 制作一个完整的音乐播放器,包含两个界面:音乐列表界面和播放界面 音乐列表界面 播放界面 2、 任务要求 1)音乐列表界面 a)该activity使用LinearLayout的垂直布局 b)在LinearLayout中使用ListView控件 c)点击ListView控件的一个子项,跳转至音乐播放界面 2)音乐播放界面 a)该activity使用LinearLayout的垂直布局 b)顶部添加 button控件,显示为“返回播放列表” c)添加一个显示歌曲名称的Textview d)添加一个显示歌手名的Textview e)添加一个imageView,请求网络显示对应的海报图片 f)添加一个seekbar和两个分别显示总时长与当前播放时间的Textview g)底部添加三个按钮,分别为上一首、播放、暂停、下一首。 安卓代码
时间: 2024-02-18 09:00:03 浏览: 90
好的,根据您的任务要求,以下是一个简单的安卓代码示例,用于实现一个基本的音乐播放器。注意,这只是一个示例代码,具体的实现方式可能会有所不同。
音乐列表界面的Activity:
```
public class MusicListActivity extends AppCompatActivity {
private ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music_list);
mListView = findViewById(R.id.list_view);
mListView.setAdapter(new MusicListAdapter(this));
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MusicListActivity.this, MusicPlayActivity.class);
intent.putExtra("position", position);
startActivity(intent);
}
});
}
}
```
音乐播放界面的Activity:
```
public class MusicPlayActivity extends AppCompatActivity {
private MediaPlayer mMediaPlayer;
private ImageView mAlbumImage;
private TextView mSongNameText;
private TextView mArtistText;
private TextView mCurrentTimeText;
private TextView mTotalTimeText;
private SeekBar mSeekBar;
private Button mPlayButton;
private Button mPauseButton;
private Button mPrevButton;
private Button mNextButton;
private int mPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music_play);
mPosition = getIntent().getIntExtra("position", 0);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mSeekBar.setMax(mMediaPlayer.getDuration());
mTotalTimeText.setText(formatTime(mMediaPlayer.getDuration()));
mp.start();
updateSeekBar();
}
});
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
playNext();
}
});
mAlbumImage = findViewById(R.id.album_image);
mSongNameText = findViewById(R.id.song_name_text);
mArtistText = findViewById(R.id.artist_text);
mCurrentTimeText = findViewById(R.id.current_time_text);
mTotalTimeText = findViewById(R.id.total_time_text);
mSeekBar = findViewById(R.id.seek_bar);
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
mMediaPlayer.seekTo(progress);
updateSeekBar();
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Do nothing
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Do nothing
}
});
mPlayButton = findViewById(R.id.play_button);
mPlayButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
mPlayButton.setVisibility(View.VISIBLE);
mPauseButton.setVisibility(View.GONE);
} else {
mMediaPlayer.start();
mPlayButton.setVisibility(View.GONE);
mPauseButton.setVisibility(View.VISIBLE);
updateSeekBar();
}
}
});
mPauseButton = findViewById(R.id.pause_button);
mPauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
mPlayButton.setVisibility(View.VISIBLE);
mPauseButton.setVisibility(View.GONE);
}
}
});
mPrevButton = findViewById(R.id.prev_button);
mPrevButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playPrev();
}
});
mNextButton = findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playNext();
}
});
loadSongInfo();
}
private void loadSongInfo() {
// 根据mPosition从音乐列表中获取歌曲信息,包括歌曲名称、歌手名和海报图片等
// 然后将这些信息显示在相应的控件上
}
private void playPrev() {
mPosition--;
if (mPosition < 0) {
mPosition = MusicListData.getInstance().getSize() - 1;
}
playSong();
}
private void playNext() {
mPosition++;
if (mPosition >= MusicListData.getInstance().getSize()) {
mPosition = 0;
}
playSong();
}
private void playSong() {
mMediaPlayer.reset();
// 根据mPosition从音乐列表中获取歌曲的url,并设置给MediaPlayer
try {
mMediaPlayer.setDataSource(songUrl);
mMediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
loadSongInfo();
}
private void updateSeekBar() {
if (mMediaPlayer.isPlaying()) {
mCurrentTimeText.setText(formatTime(mMediaPlayer.getCurrentPosition()));
mSeekBar.setProgress(mMediaPlayer.getCurrentPosition());
mHandler.postDelayed(mRunnable, 1000);
}
}
private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
updateSeekBar();
}
};
private String formatTime(int time) {
int totalSeconds = time / 1000;
int minutes = totalSeconds / 60;
int seconds = totalSeconds % 60;
return String.format("%02d:%02d", minutes, seconds);
}
@Override
protected void onDestroy() {
super.onDestroy();
mMediaPlayer.release();
mHandler.removeCallbacks(mRunnable);
}
}
```
这是一个简单的示例代码,您可以根据自己的实际需求进行修改和完善。希望能对您有所帮助!
阅读全文