最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)簡(jiǎn)易的音樂(lè)播放器

 更新時(shí)間:2021年05月13日 10:58:47   作者:m0_46515651  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)易的音樂(lè)播放器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

功能介紹

本次實(shí)驗(yàn)實(shí)現(xiàn)的是使用Andriod Studio開(kāi)發(fā)一個(gè)簡(jiǎn)易的音樂(lè)播放器,所包含的功能有音樂(lè)的播放、暫停、上一曲、下一曲、音樂(lè)播放的進(jìn)度以及手動(dòng)拖動(dòng)來(lái)控制音樂(lè)的播放進(jìn)度。

實(shí)現(xiàn)過(guò)程

導(dǎo)入項(xiàng)目所需的音樂(lè)文件、圖標(biāo)、背景等

1.創(chuàng)建一個(gè)raw文件夾,將音樂(lè)文件導(dǎo)入到這個(gè)文件夾中,方便我們?cè)陧?xiàng)目中使用

2.在drawable中導(dǎo)入所需的圖片、圖標(biāo)

設(shè)計(jì)UI界面

1.設(shè)計(jì)5個(gè)button控件,分別對(duì)應(yīng)上一曲,下一曲,暫停,播放,退出

2.設(shè)計(jì)3個(gè)TextView,分別對(duì)應(yīng)歌曲的介紹信息、歌曲的進(jìn)度(歌曲的總時(shí)間和歌曲當(dāng)前播放的時(shí)間)、歌曲的名字

service服務(wù)的編寫(xiě)
創(chuàng)建一個(gè)MusicService對(duì)象繼承Service

MusicService所需要的成員變量

MyReceiver serviceReceiver;
Thread processThread;
AssetManager am;//是附件管理器,用于根據(jù)文件名找到文件所在并打開(kāi)文件
String[] musics = new String[]{"legendsneverdie.mp3", "promise.mp3",
        "beautiful.mp3"};//默認(rèn)顯示的歌曲信息
MediaPlayer mPlayer;
// 當(dāng)前的狀態(tài),0x11代表沒(méi)有播放;0x12代表正在播放;0x13代表暫停
int status = 0x11;
// 記錄當(dāng)前正在播放的音樂(lè)
int current = 0;

實(shí)現(xiàn)循環(huán)播放

public void onCreate() {
        super.onCreate();
        am = getAssets();

        // 創(chuàng)建BroadcastReceiver
        serviceReceiver = new MyReceiver();
        // 創(chuàng)建IntentFilter
        IntentFilter filter = new IntentFilter();
        filter.addAction(MainActivity.CTL_ACTION);
        registerReceiver(serviceReceiver, filter);


        // 創(chuàng)建MediaPlayer
        mPlayer = new MediaPlayer();
        // 為MediaPlayer播放完成事件綁定監(jiān)聽(tīng)器
        mPlayer.setOnCompletionListener(new OnCompletionListener()
        {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Log.d("musicService", "播放完成");
                current++;
                if (current >= 3) {
                    current = 0;
                }
                // 準(zhǔn)備并播放音樂(lè)
                prepareAndPlay(musics[current]);
                //發(fā)送廣播通知Activity更改文本框
                Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
                sendIntent.putExtra("current", current);
                sendIntent.putExtra("currentTime", mPlayer.getCurrentPosition());
                sendIntent.putExtra("totalTime", mPlayer.getDuration());
                // 發(fā)送廣播,將被Activity組件中的BroadcastReceiver接收到
                sendBroadcast(sendIntent);

            }
        });
    private void prepareAndPlay(String music) {
        try {
            // 打開(kāi)指定音樂(lè)文件
            AssetFileDescriptor afd = am.openFd(music);
            mPlayer.reset();
            // 使用MediaPlayer加載指定的聲音文件。
            mPlayer.setDataSource(afd.getFileDescriptor(),
                    afd.getStartOffset(), afd.getLength());
            // 準(zhǔn)備聲音
            mPlayer.prepare();
            // 播放
            mPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

實(shí)現(xiàn)刷新進(jìn)度條

processThread = new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                if (status == 0x12) {
                    try {
                        Thread.sleep(1000);
                        Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
                        sendIntent.putExtra("current", current);
                        sendIntent.putExtra("currentTime", mPlayer.getCurrentPosition());
                        sendIntent.putExtra("totalTime", mPlayer.getDuration());
                        // 發(fā)送廣播,將被Activity組件中的BroadcastReceiver接收到
                        sendBroadcast(sendIntent);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    });
    processThread.start();
}

廣播通信接收器的實(shí)現(xiàn)(用于實(shí)現(xiàn)和activity的通信)

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {
        int control = intent.getIntExtra("control", -1);
        Log.d("musicReceiver", "收到廣播, control=" + control);
        switch (control) {
            // 播放或暫停
            case 1:
                // 原來(lái)處于沒(méi)有播放狀態(tài)
                if (status == 0x11) {
                    // 準(zhǔn)備并播放音樂(lè)
                    prepareAndPlay(musics[current]);
                    status = 0x12;
                }
                // 原來(lái)處于播放狀態(tài)
                else if (status == 0x12) {
                    // 暫停
                    mPlayer.pause();
                    // 改變?yōu)闀和顟B(tài)
                    status = 0x13;
                }
                // 原來(lái)處于暫停狀態(tài)
                else if (status == 0x13) {
                    // 播放
                    mPlayer.start();
                    // 改變狀態(tài)
                    status = 0x12;
                }
                break;
                // 下一首
            case 2:
                if (status == 0x12 || status == 0x13) {
                    mPlayer.stop();
                    if (current + 1 >= musics.length) {
                        current = 0;
                    } else {
                        current++;
                    }
                    prepareAndPlay(musics[current]);
                    status = 0x12;
                    break;
                }
                // 上一首
            case 3:
                if (status == 0x12 || status == 0x13) {
                    mPlayer.stop();
                    if (current - 1 < 0) {
                        current = musics.length - 1;
                    } else {
                        current--;
                    }
                    prepareAndPlay(musics[current]);
                    status = 0x12;
                }

        }

        // 廣播通知Activity更改圖標(biāo)、文本框
        Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
        sendIntent.putExtra("update", status);
        sendIntent.putExtra("current", current);
        // 發(fā)送廣播,將被Activity組件中的BroadcastReceiver接收到
        sendBroadcast(sendIntent);
    }
}

activity的實(shí)現(xiàn)

初始化和動(dòng)態(tài)綁定接收器

// 獲取界面中顯示歌曲標(biāo)題、作者文本框
TextView title, author, currentTime, totalTime;
// 播放/暫停、停止按鈕
ImageButton play;
ImageView lastMusic, nextMusic;
// 進(jìn)度條
ProgressBar progressBar;

ActivityReceiver activityReceiver;

public static final String CTL_ACTION =
        "org.xr.action.CTL_ACTION";
public static final String UPDATE_ACTION =
        "org.xr.action.UPDATE_ACTION";
// 定義音樂(lè)的播放狀態(tài),0x11代表沒(méi)有播放;0x12代表正在播放;0x13代表暫停
int status = 0x11;
String[] titleStrs = new String[]{"Legends Never Die", "約定", "美麗新世界"};
String[] authorStrs = new String[]{"英雄聯(lián)盟", "周蕙", "伍佰"};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // 獲取程序界面界面中的兩個(gè)按鈕
    play = (ImageButton) this.findViewById(R.id.play);
    lastMusic = this.findViewById(R.id.lastMusic);
    nextMusic = this.findViewById(R.id.nextMusic);
    title = (TextView) findViewById(R.id.title);
    author = (TextView) findViewById(R.id.author);
    currentTime = findViewById(R.id.currentTime);
    totalTime = findViewById(R.id.totalTime);
    progressBar = findViewById(R.id.progressBar);


    // 為兩個(gè)按鈕的單擊事件添加監(jiān)聽(tīng)器
    play.setOnClickListener(this);
    lastMusic.setOnClickListener(this);
    nextMusic.setOnClickListener(this);

    activityReceiver = new ActivityReceiver();
    // 創(chuàng)建IntentFilter
    IntentFilter filter = new IntentFilter();
    // 指定BroadcastReceiver監(jiān)聽(tīng)的Action
    filter.addAction(UPDATE_ACTION);
    // 注冊(cè)BroadcastReceiver
    registerReceiver(activityReceiver, filter);

    Intent intent = new Intent(this, MusicService.class);
    // 啟動(dòng)后臺(tái)Service
    startService(intent);
}

設(shè)置activity的廣播接收器(接收service發(fā)送過(guò)來(lái)的廣播)

public void onReceive(Context context, Intent intent) {
    // 獲取Intent中的update消息,update代表播放狀態(tài)
    int update = intent.getIntExtra("update", -1);
    // 獲取Intent中的current消息,current代表當(dāng)前正在播放的歌曲
    int current = intent.getIntExtra("current", -1);
    int totalPosition = intent.getIntExtra("totalTime", -1);
    int currentPosition = intent.getIntExtra("currentTime", -1);
    Log.d("activityReceiver", "收到廣播");
    Log.d("activityReceiver", "current:" + current + " totalPosition:" + totalPosition + " currentPosition:" + currentPosition + " update:" + update);
    if (current >= 0) {
        title.setText(titleStrs[current]);
        author.setText(authorStrs[current]);
    }

    if (totalPosition >= 0) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss", Locale.CHINA);
        Date date = new Date(totalPosition);
        String formatTime = simpleDateFormat.format(date);
        totalTime.setText(formatTime);
    }

    if (currentPosition >= 0) {
        double process = ((double)currentPosition / totalPosition)*100;
        Log.d("activityReceiver", "當(dāng)前進(jìn)度:" + (double)currentPosition/totalPosition);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss", Locale.CHINA);
        Date date = new Date(currentPosition);
        String formatTime = simpleDateFormat.format(date);
        progressBar.setProgress((int) process);
        currentTime.setText(formatTime);
    }

    switch (update) {
        case 0x11:
            play.setImageResource(R.drawable.play);
            status = 0x11;
            break;
        // 控制系統(tǒng)進(jìn)入播放狀態(tài)
        case 0x12:
            // 播放狀態(tài)下設(shè)置使用暫停圖標(biāo)
            play.setImageResource(R.drawable.pause);
            // 設(shè)置當(dāng)前狀態(tài)
            status = 0x12;
            break;
        // 控制系統(tǒng)進(jìn)入暫停狀態(tài)
        case 0x13:
            // 暫停狀態(tài)下設(shè)置使用播放圖標(biāo)
            play.setImageResource(R.drawable.play);
            // 設(shè)置當(dāng)前狀態(tài)
            status = 0x13;
            break;
    }
}

實(shí)現(xiàn)圖標(biāo)的點(diǎn)擊功能

// 創(chuàng)建Intent
    Intent intent = new Intent("org.xr.action.CTL_ACTION");
    switch (source.getId()) {
        // 按下播放/暫停按鈕
        case R.id.play:
            intent.putExtra("control", 1);
            break;
        case R.id.lastMusic:
            intent.putExtra("control", 3);
        case R.id.nextMusic:
            intent.putExtra("control", 2);
    }
    // 發(fā)送廣播,將被Service組件中的BroadcastReceiver接收到
    sendBroadcast(intent);
}

結(jié)果展示

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

相關(guān)文章

  • Android自定義TextView實(shí)現(xiàn)文字圖片居中顯示的方法

    Android自定義TextView實(shí)現(xiàn)文字圖片居中顯示的方法

    下面小編就為大家分享一篇Android自定義TextView實(shí)現(xiàn)文字圖片居中顯示的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • Android Studio 新手入門(mén)教程(一)基本設(shè)置圖解

    Android Studio 新手入門(mén)教程(一)基本設(shè)置圖解

    這篇文章主要介紹了Android Studio 新手入門(mén)教程(一)基本設(shè)置圖解,需要的朋友可以參考下
    2017-12-12
  • Android中使用ScrollView指定view的頂部懸停效果

    Android中使用ScrollView指定view的頂部懸停效果

    在項(xiàng)目開(kāi)發(fā)中遇到這樣的需求,需要實(shí)現(xiàn)scrollview頂部的懸停效果,實(shí)現(xiàn)原理非常簡(jiǎn)單,下面小編通過(guò)本文給大家分享實(shí)例代碼,需要的朋友參考下
    2017-04-04
  • Android小掛件(APP Widgets)設(shè)計(jì)指導(dǎo)

    Android小掛件(APP Widgets)設(shè)計(jì)指導(dǎo)

    這篇文章主要為大家詳細(xì)介紹了Android小掛件APP Widgets設(shè)計(jì)指導(dǎo),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Android Studio多渠道批量打包及代碼混淆

    Android Studio多渠道批量打包及代碼混淆

    這篇文章主要介紹了Android Studio多渠道批量打包及代碼混淆的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-09-09
  • android ImageView 的幾點(diǎn)經(jīng)驗(yàn)總結(jié)

    android ImageView 的幾點(diǎn)經(jīng)驗(yàn)總結(jié)

    本篇文章是對(duì)android中ImageView的使用技巧進(jìn)行了幾點(diǎn)經(jīng)驗(yàn)總結(jié),需要的朋友參考下
    2013-06-06
  • 詳解Gradle構(gòu)建過(guò)程

    詳解Gradle構(gòu)建過(guò)程

    Gradle是項(xiàng)目構(gòu)建工具,是Google官方推薦的Android項(xiàng)目編譯工具。構(gòu)建工具是可以讓開(kāi)發(fā)者以可執(zhí)行和有序的任務(wù)來(lái)表達(dá)自動(dòng)化的需求。就是將源代碼生成可執(zhí)行程序。本文將詳細(xì)介紹Gradle構(gòu)建過(guò)程
    2021-06-06
  • Android客戶端程序Gradle如何打包

    Android客戶端程序Gradle如何打包

    這篇文章主要介紹了Android客戶端程序Gradle如何打包 的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • Android編程中的四大基本組件與生命周期詳解

    Android編程中的四大基本組件與生命周期詳解

    這篇文章主要介紹了Android編程中的四大基本組件與生命周期,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android四大組件及生命周期的相關(guān)概念與使用技巧,需要的朋友可以參考下
    2015-12-12
  • Flutter異步操作實(shí)現(xiàn)流程詳解

    Flutter異步操作實(shí)現(xiàn)流程詳解

    在Flutter中,借助 FutureBuilder 組件和 StreamBuilder 組件,可以非常方便地完成異步操作,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2022-09-09

最新評(píng)論

富蕴县| 灵武市| 贡觉县| 六盘水市| 疏勒县| 南京市| 孟连| 新郑市| 资中县| 康平县| 航空| 循化| 安国市| 桦南县| 西乌珠穆沁旗| 方山县| 石家庄市| 姚安县| 桂林市| 西华县| 泰宁县| 宝清县| 新河县| 绥德县| 灵石县| 吴忠市| 洪洞县| 阿瓦提县| 博兴县| 通榆县| 库车县| 平远县| 永嘉县| 吉林市| 祥云县| 台北市| 民县| 浦县| 长兴县| 休宁县| 丁青县|