iT邦幫忙

2021 iThome 鐵人賽

DAY 8
0

activity_main:
在LinearLayout中加入播放/暫停、停止、循環的按鈕

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnPlayPause"
        android:src="@android:drawable/ic_media_play"/>
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnStop"
        android:src="@android:drawable/ic_menu_close_clear_cancel"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnloop"
        android:text="Repeat"/>
</LinearLayout>

MainActivity:
實作MediaPlayer,並搭配生命週期完成三個按鈕的功能

public class MainActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener {

    private ImageButton btnPlayPause,btnStop;
    private MediaPlayer mediaPlayer = null;
    private boolean isInit = true;
    private Button repeat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
    }

    private void findViews(){
        btnPlayPause = findViewById(R.id.btnPlayPause);
        btnStop = findViewById(R.id.btnStop);
        repeat = findViewById(R.id.btnloop);

        btnPlayPause.setOnClickListener(PlayPauseListener);
        btnStop.setOnClickListener(StopListener);

        repeat.setOnClickListener(v->{
            mediaPlayer.setLooping(true); //Repeat,False取消
        });
    }

    protected void onResume(){
        super.onResume();
        mediaPlayer = new MediaPlayer();
        Uri uri = Uri.parse("android.resource://"+ getPackageName() + "/" + R.raw.chain);
        try{
            mediaPlayer.setDataSource(this,uri);
        }catch(Exception e){
            Toast.makeText(getApplicationContext(),"檔案讀取錯誤",Toast.LENGTH_SHORT).show();
        }
        mediaPlayer.setOnPreparedListener(this);
        mediaPlayer.setOnErrorListener(this);
        mediaPlayer.setOnCompletionListener(this);
    }

    protected void onStop(){
        super.onStop();
        mediaPlayer.release();
        mediaPlayer = null;
    }

    private View.OnClickListener PlayPauseListener = v -> {
        if (mediaPlayer.isPlaying()) {
            btnPlayPause.setImageResource(android.R.drawable.ic_media_play);
            mediaPlayer.pause();
        } else {
            btnPlayPause.setImageResource(android.R.drawable.ic_media_pause);
            if (isInit) {
                mediaPlayer.prepareAsync();
                isInit = false;
            } else {
                mediaPlayer.start();
            }
        }
    };

    private View.OnClickListener StopListener = v->{
        mediaPlayer.stop();
        isInit = true;
        btnPlayPause.setImageResource(android.R.drawable.ic_media_play);
    };

    @Override
    public void onCompletion(MediaPlayer mp) {
        btnPlayPause.setImageResource(android.R.drawable.ic_media_play);
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        mp.release();
        mp = null;
        Toast.makeText(getApplicationContext(),"Error!!",Toast.LENGTH_SHORT).show();
        return true;
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.seekTo(0);
        mp.start();
    }
}

謝謝大家願意花時間閱讀,小弟弟我在此鞠躬/images/emoticon/emoticon41.gif


上一篇
[Lesson7] Activity生命週期
下一篇
[Lesson9] Firebase
系列文
刮掉Web Development的我,與撿到的Android Development30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言