Android(安卓)简单音乐播放器(破烂版,后续更新)
16lz
2021-12-04
Android 简单音乐播放器(破烂版,后续更新)
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="文件名称" /> <EditText android:id="@+id/edit" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_play" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="play" /> <Button android:id="@+id/btn_pause" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="pause" /> <Button android:id="@+id/btn_stop" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="stop" /> <Button android:id="@+id/btn_reset" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="reset" />LinearLayout>
MainActivity.java
public class MainActivity extends Activity { private EditText edit; private Button btn_play; private Button btn_pause; private Button btn_stop; private Button btn_reset; private MediaPlayer mediaPlayer; private String filename; private int position; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //init edit = (EditText) findViewById(R.id.edit); btn_play = (Button) findViewById(R.id.btn_play); btn_pause = (Button) findViewById(R.id.btn_pause); btn_stop = (Button) findViewById(R.id.btn_stop); btn_reset = (Button) findViewById(R.id.btn_reset); BtnListener btnListener = new BtnListener(); mediaPlayer = new MediaPlayer(); btn_play.setOnClickListener(btnListener); btn_pause.setOnClickListener(btnListener); btn_stop.setOnClickListener(btnListener); btn_reset.setOnClickListener(btnListener); } @Override protected void onPause() { super.onPause(); if(mediaPlayer.isPlaying()) { position = mediaPlayer.getCurrentPosition(); mediaPlayer.stop(); } } @Override protected void onResume() { super.onResume(); if(position > 0 && filename != null) { try { play(); mediaPlayer.seekTo(position); position = 0; } catch (Exception e) { Log.i("MediaPlayer", e.toString()); } } } @Override protected void onDestroy() { super.onDestroy(); mediaPlayer.release(); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); this.filename = savedInstanceState.getString("filename"); this.position = savedInstanceState.getInt("position"); Log.i("MediaPlayer", "onRestoreInstanceState"); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("filename", filename); outState.putInt("position", position); Log.i("MediaPlayer", "onSaveInstanceState"); } private final class BtnListener implements OnClickListener { @Override public void onClick(View v) { filename = edit.getText().toString(); Button btn = (Button) v; try { switch (btn.getId()) { case R.id.btn_play: play(); break; case R.id.btn_pause: if(mediaPlayer.isPlaying()) { mediaPlayer.pause(); btn.setText("continue"); } else { mediaPlayer.start(); btn.setText("pause"); } break; case R.id.btn_stop: if (mediaPlayer.isPlaying()) { mediaPlayer.stop(); } break; case R.id.btn_reset: if(mediaPlayer.isPlaying()) { mediaPlayer.seekTo(0); } else { play(); } break; default: break; } } catch (Exception e) { Log.i("MediaPlayer", e.toString()); } } } private void play() throws IOException { File audioFile = new File( Environment.getExternalStorageDirectory(), filename); mediaPlayer.reset(); mediaPlayer.setDataSource(audioFile.getAbsolutePath()); mediaPlayer.prepare(); mediaPlayer.start(); }}
更多相关文章
- 更新android studio gradle 不成功解决方法
- android 自动更新apk版本
- Android(安卓)Studio 更新问题
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerT
- ubuntu下无法更新android sdk
- Android资源汇总---持续整理更新中!!!
- android 版本更新
- pixel升级Android(安卓)Q beta
- android在线播放音乐