国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Android Studio 做一個(gè) MP3 播放器

這篇具有很好參考價(jià)值的文章主要介紹了Android Studio 做一個(gè) MP3 播放器。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

關(guān)于 Android Studio 做一個(gè) MP3 播放器:

  1. Android Studio 本身提供了多個(gè)音頻播放器組件,可以通過這些組件來實(shí)現(xiàn)音頻播放功能,其中包括 MediaPlayer、SoundPool 和 ExoPlayer 等。官方推薦我們使用ExoPlayer因?yàn)楣δ芨鼜?qiáng)大,但這里用MediaPlayer因?yàn)榫帉懜?jiǎn)單。

  2. 在使用 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() 方法,就可以開啟播放功能。

  3. 這里提供一些關(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 是否正在播放。

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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Android Studio——簡(jiǎn)單視頻播放器

    Android Studio——簡(jiǎn)單視頻播放器

    ? ? ? ? 三個(gè)界面:主界面,視頻列表界面,視頻播放界面,如下圖所示: ? ? ? ? (1)主界面設(shè)計(jì) ???????????很簡(jiǎn)單,就一個(gè)背景和一個(gè)控件,然后為控件設(shè)置點(diǎn)擊事件,然后跳轉(zhuǎn)到視頻列表界面 ? ?activity_main.xml界面 ?MainActivity.java類 ? ? ? ? ? (2)視頻列表界面

    2024年02月07日
    瀏覽(27)
  • Mini MP3 Player播放器簡(jiǎn)介與STC12例程

    Mini MP3 Player播放器簡(jiǎn)介與STC12例程

    The DFPlayer Mini MP3 Player For Arduino is a small and low price MP3 module with an simplified output directly to the speaker. The module can be used as a stand alone module with attached battery, speaker and push buttons or used in combination with an Arduino UNO or any other with RX/TX capabilities. 簡(jiǎn)介 DFPlayer迷你MP3播放器Arduino是一個(gè)小而便

    2024年02月08日
    瀏覽(96)
  • AI智能語(yǔ)音識(shí)別模塊(二)——基于Arduino的語(yǔ)音控制MP3播放器

    AI智能語(yǔ)音識(shí)別模塊(二)——基于Arduino的語(yǔ)音控制MP3播放器

    在前面一篇文章里我們對(duì)AI智能語(yǔ)音識(shí)別模塊進(jìn)行了介紹,并對(duì)離線語(yǔ)音模組下載固件的過程進(jìn)行了一個(gè)簡(jiǎn)單描述,不知道大家還記不記得,這篇文章也是鴿了好久,,本文將用這個(gè)語(yǔ)音控制模塊結(jié)合前面介紹的DFPlayer Mini MP3模塊來做一個(gè)有趣的應(yīng)用,在上一期文章中,我們只

    2024年02月03日
    瀏覽(29)
  • Android Studio初學(xué)者實(shí)例:仿網(wǎng)易音樂播放器

    Android Studio初學(xué)者實(shí)例:仿網(wǎng)易音樂播放器

    本期帶來的是以Service為主要的知識(shí)點(diǎn)的網(wǎng)易音樂播放器 看一下效果圖 ?首先項(xiàng)目準(zhǔn)備: 在res下新建raw文件夾,并在文件夾中添加喜愛的mp3音樂 ?OK,第一步,先寫一個(gè)背景文件,在res/drawable文件夾中新建xml文件: btn_bg_selector.xml ?編寫主界面代碼activity_main.xml 編寫MusicServic

    2024年02月05日
    瀏覽(26)
  • 零基礎(chǔ)用Android Studio實(shí)現(xiàn)簡(jiǎn)單的本地視頻播放器

    零基礎(chǔ)用Android Studio實(shí)現(xiàn)簡(jiǎn)單的本地視頻播放器

    本文將講解如何使用Android視頻播放器VideoView組件來播放本地視頻和網(wǎng)絡(luò)視頻,實(shí)現(xiàn)起來還是比較簡(jiǎn)單的。VideoView組件的作用與ImageView類似,只是ImageView用于顯示圖片,VideoView用于播放視頻。 (本文章的代碼參考: https://www.jb51.net/article/122051.htm) 實(shí)現(xiàn)效果: 用Android Studio做的

    2024年02月02日
    瀏覽(25)
  • Android Studio初學(xué)者實(shí)例:音樂播放器與Service學(xué)習(xí)

    Android Studio初學(xué)者實(shí)例:音樂播放器與Service學(xué)習(xí)

    本次一個(gè)案例實(shí)現(xiàn)的一個(gè)簡(jiǎn)單的音樂播放器 用到的知識(shí)點(diǎn)最主要的幾點(diǎn)是:Service、handler(實(shí)現(xiàn)音樂播放的進(jìn)度條更新與圖片旋轉(zhuǎn))以及用于播放音頻的MediaPlayer 看一下案例效果: ?由于Service是Android的四大組件之一,Activity、Service等等一個(gè)重要知識(shí)點(diǎn)就是生命周期的問題,

    2024年02月03日
    瀏覽(42)
  • STM32F407使用Helix庫(kù)軟解MP3并通過DAC輸出,最精簡(jiǎn)的STM32+SD卡實(shí)現(xiàn)MP3播放器

    STM32F407使用Helix庫(kù)軟解MP3并通過DAC輸出,最精簡(jiǎn)的STM32+SD卡實(shí)現(xiàn)MP3播放器

    只用STM32單片機(jī)+SD卡+耳機(jī)插座,實(shí)現(xiàn)播放MP3播放器! 看過很多STM32軟解MP3的方案,即不通過類似VS1053之類的解碼器芯片,直接用STM32和軟件庫(kù)解碼MP3文件,通常使用了labmad或者Helix解碼庫(kù)實(shí)現(xiàn),Helix相對(duì)labmad占用的RAM更少。但是大多數(shù)參考的方案還是用了外接IIS接口WM98xx之類的

    2024年02月12日
    瀏覽(30)
  • MP4轉(zhuǎn)AVI轉(zhuǎn)AMV教程:教你把B站視頻導(dǎo)入你的MP3MP4隨身聽播放器

    MP4轉(zhuǎn)AVI轉(zhuǎn)AMV教程:教你把B站視頻導(dǎo)入你的MP3MP4隨身聽播放器

    學(xué)校不給帶手機(jī)?去到學(xué)校就不能看視頻了?我該怎么辦? 別慌!UP主教你如何把你喜歡的視頻導(dǎo)入到自己的隨身聽上! 前言:想必很多人都買過或者手上都有MP3/MP4播放器,在學(xué)校查手機(jī)變態(tài)嚴(yán)的中學(xué)時(shí)代,這個(gè)或許是唯一可以替代手機(jī)的慰藉了吧……每天晚上聽著歌度過

    2023年04月25日
    瀏覽(23)
  • Android 音樂播放器

    Android 音樂播放器

    ? 音樂播放器 . ? 要求 : Activity 編程、 ListView 編程、 SeekBar 編程、 ExoPlayer 編程( 播放 、 暫停 、 停止 、 上一首 、 下一首 ),音樂文件放在 assets/music 目錄下,界面自擬. ? 期望最終效果: ? 分別對(duì)應(yīng) activity_music_list.xml 、 activity_my_music_player.xml 的視圖. ? 點(diǎn)擊列表任

    2024年02月03日
    瀏覽(27)
  • Android 播放mp3文件

    Android 播放mp3文件

    1,在res/raw中加入mp3文件 2,實(shí)現(xiàn)播放類 3,初始化聲音播放類,一般在onCreate中調(diào)用 4,調(diào)用播放,初始化后可以多次調(diào)用 5,播放速度與音量等參數(shù)調(diào)節(jié)

    2024年02月09日
    瀏覽(20)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包