Android Studio完成简易音乐盒

267 阅读3分钟
**一.实验目的**

使用Android Studio,了解并利用其中的Service和Broadcast机制完成一个简易的音乐播放器。这个播放器可以完成:在音乐正在播放时,点击播放键暂停,未播放时,点击播放键音乐继续播放,点击stop键实现停止播放,点击上一首,下一首实现音乐播放顺序的转换,在播放音乐时,显示正在播放音乐的标题和演唱者名。

二.实验环境

Android Studio

gitee代码仓库(gitee.com/shezhenlong…

三.实验步骤

1.导入歌曲和图片并完成文件main.xml

一般是在res文件夹下创建raw文件夹存放歌曲,我放在assets文件夹里,图片放在drawble里

拖控件进行布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical">

   <ImageView
      android:id="@+id/imageView"
      android:layout_width="match_parent"
      android:layout_height="260dp"
      android:src="@drawable/music" />

   <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">

      <TextView
         android:id="@+id/title"
         android:layout_width="match_parent"
         android:layout_height="40sp"
         android:gravity="center"
         android:textSize="20dp" />

      <TextView
         android:id="@+id/author"
         android:layout_width="match_parent"
         android:layout_height="40sp"
         android:gravity="center"
         android:textSize="20dp" />
   </LinearLayout>

   <LinearLayout
      android:id="@+id/rl"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_marginBottom="20dp"
      android:gravity="center"
      android:orientation="horizontal">

      <ImageButton
         android:id="@+id/last"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:background="#F9FAFA"
         android:src="@drawable/last"/>

      <ImageButton
         android:id="@+id/play"
         android:layout_width="111dp"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:background="#F9FAFA"
         android:src="@drawable/play"/>

      <ImageButton
         android:id="@+id/stop"
         android:layout_width="112dp"
         android:layout_height="match_parent"
         android:background="#F9FAFA"
         android:src="@drawable/stop" />

      <ImageButton
         android:id="@+id/next"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:background="#F9FAFA"
         android:src="@drawable/next" />
   </LinearLayout>
</LinearLayout>

2.编写MainActivity.java文件

主要完成以下功能

(1)自定义了BroadcastReceiver,负责监听从Service传回来的广播,用switch控制系统状态

   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]);
      }

      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;
      }
   }
}

(2)完成点击事件,用switch判断点击事件,向Service中的receiver发送广播

public void onClick(View source)
{
   // 创建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.stop:
         intent.putExtra("control", 2);
         break;
      //按下上一首按钮
      case R.id.last:
         intent.putExtra("control",3);
         break;
      //按下下一首按钮
      case  R.id.next:
         intent.putExtra("control",4);
         break;
   }

3.编写MusicService.java文件

实现以下功能

(1)列表循环播放

// 创建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]);
      }
   });
}

(2)MyReceiver子类,接收MainActivity发来的信息,完成MyReceiver播放控制。

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;
            }
            break;
         //上一曲
         case 3:
            current--;
            if (current < 0)
            {
               current = 2;
            }
            // 准备并播放音乐
            prepareAndPlay(musics[current]);
            break;
         case 4:
            current++;
            if (current >= 3)
            {
               current = 0;
            }
            // 准备并播放音乐
            prepareAndPlay(musics[current]);
            break;
      }
      // 广播通知Activity更改图标、文本框
      Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
      sendIntent.putExtra("update", status);
      sendIntent.putExtra("current", current);
      // 发送广播,将被Activity组件中的BroadcastReceiver接收到
      sendBroadcast(sendIntent);
   }
}

(3)准备并播放音乐

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();
   }
}

4.完成结果截图