Android studio1、在主界面中,通过点击按钮跳转到指定歌曲的播放界面 2、在歌曲播放界面中,需要播放歌曲,并且具有对歌曲评价的功能(例如,评价框、编辑框、单选框、多选框等) 3、至少实现三个不同的歌曲播放页面。代码
时间: 2023-06-16 16:04:20 浏览: 113
Android Studio如何实现音乐播放器(简单易上手)
以下是实现上述功能的 Android Studio 代码示例:
1. MainActivity.java
```java
public class MainActivity extends AppCompatActivity {
Button song1Btn, song2Btn, song3Btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
song1Btn = findViewById(R.id.song1_btn);
song2Btn = findViewById(R.id.song2_btn);
song3Btn = findViewById(R.id.song3_btn);
song1Btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Song1Activity.class);
startActivity(intent);
}
});
song2Btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Song2Activity.class);
startActivity(intent);
}
});
song3Btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Song3Activity.class);
startActivity(intent);
}
});
}
}
```
2. activity_main.xml
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/song1_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Song 1" />
<Button
android:id="@+id/song2_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Song 2" />
<Button
android:id="@+id/song3_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Song 3" />
</LinearLayout>
```
3. Song1Activity.java
```java
public class Song1Activity extends AppCompatActivity {
MediaPlayer mediaPlayer;
RatingBar ratingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_song1);
mediaPlayer = MediaPlayer.create(this, R.raw.song1);
ratingBar = findViewById(R.id.rating_bar);
Button playBtn = findViewById(R.id.play_btn);
Button pauseBtn = findViewById(R.id.pause_btn);
playBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.start();
}
});
pauseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.pause();
}
});
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
// Save rating to a database or send to a server
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
mediaPlayer.release();
}
}
```
4. activity_song1.xml
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/play_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play" />
<Button
android:id="@+id/pause_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause" />
<RatingBar
android:id="@+id/rating_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1" />
</LinearLayout>
```
5. Song2Activity.java 和 activity_song2.xml,Song3Activity.java 和 activity_song3.xml 的代码与上述类似,只需修改资源文件和对应的播放歌曲即可。
请注意,以上代码仅提供了一个简单的示例,并且并没有完全实现歌曲评价的功能。您需要根据实际需求进行修改和完善。
阅读全文