Android音乐播放器代码编写
时间: 2023-12-19 10:31:49 浏览: 99
以下是Android音乐播放器代码编写的一些基本步骤和注意事项:
1. 首先需要在AndroidManifest.xml文件中添加权限:
```xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
2. 在布局文件中添加音乐播放器的UI界面,例如:
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_music_player"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<ImageView
android:id="@+id/iv_music_cover"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/tv_music_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/black_alpha_50"
android:gravity="center_vertical"
android:padding="10dp"
android:singleLine="true"
android:textColor="@color/white"
android:textSize="16sp" />
<LinearLayout
android:id="@+id/layout_music_control"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/tv_music_title"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:id="@+id/iv_music_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_play_arrow_black_24dp" />
<ImageView
android:id="@+id/iv_music_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_pause_black_24dp"
android:visibility="gone" />
<SeekBar
android:id="@+id/sb_music_progress"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="100"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:thumb="@drawable/seekbar_thumb"
android:thumbOffset="0dp" />
<TextView
android:id="@+id/tv_music_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
```
3. 在Activity中初始化音乐播放器,并设置相关监听器,例如:
```java
public class MusicPlayerActivity extends AppCompatActivity implements View.OnClickListener {
private MediaPlayer mMediaPlayer;
private ImageView mIvPlay;
private ImageView mIvPause;
private SeekBar mSbProgress;
private TextView mTvDuration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music_player);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mSbProgress.setMax(mMediaPlayer.getDuration());
mTvDuration.setText(formatTime(mMediaPlayer.getDuration()));
mMediaPlayer.start();
mIvPlay.setVisibility(View.GONE);
mIvPause.setVisibility(View.VISIBLE);
}
});
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mIvPlay.setVisibility(View.VISIBLE);
mIvPause.setVisibility(View.GONE);
}
});
mIvPlay = findViewById(R.id.iv_music_play);
mIvPause = findViewById(R.id.iv_music_pause);
mSbProgress = findViewById(R.id.sb_music_progress);
mTvDuration = findViewById(R.id.tv_music_duration);
mIvPlay.setOnClickListener(this);
mIvPause.setOnClickListener(this);
mSbProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
mMediaPlayer.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_music_play:
try {
mMediaPlayer.setDataSource("音乐文件路径");
mMediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.iv_music_pause:
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
mIvPlay.setVisibility(View.VISIBLE);
mIvPause.setVisibility(View.GONE);
}
break;
}
}
private String formatTime(int duration) {
int minute = duration / 1000 / 60;
int second = duration / 1000 % 60;
return String.format(Locale.getDefault(), "%02d:%02d", minute, second);
}
}
```
4. 在AndroidManifest.xml文件中注册Activity:
```xml
<activity android:name=".MusicPlayerActivity" />
```
阅读全文