Android studio 音樂播放器提交

這裏要完成音樂播放,我們需要使用到Broadcast實現模式,也就是我們所說的廣播
先展示一下項目成品哈:
在這裏插入圖片描述
首先是佈局文件:
(這裏的佈局我是直接採用的酷狗的音樂盒佈局)
layout/activity_main.xml

<?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"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/cover"
        android:layout_width="match_parent"
        android:layout_height="600dp"
        android:layout_weight="1"
        android:src="@drawable/composer" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_weight="1">

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:layout_weight="1"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:text="歌曲名"
            android:gravity="center"
            android:textColor="#000000"
            android:textSize="30dp" />

        <TextView
            android:id="@+id/author"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="藝術家"
            android:textSize="20dp" />
    </LinearLayout>


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:gravity="center"
        android:layout_marginBottom="10dp">
        <ImageButton
            android:id="@+id/pre"
            android:layout_width="wrap_content"

            android:layout_height="wrap_content"
            android:src="@drawable/pre"
            android:background="#FFFFFF"/>
        <ImageButton
            android:id="@+id/play"
            android:layout_width="wrap_content"

            android:layout_height="wrap_content"
            android:src="@drawable/play"
            android:background="#FFFFFF"/>
        <ImageButton
            android:id="@+id/next"
            android:layout_width="wrap_content"

            android:layout_height="wrap_content"
            android:src="@drawable/next"
            android:background="#FFFFFF"/>
        <!--停止在這不太好看,將其寬度設置爲0,先隱藏起來-->
        <ImageButton
            android:id="@+id/stop"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:src="@drawable/stop"
            android:background="#FFFFFF"/>
    </LinearLayout>
</LinearLayout>

在編寫main之前,我們不妨先將music類寫好
MusicService.java

package com.example.mymusicbox;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;

import java.io.IOException;

public class MusicService extends Service
{
    MyReceiver serviceReceiver;
    AssetManager am;
    String[] musics = new String[] { "composer.mp3" +
            "", "bridge.mp3",
            "graininear.mp3" };
    MediaPlayer mPlayer;
    // 當前的狀態,0x11代表沒有播放;0x12代表正在播放;0x13代表暫停
    int status = 0x11;
    // 記錄當前正在播放的音樂
    int current = 0;
    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }
    @Override
    public void onCreate()
    {
        super.onCreate();
        am = getAssets();
        // 創建BroadcastReceiver
        serviceReceiver = new MyReceiver();
        // 創建IntentFilter
        IntentFilter filter = new IntentFilter();
        filter.addAction(MainActivity.CTL_ACTION);
        registerReceiver(serviceReceiver, filter);
        // 創建MediaPlayer
        mPlayer = new MediaPlayer();
        // 爲MediaPlayer播放完成事件綁定監聽器
        mPlayer.setOnCompletionListener(new OnCompletionListener() // ①
        {
            @Override
            public void onCompletion(MediaPlayer mp)
            {
                current++;
                if (current >= 3)
                {
                    current = 0;
                }
                //發送廣播通知Activity更改文本框
                Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
                sendIntent.putExtra("current", current);
                // 發送廣播,將被Activity組件中的BroadcastReceiver接收到
                sendBroadcast(sendIntent);
                // 準備並播放音樂
                prepareAndPlay(musics[current]);
            }
        });
    }
    public class MyReceiver extends BroadcastReceiver
    {
        @Override
        public void onReceive(final Context context, Intent intent)
        {
            int control = intent.getIntExtra("control", -1);
            switch (control)
            {
                // 播放或暫停
                case 1:
                    // 原來處於沒有播放狀態
                    if (status == 0x11)
                    {
                        // 準備並播放音樂
                        prepareAndPlay(musics[current]);
                        status = 0x12;
                    }
                    // 原來處於播放狀態
                    else if (status == 0x12)
                    {
                        // 暫停
                        mPlayer.pause();
                        // 改變爲暫停狀態
                        status = 0x13;
                    }
                    // 原來處於暫停狀態
                    else if (status == 0x13)
                    {
                        // 播放
                        mPlayer.start();
                        // 改變狀態
                        status = 0x12;
                    }
                    break;
                // 停止聲音
                case 2:
                    // 如果原來正在播放或暫停
                    if (status == 0x12 || status == 0x13)
                    {
                        // 停止播放
                        mPlayer.stop();
                        status = 0x11;
                    }
                case 3:
                    current--;
                    if(current < 0)
                        current = 0;
                    prepareAndPlay(musics[current]);
                case 4:
                    current++;
                    if(current > musics.length-1)
                        current = 0;
                    prepareAndPlay(musics[current]);
            }
            // 廣播通知Activity更改圖標、文本框
            Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
            sendIntent.putExtra("update", status);
            sendIntent.putExtra("current", current);
            // 發送廣播,將被Activity組件中的BroadcastReceiver接收到
            sendBroadcast(sendIntent);
        }
    }
    private void prepareAndPlay(String music)
    {
        try
        {
            // 打開指定音樂文件
            AssetFileDescriptor afd = am.openFd(music);
            mPlayer.reset();
            // 使用MediaPlayer加載指定的聲音文件。
            mPlayer.setDataSource(afd.getFileDescriptor(),
                    afd.getStartOffset(), afd.getLength());
            // 準備聲音
            mPlayer.prepare();
            // 播放
            mPlayer.start();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

想必大家這是已經報錯了。。。不過不慌
隨後我們進入項目的xml中更改一個serviece
AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mymusicbox">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".MusicService">
        </service>
    </application>

</manifest>

誒 增加了service之後 舒服啦
最後就是我們的main.java啦
MainActivity.java

package com.example.mymusicbox;
import android.support.v4.app.*;
import android.os.Bundle;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener
{
    // 獲取界面中顯示歌曲標題、作者文本框
    TextView title, author;
    // 播放/暫停、停止按鈕
    ImageButton play, stop;
    // 上一首,下一首按鈕
    ImageButton pre, next;
    // 獲取封面
    ImageView cover;

    ActivityReceiver activityReceiver;

    public static final String CTL_ACTION =
            "org.crazyit.action.CTL_ACTION";
    public static final String UPDATE_ACTION =
            "org.crazyit.action.UPDATE_ACTION";
    // 定義音樂的播放狀態,0x11代表沒有播放;0x12代表正在播放;0x13代表暫停
    int status = 0x11;
    String[] titleStrs = new String[] { "作曲家", "木橋小謠", "芒種" };
    String[] authorStrs = new String[] { "李榮浩", "王梓鈺", "趙方婧" };
    Integer[] covers = new Integer[] { R.drawable.composer, R.drawable.bridge, R.drawable.graininear};

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

        // 獲取程序界面界面中的兩個按鈕
        play = (ImageButton) this.findViewById(R.id.play);
        stop = (ImageButton) this.findViewById(R.id.stop);
        title = (TextView) findViewById(R.id.title);
        author = (TextView) findViewById(R.id.author);
        cover = findViewById(R.id.cover);
        pre = this.findViewById(R.id.pre);
        next = this.findViewById(R.id.next);

        // 爲兩個按鈕的單擊事件添加監聽器
        play.setOnClickListener(this);
        stop.setOnClickListener(this);
        // 爲上一首、下一首的單擊事件添加監聽器
        pre.setOnClickListener(this);
        next.setOnClickListener(this);

        activityReceiver = new ActivityReceiver();
        // 創建IntentFilter
        IntentFilter filter = new IntentFilter();
        // 指定BroadcastReceiver監聽的Action
        filter.addAction(UPDATE_ACTION);
        // 註冊BroadcastReceiver
        registerReceiver(activityReceiver, filter);

        Intent intent = new Intent(this, MusicService.class);
        // 啓動後臺Service
        startService(intent);
    }
    // 自定義的BroadcastReceiver,負責監聽從Service傳回來的廣播
    public class ActivityReceiver extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            // 獲取Intent中的update消息,update代表播放狀態
            int update = intent.getIntExtra("update", -1);
            // 獲取Intent中的current消息,current代表當前正在播放的歌曲
            int current = intent.getIntExtra("current", -1);
            if (current >= 0)
            {
                title.setText(titleStrs[current]);
                author.setText(authorStrs[current]);
                cover.setImageResource(covers[current]);
            }
            switch (update)
            {
                case 0x11:
                    play.setImageResource(R.drawable.play);
                    status = 0x11;
                    break;
                // 控制系統進入播放狀態
                case 0x12:
                    // 播放狀態下設置使用暫停圖標
                    play.setImageResource(R.drawable.pause);
                    // 設置當前狀態
                    status = 0x12;
                    break;
                // 控制系統進入暫停狀態
                case 0x13:
                    // 暫停狀態下設置使用播放圖標
                    play.setImageResource(R.drawable.play);
                    // 設置當前狀態
                    status = 0x13;
                    break;
            }
        }
    }
    @Override
    public void onClick(View source)
    {
        // 創建Intent
        Intent intent = new Intent("org.crazyit.action.CTL_ACTION");
        switch (source.getId())
        {
            // 按下播放/暫停按鈕
            case R.id.play:
                intent.putExtra("control", 1);
                break;
            // 按下停止按鈕
            case R.id.stop:
                intent.putExtra("control", 2);
                break;
            case R.id.pre:
                intent.putExtra("control",3);
            case R.id.next:
                intent.putExtra("control",4);
        }
        // 發送廣播,將被Service組件中的BroadcastReceiver接收到
        sendBroadcast(intent);
    }
}


好了 至此,我們也就成功完成了一個簡單的音樂播放器。可以進行音樂的播放和上一首下一週的切換

最後是項目的源碼:
碼雲源碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章