Android 简单音乐播放器(破烂版,后续更新)

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();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值