android实现一个简单的播放器,要求功能有: 1. 播放、暂停,停止,退出功能; 2. 后台播放功能; 3. 进度条显示播放进度、拖动进度条改变进度功能; 4. 播放时图片旋转,显示当前播放时间功能。
时间: 2023-11-27 09:54:10 浏览: 154
Android_MP3播放器(完善版_添加了桌面widget_后台播放时桌面也可以控制播放、同步歌词等).
3星 · 编辑精心推荐
实现一个简单的Android播放器,可以按照以下步骤进行:
1. 创建一个新的Android项目,并在布局文件中添加一个MediaPlayer和一个SeekBar。
2. 在Activity中,初始化MediaPlayer并设置其数据源,设置播放器的监听器以便可以监听播放结束等事件。同时设置SeekBar的监听器以便可以监听进度条变化。
3. 在播放时,使用MediaPlayer的start()方法来启动播放器,并更新SeekBar的进度,同时显示当前播放时间。
4. 如果需要支持后台播放,可以通过将播放器绑定到service并启动service来实现。这样即使用户退出应用程序,播放器仍然可以继续播放。
5. 添加停止播放和退出功能,可以在Activity中添加按钮,并在按钮点击事件中调用相应的方法来实现这些功能。
6. 如果需要在播放时显示旋转图像,可以使用ImageView并在播放器监听器中更新其旋转角度。
下面是示例代码:
MainActivity.java
```java
public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
private MediaPlayer mediaPlayer;
private SeekBar seekBar;
private ImageView imageView;
private boolean isPlaying = false;
private boolean isStop = true;
private int currentPosition = 0;
private Timer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(this, R.raw.music);
seekBar = findViewById(R.id.seekBar);
imageView = findViewById(R.id.imageView);
seekBar.setOnSeekBarChangeListener(this);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlaying();
}
});
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (mediaPlayer != null && isPlaying && !isStop) {
currentPosition = mediaPlayer.getCurrentPosition();
runOnUiThread(new Runnable() {
@Override
public void run() {
seekBar.setProgress(currentPosition);
updateImageView();
}
});
}
}
}, 0, 100);
}
private void updateImageView() {
imageView.setRotation(imageView.getRotation() + 5);
}
private void startPlaying() {
mediaPlayer.start();
isPlaying = true;
isStop = false;
seekBar.setMax(mediaPlayer.getDuration());
}
private void pausePlaying() {
mediaPlayer.pause();
isPlaying = false;
}
private void stopPlaying() {
mediaPlayer.stop();
mediaPlayer.reset();
isPlaying = false;
isStop = true;
seekBar.setProgress(0);
updateImageView();
}
private void releasePlayer() {
mediaPlayer.release();
mediaPlayer = null;
}
private void bindService() {
Intent intent = new Intent(this, BackgroundMusicService.class);
startService(intent);
bindService(intent, serviceConnection, BIND_AUTO_CREATE);
}
private void unbindService() {
if (backgroundMusicService != null) {
unbindService(serviceConnection);
backgroundMusicService = null;
}
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
mediaPlayer.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
pausePlaying();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
startPlaying();
}
public void onClickPlay(View view) {
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(this, R.raw.music);
}
if (!isPlaying) {
startPlaying();
}
}
public void onClickPause(View view) {
if (isPlaying) {
pausePlaying();
}
}
public void onClickStop(View view) {
if (!isStop) {
stopPlaying();
}
}
public void onClickExit(View view) {
stopPlaying();
releasePlayer();
unbindService();
finish();
}
private BackgroundMusicService backgroundMusicService;
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
BackgroundMusicService.LocalBinder binder = (BackgroundMusicService.LocalBinder) service;
backgroundMusicService = binder.getService();
backgroundMusicService.playMusic();
}
@Override
public void onServiceDisconnected(ComponentName name) {
backgroundMusicService = null;
}
};
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
if (timer != null) {
timer.cancel();
timer = null;
}
}
}
```
activity_main.xml
```xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_alignParentTop="true"/>
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_foreground"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_below="@id/seekBar"
android:gravity="center_horizontal">
<Button
android:id="@+id/button_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放"
android:onClick="onClickPlay"/>
<Button
android:id="@+id/button_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停"
android:onClick="onClickPause"/>
<Button
android:id="@+id/button_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止"
android:onClick="onClickStop"/>
<Button
android:id="@+id/button_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="退出"
android:onClick="onClickExit"/>
</LinearLayout>
</RelativeLayout>
```
BackgroundMusicService.java
```java
public class BackgroundMusicService extends Service implements MediaPlayer.OnCompletionListener {
private MediaPlayer mediaPlayer;
public BackgroundMusicService() {
}
@Override
public void onCreate() {
super.onCreate();
mediaPlayer = MediaPlayer.create(this, R.raw.music);
mediaPlayer.setOnCompletionListener(this);
}
public void playMusic() {
if (mediaPlayer != null && !mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
@Override
public IBinder onBind(Intent intent) {
return new LocalBinder();
}
@Override
public void onCompletion(MediaPlayer mp) {
stopSelf();
}
public class LocalBinder extends Binder {
public BackgroundMusicService getService() {
return BackgroundMusicService.this;
}
}
}
```
阅读全文