欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實(shí)現(xiàn)簡單的音樂播放器

 更新時間:2022年05月19日 13:55:13   作者:一條比目魚  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡單的音樂播放器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android實(shí)現(xiàn)簡單音樂播放器的具體代碼,供大家參考,具體內(nèi)容如下

1.制作一個簡易的音樂播放器

使用軟件:Android studio + jdk1.8 + Gradle6.5(其他版本也可以)

2.activity_main.xml文件(主頁面的編寫)

先看一下示意圖再來排版(音樂板塊的模型看的是某云)

分3個部分來看,第一個是音樂的標(biāo)題,就是Sky_High、No Copyright Sounds和那個圓形的可以轉(zhuǎn)動的圖標(biāo)。

<ImageView
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:scaleType="fitXY"
? ? android:src="@mipmap/music_background" /> ?//這里是配置背景的,就是后面的青藍(lán)色板塊
?
<TextView
? ? android:id="@+id/tv_song_name"
? ? android:layout_width="match_parent"
? ? android:layout_height="wrap_content"
? ? android:layout_marginTop="32dp"
? ? android:gravity="center_horizontal"
? ? android:text="Sky_High"
? ? android:textAlignment="center"
? ? android:textSize="24sp" /> ?//這是第一行Sky_High的
?
<TextView
? ? android:id="@+id/tv_song_singer"
? ? android:layout_width="match_parent"
? ? android:layout_height="wrap_content"
? ? android:layout_below="@id/tv_song_name"
? ? android:gravity="center_horizontal"
? ? android:text="No Copyright Sounds"
? ? android:textAlignment="center"
? ? android:textSize="18sp" /> ?//這是第二行No Copyright Sounds
?
<ImageView
? ? android:id="@+id/iv_disk"
? ? android:layout_width="250dp"
? ? android:layout_height="250dp"
? ? android:layout_below="@id/tv_song_singer"
? ? android:layout_centerHorizontal="true"
? ? android:layout_marginTop="32dp"
? ? android:src="@mipmap/id3_artwork" /> ?//圓球圖片
?
<TextView
? ? android:id="@+id/tv_lyric_previous"
? ? android:layout_width="match_parent"
? ? android:layout_height="wrap_content"
? ? android:layout_below="@id/iv_disk"
? ? android:layout_marginTop="16dp"
? ? android:gravity="center_horizontal"
? ? android:text="---"
? ? android:textAlignment="center" /> ?//這是為了方便“此音樂為純音樂,請欣賞”這里的排版大小來加的,可以刪除掉
?
<TextView
? ? android:id="@+id/tv_lyric_current"
? ? android:layout_width="match_parent"
? ? android:layout_height="wrap_content"
? ? android:layout_below="@id/tv_lyric_previous"
? ? android:layout_marginTop="16dp"
? ? android:gravity="center_horizontal"
? ? android:text="此音樂為純音樂,請欣賞"
? ? android:textAlignment="center"
? ? android:textSize="20dp" /> ?//“此音樂為純音樂,請欣賞”這里是用來放歌詞的
?
<TextView
? ? android:id="@+id/tv_lyric_next"
? ? android:layout_width="match_parent"
? ? android:layout_height="wrap_content"
? ? android:layout_below="@id/tv_lyric_current"
? ? android:layout_marginTop="16dp"
? ? android:gravity="center_horizontal"
? ? android:text="---"
? ? android:textAlignment="center" /> ?//這是為了方便“此音樂為純音樂,請欣賞”這里的排版大小來加的,可以刪除掉

第二部分是用來顯示音樂播放進(jìn)度的SeekBar

<RelativeLayout
? ? android:id="@+id/rl_progress"
? ? android:layout_width="match_parent"
? ? android:layout_height="wrap_content"
? ? android:layout_below="@id/tv_lyric_next"
? ? android:layout_marginTop="32dp">
?
? ? <androidx.appcompat.widget.AppCompatSeekBar ?//這里一般看版本的,有點(diǎn)版本直接seekBar就行了,我的版本較低
? ? ? ? android:id="@+id/sb_progress"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content" />
?
? ? <TextView
? ? ? ? android:id="@+id/tv_progress_current"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_below="@id/sb_progress"
? ? ? ? android:layout_alignParentStart="true"
? ? ? ? android:layout_alignParentLeft="true"
? ? ? ? android:text="0:00" /> ?//這是兩個定點(diǎn),這個定點(diǎn)表示聲音起始點(diǎn)
?
? ? <TextView
? ? ? ? android:id="@+id/tv_progress_total"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_below="@id/sb_progress"
? ? ? ? android:layout_alignParentEnd="true"
? ? ? ? android:layout_alignParentRight="true"
? ? ? ? android:text="0:00" /> ?//這是兩個定點(diǎn),這個定點(diǎn)表示聲音終點(diǎn)
</RelativeLayout>

第三部分是音樂的暫停、開始、繼續(xù)、前一首和后一首的選擇。

這一部分可能就是排版比較煩,耐心點(diǎn)還是可以弄好的,調(diào)參可能費(fèi)點(diǎn)時間。

<RelativeLayout
? ? android:id="@+id/rl_control"
? ? android:layout_width="match_parent"
? ? android:layout_height="wrap_content"
? ? android:layout_alignParentBottom="true"
? ? android:layout_marginBottom="32dp">
?
? ? <ImageButton
? ? ? ? android:id="@+id/btn_prev"
? ? ? ? android:layout_width="64dp"
? ? ? ? android:layout_height="64dp"
? ? ? ? android:layout_centerHorizontal="true"
? ? ? ? android:layout_toStartOf="@id/btn_play_pause"
? ? ? ? android:layout_toLeftOf="@id/btn_play_pause"
? ? ? ? android:scaleType="fitCenter"
? ? ? ? android:src="@mipmap/id3_icon_prev_d" />
?
? ? <ImageButton
? ? ? ? android:id="@+id/btn_play_pause"
? ? ? ? android:layout_width="64dp"
? ? ? ? android:layout_height="64dp"
? ? ? ? android:layout_centerHorizontal="true"
? ? ? ? android:scaleType="fitCenter"
? ? ? ? android:src="@mipmap/id3_button_pause_n" />
?
? ? <ImageButton
? ? ? ? android:id="@+id/btn_next"
? ? ? ? android:layout_width="64dp"
? ? ? ? android:layout_height="64dp"
? ? ? ? android:layout_centerHorizontal="true"
? ? ? ? android:layout_toEndOf="@id/btn_play_pause"
? ? ? ? android:layout_toRightOf="@id/btn_play_pause"
? ? ? ? android:scaleType="fitCenter"
? ? ? ? android:src="@mipmap/id3_icon_next_d" />
?
</RelativeLayout>

下面一部分是MainActivity.java的編寫

這一部分關(guān)鍵在于球型圖的轉(zhuǎn)動、seekbar的運(yùn)行和音樂的暫停播放邏輯關(guān)系等等

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.interpolator.view.animation.LinearOutSlowInInterpolator;
?
import android.animation.ObjectAnimator;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
?
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
?
public class MainActivity extends AppCompatActivity {
? ? private TextView songName, singerName;
? ? private ImageView diskImage;
? ? private TextView lyricPrev, lyricCurrent, lyricNext;
? ? private SeekBar musicProgress;
? ? private TextView currentTime, totalTime;
? ? private ImageButton prevBtn, playPauseBtn, nextBtn;
? ? private ObjectAnimator animator;
?
? ? private MediaPlayer player;
? ? private int currentPlaying = 0; //用作ArrayList下表,當(dāng)前播放的歌曲、
? ? private ArrayList<Integer> playList = new ArrayList<>();
?
? ? private boolean isPausing, isPlaying; ?//音樂暫停狀態(tài),音樂第一次播放之后變?yōu)閠rue
?
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? init();
? ? ? ? preparePlayList();
? ? ? ? TimerTask timerTask = new TimerTask() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? if (isPlaying) {
? ? ? ? ? ? ? ? ? ? updateTimer();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? };
? ? ? ? new Timer().scheduleAtFixedRate(timerTask, 0, 500);
? ? }
?
? ? void init() {
? ? ? ? songName = findViewById(R.id.tv_song_name);
? ? ? ? singerName = findViewById(R.id.tv_song_singer);
? ? ? ? diskImage = findViewById(R.id.iv_disk);
? ? ? ? lyricPrev = findViewById(R.id.tv_lyric_previous);
? ? ? ? lyricCurrent = findViewById(R.id.tv_lyric_current);
? ? ? ? lyricNext = findViewById(R.id.tv_lyric_next);
? ? ? ? musicProgress = findViewById(R.id.sb_progress);
? ? ? ? currentTime = findViewById(R.id.tv_progress_current);
? ? ? ? totalTime = findViewById(R.id.tv_progress_total);
? ? ? ? prevBtn = findViewById(R.id.btn_prev);
? ? ? ? playPauseBtn = findViewById(R.id.btn_play_pause);
? ? ? ? nextBtn = findViewById(R.id.btn_next);
?
? ? ? ? View.OnClickListener onClick = new OnClickControl();
? ? ? ? prevBtn.setOnClickListener(onClick);
? ? ? ? playPauseBtn.setOnClickListener(onClick);
? ? ? ? nextBtn.setOnClickListener(onClick);
?
? ? ? ? OnSeekBarChangeControl onSbChange = new OnSeekBarChangeControl();
? ? ? ? musicProgress.setOnSeekBarChangeListener(onSbChange);
?
? ? ? ? animator = ObjectAnimator.ofFloat(diskImage, "rotation", 0, 360.0F); ?//初始化狀態(tài)
? ? ? ? animator.setDuration(2000); ?//狀態(tài)時長,10秒
? ? ? ? animator.setInterpolator(new LinearInterpolator()); ?//時間函數(shù),有很多類型
? ? ? ? animator.setRepeatCount(-1); // 一直旋轉(zhuǎn)
?
? ? }
?
? ? private void preparePlayList() {
? ? ? ? Field[] field = R.raw.class.getFields();
? ? ? ? for (int count = 0; count < field.length; count++) {
? ? ? ? ? ? Log.i("Raw Asset", field[count].getName());
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? int resId = field[count].getInt(field[count]);
? ? ? ? ? ? ? ? playList.add(resId);
? ? ? ? ? ? } catch (IllegalAccessException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? }
?
? ? private void prepareMedia() {
? ? ? ? if (isPlaying) {
? ? ? ? ? ? player.stop();
? ? ? ? ? ? player.reset();
? ? ? ? }
? ? ? ? player = MediaPlayer.create(getApplicationContext(), playList.get(currentPlaying));
? ? ? ? int musicDuration = player.getDuration();
? ? ? ? musicProgress.setMax(musicDuration);
? ? ? ? int sec = musicDuration / 1000;
? ? ? ? int min = sec / 60;
? ? ? ? sec -= min * 60;
? ? ? ? String musicTime = String.format("%02d:%02d", min, sec);
? ? ? ? totalTime.setText(musicTime);
? ? ? ? player.start();
? ? }
?
? ? private void updateTimer() {
? ? ? ? runOnUiThread(() -> {
? ? ? ? ? ? int currentMs = player.getCurrentPosition();
? ? ? ? ? ? int sec = currentMs / 1000;
? ? ? ? ? ? int min = sec / 60;
? ? ? ? ? ? sec -= min * 60;
? ? ? ? ? ? String time = String.format("%02d:%02d", min, sec);
? ? ? ? ? ? musicProgress.setProgress(currentMs);
? ? ? ? ? ? currentTime.setText(time);
? ? ? ? });
? ? }
?
//這一部分很重要,對于初學(xué)者來說,一定要搞清楚這三個按鈕之間的邏輯關(guān)系,因?yàn)橐袛嗳绻魳窌和A?,是?zhí)行哪個線程,跳到哪個圖標(biāo)上去。怎么樣繼續(xù)運(yùn)行接下來的音樂,我這里加了日志文件,可以判斷一下哪一步出錯了。
? ? private class OnClickControl implements View.OnClickListener { ?
? ? ? ? @RequiresApi(api = Build.VERSION_CODES.KITKAT)
? ? ? ? @Override
? ? ? ? public void onClick(View v) {
? ? ? ? ? ? switch (v.getId()) {
? ? ? ? ? ? ? ? case R.id.btn_prev:
? ? ? ? ? ? ? ? ? ? //重播
? ? ? ? ? ? ? ? ? ? Log.i("INFO", "onClick:重播按鈕被點(diǎn)擊!");
? ? ? ? ? ? ? ? ? ? playPauseBtn.setImageResource(R.mipmap.id3_button_pause_n); ?//切換成暫停鍵
? ? ? ? ? ? ? ? ? ? animator.start();
? ? ? ? ? ? ? ? ? ? if (!player.isPlaying()) {
? ? ? ? ? ? ? ? ? ? ? ? currentPlaying = --currentPlaying % playList.size();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? prepareMedia();
? ? ? ? ? ? ? ? ? ? isPausing = false;
? ? ? ? ? ? ? ? ? ? isPlaying = true;
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case R.id.btn_play_pause:
? ? ? ? ? ? ? ? ? ? //開始暫停
? ? ? ? ? ? ? ? ? ? Log.i("INFO", "onClick:開始暫停按鈕被點(diǎn)擊!");
? ? ? ? ? ? ? ? ? ? if (!isPausing && !isPlaying) { //暫停狀態(tài),且從未被播放
? ? ? ? ? ? ? ? ? ? ? ? //開始播放
? ? ? ? ? ? ? ? ? ? ? ? playPauseBtn.setImageResource(R.mipmap.id3_button_pause_n); ?//切換成暫停鍵
? ? ? ? ? ? ? ? ? ? ? ? animator.start();
? ? ? ? ? ? ? ? ? ? ? ? prepareMedia();
? ? ? ? ? ? ? ? ? ? ? ? isPlaying = true;
? ? ? ? ? ? ? ? ? ? } else if (!isPausing && isPlaying) { //暫停狀態(tài),且被播放過一次
? ? ? ? ? ? ? ? ? ? ? ? //繼續(xù)播放
? ? ? ? ? ? ? ? ? ? ? ? playPauseBtn.setImageResource(R.mipmap.id3_button_pause_n);//切換成暫停鍵
? ? ? ? ? ? ? ? ? ? ? ? animator.resume();
? ? ? ? ? ? ? ? ? ? ? ? player.start();
? ? ? ? ? ? ? ? ? ? } else { //播放狀態(tài)
? ? ? ? ? ? ? ? ? ? ? ? //暫停播放
? ? ? ? ? ? ? ? ? ? ? ? playPauseBtn.setImageResource(R.mipmap.id3_button_play_p);//切換成播放鍵
? ? ? ? ? ? ? ? ? ? ? ? animator.pause();
? ? ? ? ? ? ? ? ? ? ? ? player.pause();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? isPausing = !isPausing; ?//切換歌曲
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case R.id.btn_next:
? ? ? ? ? ? ? ? ? ? Log.i("INFO", "onClick:重播按鈕被點(diǎn)擊!");
? ? ? ? ? ? ? ? ? ? //切歌
? ? ? ? ? ? ? ? ? ? playPauseBtn.setImageResource(R.mipmap.id3_button_pause_n); // 切換成暫停鍵
? ? ? ? ? ? ? ? ? ? currentPlaying = ++currentPlaying % playList.size();
? ? ? ? ? ? ? ? ? ? prepareMedia();
? ? ? ? ? ? ? ? ? ? animator.start();
? ? ? ? ? ? ? ? ? ? isPausing = false;
? ? ? ? ? ? ? ? ? ? isPlaying = true;
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? Log.i("INFO", "onClick:按鈕被點(diǎn)擊了,但是有BUG");
? ? ? ? ? ? ? ? ? ? //有BUG了
? ? ? ? ? ? }
? ? ? ? }
? ? }
?
? ? private class OnSeekBarChangeControl implements SeekBar.OnSeekBarChangeListener {
? ? ? ? @Override
? ? ? ? public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
? ? ? ? ? ? if (fromUser) {
? ? ? ? ? ? ? ? player.seekTo(progress);
? ? ? ? ? ? }
? ? ? ? }
?
? ? ? ? @RequiresApi(api = Build.VERSION_CODES.KITKAT)
? ? ? ? @Override
? ? ? ? public void onStartTrackingTouch(SeekBar seekBar) {
? ? ? ? ? ? player.pause();
? ? ? ? ? ? animator.pause();
? ? ? ? }
?
? ? ? ? @RequiresApi(api = Build.VERSION_CODES.KITKAT)
? ? ? ? @Override
? ? ? ? public void onStopTrackingTouch(SeekBar seekBar) {
? ? ? ? ? ? player.start();
? ? ? ? ? ? if (seekBar.getProgress() < 10) {
? ? ? ? ? ? ? ? animator.start();
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? animator.resume();
? ? ? ? ? ? }
? ? ? ? }
? ? }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論