Android Studio课程设计音乐播放器 代码
时间: 2023-11-01 17:06:46 浏览: 93
以下是一个简单的Android Studio课程设计音乐播放器的代码示例:
XML布局文件:
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/controller"
android:layout_marginBottom="10dp"
android:divider="@null"
android:listSelector="@drawable/list_selector" />
<RelativeLayout
android:id="@+id/controller"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:background="#DDDDDD"
android:padding="10dp">
<ImageView
android:id="@+id/prevBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_skip_previous_black_36dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_marginRight="20dp"/>
<ImageView
android:id="@+id/playBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_play_arrow_black_36dp"
android:layout_centerInParent="true"/>
<ImageView
android:id="@+id/nextBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_skip_next_black_36dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="20dp"/>
<TextView
android:id="@+id/titleView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="18sp"
android:textColor="#333333"
android:layout_centerInParent="true"
android:layout_toLeftOf="@id/nextBtn"
android:layout_toRightOf="@id/prevBtn"/>
</RelativeLayout>
</RelativeLayout>
```
Java代码:
```java
public class MainActivity extends AppCompatActivity {
private ListView listView;
private ImageView prevBtn, playBtn, nextBtn;
private TextView titleView;
private MediaPlayer mediaPlayer;
private int currentSongIndex = 0;
private boolean isPlaying = false;
private ArrayList<String> songList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化控件
listView = findViewById(R.id.listView);
prevBtn = findViewById(R.id.prevBtn);
playBtn = findViewById(R.id.playBtn);
nextBtn = findViewById(R.id.nextBtn);
titleView = findViewById(R.id.titleView);
// 添加音乐文件到列表
songList.add("song1.mp3");
songList.add("song2.mp3");
songList.add("song3.mp3");
// 设置ListView适配器
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
R.layout.list_item, R.id.songName, songList);
listView.setAdapter(adapter);
// ListView点击事件处理
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
currentSongIndex = position;
playSong();
}
});
// 播放按钮点击事件处理
playBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isPlaying) {
pauseSong();
} else {
playSong();
}
}
});
// 上一首按钮点击事件处理
prevBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (currentSongIndex > 0) {
currentSongIndex--;
playSong();
}
}
});
// 下一首按钮点击事件处理
nextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (currentSongIndex < songList.size() - 1) {
currentSongIndex++;
playSong();
}
}
});
// 初始化MediaPlayer
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
// 设置MediaPlayer监听器
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
isPlaying = true;
playBtn.setImageResource(R.drawable.ic_pause_black_36dp);
titleView.setText(songList.get(currentSongIndex));
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
isPlaying = false;
playBtn.setImageResource(R.drawable.ic_play_arrow_black_36dp);
}
});
}
// 播放音乐
private void playSong() {
try {
mediaPlayer.reset();
AssetFileDescriptor afd = getAssets().openFd(songList.get(currentSongIndex));
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
// 暂停音乐
private void pauseSong() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
isPlaying = false;
playBtn.setImageResource(R.drawable.ic_play_arrow_black_36dp);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
}
```
这是一个简单的音乐播放器示例,仅供参考。您可以根据自己的需求进行更改和扩展。
阅读全文