android studio期末大作业制作一个音乐播放器包括代码
时间: 2023-10-21 08:03:42 浏览: 145
利用Android编写一个音乐播放器App
5星 · 资源好评率100%
很高兴能帮助你!下面是一个简单的Android音乐播放器的示例代码:
首先,在你的布局文件中添加以下内容:
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_above="@+id/layout"
android:dividerHeight="1dp"
android:divider="#cccccc" />
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:background="#ffffff"
android:padding="5dp">
<ImageView
android:id="@+id/prev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/prev"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp" />
<ImageView
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/play"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp" />
<ImageView
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/next"
android:layout_gravity="center_vertical" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="16sp"
android:textColor="#000000"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
```
然后,在你的Activity中添加以下代码:
```
public class MainActivity extends AppCompatActivity {
private ListView listView;
private ImageView prev, play, next;
private TextView title;
private MediaPlayer mediaPlayer;
private boolean isPlaying = false;
private int currentSongIndex = 0;
private String[] songs = {
"song1.mp3",
"song2.mp3",
"song3.mp3"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
prev = findViewById(R.id.prev);
play = findViewById(R.id.play);
next = findViewById(R.id.next);
title = findViewById(R.id.title);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
isPlaying = false;
play.setImageResource(R.drawable.play);
}
});
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, songs);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(MainActivity.this, Uri.parse("android.resource://" + getPackageName() + "/raw/" + songs[i]));
mediaPlayer.prepare();
mediaPlayer.start();
isPlaying = true;
currentSongIndex = i;
play.setImageResource(R.drawable.pause);
title.setText(songs[i]);
} catch (IOException e) {
e.printStackTrace();
}
}
});
prev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (currentSongIndex > 0) {
currentSongIndex--;
listView.setSelection(currentSongIndex);
listView.performItemClick(listView.getAdapter().getView(currentSongIndex, null, null), currentSongIndex, listView.getAdapter().getItemId(currentSongIndex));
}
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (currentSongIndex < songs.length - 1) {
currentSongIndex++;
listView.setSelection(currentSongIndex);
listView.performItemClick(listView.getAdapter().getView(currentSongIndex, null, null), currentSongIndex, listView.getAdapter().getItemId(currentSongIndex));
}
}
});
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isPlaying) {
mediaPlayer.pause();
isPlaying = false;
play.setImageResource(R.drawable.play);
} else {
mediaPlayer.start();
isPlaying = true;
play.setImageResource(R.drawable.pause);
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
mediaPlayer.release();
}
}
```
这段代码创建了一个简单的音乐播放器,其中包括一个ListView来列出所有可用的歌曲,ImageViews来控制播放,暂停,上一首和下一首,以及一个TextView来显示当前播放的歌曲标题。它还使用了Android的MediaPlayer类来播放歌曲。
请注意,此示例仅包含基本的播放功能,如果您想要更多高级的功能,您需要自己添加它们。
阅读全文