關(guān)于 Android Studio 做一個(gè) MP3 播放器:
-
Android Studio 本身提供了多個(gè)音頻播放器組件,可以通過這些組件來實(shí)現(xiàn)音頻播放功能,其中包括 MediaPlayer、SoundPool 和 ExoPlayer 等。官方推薦我們使用ExoPlayer因?yàn)楣δ芨鼜?qiáng)大,但這里用MediaPlayer因?yàn)榫帉懜?jiǎn)單。
-
在使用 MediaPlayer 實(shí)現(xiàn) MP3 播放器時(shí),需要注意以下幾點(diǎn):
-
在使用 MediaPlayer 之前,需要申請(qǐng) “android.permission.READ_EXTERNAL_STORAGE” 權(quán)限,以讀取本地儲(chǔ)存的 MP3 文件。
-
在代碼中,需要實(shí)例化一個(gè) MediaPlayer 對(duì)象,并設(shè)置其數(shù)據(jù)源、音頻焦點(diǎn)、循環(huán)播放等屬性。
-
然后,通過調(diào)用 MediaPlayer 對(duì)象的 start() 方法,就可以開啟播放功能。
-
-
這里提供一些關(guān)于 MediaPlayer 的資源鏈接:
- 官方文檔:https://developer.android.com/reference/android/media/MediaPlayer
- 實(shí)現(xiàn) MP3 播放器的教程:https://www.androidhive.info/2012/03/android-building-audio-player-tutorial/
- GitHub 上的相關(guān)代碼:https://github.com/DeweyReed/android_media_player
開發(fā)一個(gè)基本的 MP3 播放器需要以下步驟:
1.創(chuàng)建一個(gè)新的 Android Studio 項(xiàng)目
2.在 layout 文件夾中創(chuàng)建 UI 布局,包括播放/暫停按鈕、進(jìn)度條和音樂信息
3.在 drawable 文件夾中添加播放/暫停圖標(biāo)
4.導(dǎo)入 jaudiotagger 庫(kù)以讀取 MP3 文件的元數(shù)據(jù)
5.使用 MediaPlayer 類實(shí)現(xiàn) MP3 文件的播放與控制
下面是一個(gè)簡(jiǎn)單的代碼示例來實(shí)現(xiàn)這些步驟:
1.創(chuàng)建一個(gè)新的 Android Studio 項(xiàng)目
在 Android Studio 中,選擇 File -> New -> New Project。在彈出窗口中,輸入應(yīng)用名稱、包名和項(xiàng)目位置等信息創(chuàng)建新項(xiàng)目。
2.創(chuàng)建 UI 布局
在 res/layout 文件夾中創(chuàng)建一個(gè)名為 activity_main.xml 的布局文件。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/song_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Song Title"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/song_artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Song Artist"
android:layout_below="@id/song_title"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<ImageView
android:id="@+id/song_album_art"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/song_artist"
android:src="@drawable/default_album_art"
android:scaleType="fitCenter"/>
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/song_album_art"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/seek_bar"
android:orientation="horizontal">
<ImageButton
android:id="@+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/play_button"/>
<ImageButton
android:id="@+id/pause_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pause_button"
android:visibility="gone"/>
</LinearLayout>
</RelativeLayout>
該布局中包括音樂標(biāo)題、藝術(shù)家、專輯封面、進(jìn)度條和播放/暫停按鈕。
3.導(dǎo)入播放/暫停圖標(biāo)
在 res/drawable 文件夾中添加兩個(gè)名為 play_button.png 和 pause_button.png 的圖標(biāo)。您可以在網(wǎng)上搜索并下載這些圖標(biāo),然后將它們拖放到 Android Studio 項(xiàng)目中。
4.導(dǎo)入 jaudiotagger 庫(kù)
在項(xiàng)目的 build.gradle 文件中添加以下依賴項(xiàng):
dependencies {
implementation 'org.jaudiotagger:jaudiotagger:2.0.4'
}
這個(gè)庫(kù)可以用于讀取 MP3 文件的元數(shù)據(jù),例如標(biāo)題、藝術(shù)家和專輯信息。
5.使用 MediaPlayer 類實(shí)現(xiàn) MP3 文件的播放和控制
在 MainActivity.java 文件中添加以下代碼:
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private ImageButton playButton, pauseButton;
private boolean isPlaying;
private TextView songTitle, songArtist;
private ImageView albumArt;
private SeekBar seekBar;
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
songTitle = findViewById(R.id.song_title);
songArtist = findViewById(R.id.song_artist);
albumArt = findViewById(R.id.song_album_art);
seekBar = findViewById(R.id.seek_bar);
playButton = findViewById(R.id.play_button);
pauseButton = findViewById(R.id.pause_button);
// Load the audio file from the asset folder into the MediaPlayer
AssetFileDescriptor fileDescriptor = null;
try {
fileDescriptor = getAssets().openFd("sample_music.mp3");
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength());
} catch (IOException e) {
e.printStackTrace();
}
// Initialize the MediaPlayer and UI elements
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
int duration = mp.getDuration();
seekBar.setMax(duration);
songTitle.setText(mp.getMetadata("title"));
songArtist.setText(mp.getMetadata("artist"));
albumArt.setImageBitmap(mp.getMetadata("album art"));
playButton.setVisibility(View.VISIBLE);
pauseButton.setVisibility(View.GONE);
isPlaying = false;
}
});
// Update the seek bar progress as the audio is playing
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
playButton.setVisibility(View.VISIBLE);
pauseButton.setVisibility(View.GONE);
isPlaying = false;
}
});
// Start the MediaPlayer when the play button is clicked
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer != null && !isPlaying) {
mediaPlayer.start();
mHandler.postDelayed(updateSeekBar, 100);
playButton.setVisibility(View.GONE);
pauseButton.setVisibility(View.VISIBLE);
isPlaying = true;
}
}
});
// Pause the MediaPlayer when the pause button is clicked
pauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer != null && isPlaying) {
mediaPlayer.pause();
mHandler.removeCallbacks(updateSeekBar);
pauseButton.setVisibility(View.GONE);
playButton.setVisibility(View.VISIBLE);
isPlaying = false;
}
}
});
}
// Update the seek bar progress every second
private Runnable updateSeekBar = new Runnable() {
@Override
public void run() {
int currentPosition = mediaPlayer.getCurrentPosition();
seekBar.setProgress(currentPosition);
mHandler.postDelayed(this, 1000);
}
};
// Release the MediaPlayer when the activity is destroyed
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
}
該代碼中加載了一個(gè)名為 sample_music.mp3 的 MP3 文件并將其作為 MediaPlayer 的數(shù)據(jù)源。在 MediaPlayer 準(zhǔn)備好播放文件時(shí),將更新 UI 元素,例如標(biāo)題、藝術(shù)家、專輯封面和時(shí)長(zhǎng)。另外,為了使播放和暫停按鈕正常工作,我們需要檢查 MediaPlayer 是否正在播放。文章來源:http://www.zghlxwxcb.cn/news/detail-761322.html
MP3 播放器的基本功能已經(jīng)實(shí)現(xiàn)了。當(dāng)您點(diǎn)擊播放按鈕時(shí),音樂會(huì)開始播放,并且進(jìn)度條會(huì)隨著音樂進(jìn)度而更新。同樣,當(dāng)您點(diǎn)擊暫停按鈕時(shí),音樂會(huì)暫停。您可以根據(jù)自己的需要修改 UI 和播放器的一些設(shè)置。文章來源地址http://www.zghlxwxcb.cn/news/detail-761322.html
到了這里,關(guān)于Android Studio 做一個(gè) MP3 播放器的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!