Android实验音乐播放器
时间: 2023-06-14 07:03:39 浏览: 155
Android 音乐播放器.
5星 · 资源好评率100%
好的,Android实验中实现一个音乐播放器可以分为以下几个步骤:
1. 添加权限
在AndroidManifest.xml文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
```
2. 构建UI
在activity_main.xml文件中添加以下UI控件:
```xml
<RelativeLayout
android:id="@+id/parent_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="@android:color/white"
android:textSize="24sp"
android:textStyle="bold" />
<LinearLayout
android:id="@+id/control_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary">
<ImageButton
android:id="@+id/previous_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@null"
android:padding="10dp"
android:src="@drawable/ic_skip_previous_white_48dp" />
<ImageButton
android:id="@+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@null"
android:padding="10dp"
android:src="@drawable/ic_play_arrow_white_48dp" />
<ImageButton
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@null"
android:padding="10dp"
android:src="@drawable/ic_skip_next_white_48dp" />
</LinearLayout>
</RelativeLayout>
```
3. 实现音乐播放
在MainActivity.java文件中实现音乐播放功能,包括播放、暂停、上一曲、下一曲等功能。
```java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private MediaPlayer mediaPlayer;
private ArrayList<String> musicList;
private int currentIndex = 0;
private boolean isPlaying = false;
private Handler handler = new Handler();
private TextView textView;
private ImageButton previousButton, playButton, nextButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
previousButton = findViewById(R.id.previous_button);
playButton = findViewById(R.id.play_button);
nextButton = findViewById(R.id.next_button);
previousButton.setOnClickListener(this);
playButton.setOnClickListener(this);
nextButton.setOnClickListener(this);
// 获取音乐列表
musicList = new ArrayList<>();
ContentResolver contentResolver = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
do {
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
musicList.add(path);
} while (cursor.moveToNext());
cursor.close();
}
// 初始化MediaPlayer
mediaPlayer = new MediaPlayer();
mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
playNext();
}
});
// 播放第一首歌
playMusic(currentIndex);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.previous_button:
playPrevious();
break;
case R.id.play_button:
if (isPlaying) {
pauseMusic();
} else {
startMusic();
}
break;
case R.id.next_button:
playNext();
break;
}
}
private void playMusic(int index) {
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(musicList.get(index));
mediaPlayer.prepare();
mediaPlayer.start();
isPlaying = true;
updatePlayButton();
updateTextView();
} catch (IOException e) {
e.printStackTrace();
}
}
private void pauseMusic() {
mediaPlayer.pause();
isPlaying = false;
updatePlayButton();
}
private void startMusic() {
mediaPlayer.start();
isPlaying = true;
updatePlayButton();
}
private void playPrevious() {
if (currentIndex > 0) {
currentIndex--;
playMusic(currentIndex);
} else {
Toast.makeText(this, "已经是第一首歌了", Toast.LENGTH_SHORT).show();
}
}
private void playNext() {
if (currentIndex < musicList.size() - 1) {
currentIndex++;
playMusic(currentIndex);
} else {
Toast.makeText(this, "已经是最后一首歌了", Toast.LENGTH_SHORT).show();
}
}
private void updatePlayButton() {
if (isPlaying) {
playButton.setImageResource(R.drawable.ic_pause_white_48dp);
} else {
playButton.setImageResource(R.drawable.ic_play_arrow_white_48dp);
}
}
private void updateTextView() {
handler.post(new Runnable() {
@Override
public void run() {
textView.setText(musicList.get(currentIndex));
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
mediaPlayer.stop();
mediaPlayer.release();
}
}
```
4. 运行程序
运行程序,可以看到一个简单的音乐播放器界面,可以通过上一曲、播放/暂停、下一曲按钮进行操作,同时也可以通过列表选择音乐进行播放。
阅读全文