Chinaunix首页 | 论坛 | 博客
  • 博客访问: 199918
  • 博文数量: 102
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1015
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-05 16:45
文章存档

2014年(73)

2013年(29)

我的朋友

分类: Android平台

2014-04-06 16:35:47

Mp3Player(4) PlayerActivity、Intent、BroadcastReceiver、Service


点击(此处)折叠或打开

  1. //PlayerActivity.java
  2. package com.lwb.mp3player;

  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.InputStream;
  7. import java.util.ArrayList;
  8. import java.util.LinkedList;
  9. import java.util.Queue;

  10. import com.lwb.lrc.LrcProcessor;
  11. import com.lwb.model.Mp3Info;
  12. import com.lwb.mp3player.AppConstant.PlayerMsg;
  13. import com.lwb.mp3player.service.PlayerService;

  14. import android.app.Activity;
  15. import android.content.BroadcastReceiver;
  16. import android.content.Context;
  17. import android.content.Intent;
  18. import android.content.IntentFilter;
  19. import android.media.MediaPlayer;
  20. import android.net.Uri;
  21. import android.os.Bundle;
  22. import android.os.Environment;
  23. import android.os.Handler;
  24. import android.view.View;
  25. import android.view.View.OnClickListener;
  26. import android.widget.ImageButton;
  27. import android.widget.TextView;

  28. /*PlayerActivity播放界面Activity
  29.  *主要内容:
  30.  *1、开始、暂停、结束 歌曲播放 播放歌曲是通过启动另一个Service中实现的
  31.  *2、同步更新显示歌词 有播放歌曲中通过BroadcastReceiver发送来更新歌词的信息
  32.  **/
  33. public class PlayerActivity extends Activity{
  34.     
  35.     private IntentFilter intentFilter = null;
  36.     //IntentFilter详见:http://blog.csdn.net/today520/article/details/7000048
  37.     private BroadcastReceiver receiver = null;
  38.     
  39.     /*onPause()该Activity暂停时
  40.      * 注销广播接收器BroadcastReceiver receiver:unregisterReceiver(receiver)
  41.      * */
  42.     @Override
  43.     protected void onPause() {
  44.         super.onPause();
  45.         unregisterReceiver(receiver);
  46.     }

  47.     /*onResume显示、再次显示时得
  48.      * 1、创建广播接收器receiver=new LrcMessageBroadcastreceiver()
  49.      * 2、注册BroadcastReceiver receiver: registerReceiver(receiver,getIntentFileter())
  50.      * getIntentFileter是注册的意思,在AndroidMainfest.xml也可以注册*/
  51.     @Override
  52.     protected void onResume() {
  53.         super.onResume();
  54.         receiver = new LrcMessageBroadcastReceiver();
  55.         registerReceiver(receiver,getIntentFileter());
  56.     }
  57.     
  58.     /*IntentFilter getIntentFileter()获得IntentFilter
  59.      * 1、创建一个IntentFilter:intentFilter = new IntentFilter()
  60.      * 2、为IntentFilter添加Action:IntentFilter.addAction(AppConstant.LRC_MESSAGE_ACTION)
  61.      * */
  62.     private IntentFilter getIntentFileter(){
  63.         if(intentFilter==null){
  64.             intentFilter = new IntentFilter();
  65.             intentFilter.addAction(AppConstant.LRC_MESSAGE_ACTION);
  66.         }
  67.         return intentFilter;
  68.     }
  69.     
  70.     /*LrcMessageBroadcastReceiver继承广播接收器BroadcastReceiver
  71.      * 并复写onRTeceiver(Context arg0,Intent intent)方法
  72.      * 1、获得Intent中的歌词:lrcMessage = intent.getStringExtra("lrcMessage");
  73.      * 2、显示歌词到屏幕:lrcTextView.setText(lrcMessage)
  74.      * 广播接收器,主要作用是接受Service所返回的广播,并且更新UI,也就是放置歌词的TextView*/
  75.     class LrcMessageBroadcastReceiver extends BroadcastReceiver{
  76.         @Override
  77.         public void onReceive(Context arg0, Intent intent) {    
  78.             //从Intent对象中取出歌词信息,然后更新TexView
  79.             String lrcMessage = intent.getStringExtra("lrcMessage");
  80.             lrcTextView.setText(lrcMessage);        
  81.         }        
  82.     }

  83.     ImageButton beginButton = null ;
  84.     ImageButton pauseButton = null;
  85.     ImageButton stopButton = null;

  86.     private TextView lrcTextView = null;
  87.     private Mp3Info mp3Info = null;
  88.     private boolean isPlaying = false;
  89.     
  90.     /*onCreate(Bundle savedInstanceState)获得用户点击了本地音乐列表中歌曲进行播放界面的显示
  91.      *1、获得用户点击要播放歌的信息
  92.      * Intent intent = getIntent();
  93.      * mp3Info = (Mp3Info)intent.getSerializableExtra("mp3Info");
  94.      *2、曲其中巩固一下的是为按钮设置点击监听事件
  95.      * beginButton = (ImageButton)findViewById(R.id.begin)
  96.      * beginButton.setOnClickListener(new BeginButtonListener())
  97.      * */
  98.     @Override
  99.     protected void onCreate(Bundle savedInstanceState) {
  100.         super.onCreate(savedInstanceState);
  101.         setContentView(R.layout.player);
  102.         Intent intent = getIntent();
  103.         mp3Info = (Mp3Info)intent.getSerializableExtra("mp3Info");    
  104.         beginButton = (ImageButton)findViewById(R.id.begin);
  105.         pauseButton = (ImageButton)findViewById(R.id.pause);
  106.         stopButton = (ImageButton)findViewById(R.id.stop);        
  107.         beginButton.setOnClickListener(new BeginButtonListener());
  108.         pauseButton.setOnClickListener(new PauseButtonListener());
  109.         stopButton.setOnClickListener(new StopButtonListener());
  110.         lrcTextView = (TextView)findViewById(R.id.lrcText);
  111.     }    

  112.     
  113.     /*BeginButtonListener implements OnClickListener
  114.      * 实现了OnClickListener接口并复写了onClick(view arg0)方法
  115.      * 实现控制歌曲开始播放
  116.      * 1、创建一个Intent对象,用于通知Service播放歌曲
  117.      * 2、设置要从这个Activity启动的ServiceIntent.setClass(PlayerActivity.this,PlayerService.class)
  118.      * 3、通过Intent传递播放歌曲信息
  119.      * intent.putExtra("mp3Info",mp3Info)
  120.      * intent.putExtra("MSG",AppConstant.PlayerMsg.PLAY_MSG
  121.      * 4、启动Service:startService(intent)
  122.      * */
  123.      class BeginButtonListener implements OnClickListener{
  124.         @Override
  125.         public void onClick(View arg0) {        
  126.             //创建一个Intent对象,用于通知Service开始播放MP3            
  127.             Intent intent = new Intent();
  128.             intent.setClass(PlayerActivity.this,PlayerService.class);
  129.             intent.putExtra("mp3Info", mp3Info);
  130.             intent.putExtra("MSG", AppConstant.PlayerMsg.PLAY_MSG);
  131.             //启动Service
  132.             startService(intent);
  133.         }    
  134.     }
  135.     
  136.      class PauseButtonListener implements OnClickListener{
  137.         @Override
  138.         public void onClick(View arg0) {
  139.             //通知Service停止播放MP3文件
  140.             Intent intent = new Intent();
  141.             intent.setClass(PlayerActivity.this, PlayerService.class);
  142.             intent.putExtra("mp3Info", mp3Info);
  143.             intent.putExtra("MSG", AppConstant.PlayerMsg.PAUSE_MSG);
  144.             startService(intent);
  145.         }    
  146.     }
  147.     
  148.      class StopButtonListener implements OnClickListener{
  149.         @Override
  150.         public void onClick(View arg0) {
  151.             //通知Service停止播放MP3文件
  152.             Intent intent = new Intent();
  153.             intent.setClass(PlayerActivity.this, PlayerService.class);
  154.             intent.putExtra("mp3Info", mp3Info);
  155.             intent.putExtra("MSG", AppConstant.PlayerMsg.STOP_MSG);
  156.             startService(intent);
  157.             lrcTextView.setText("");
  158.         }    
  159.     }
  160. }
布局 player.xml:

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:orientation="vertical" >
  6.     <LinearLayout
  7.         android:layout_width="fill_parent"
  8.         android:layout_height="fill_parent"
  9.         android:layout_weight="1"
  10.         android:orientation="horizontal" >
  11.         <TextView
  12.             android:id="@+id/lrcText"
  13.             android:layout_width="wrap_content"
  14.             android:layout_height="wrap_content"
  15.             android:singleLine="false" />
  16.     </LinearLayout>
  17.     <LinearLayout
  18.         android:layout_width="fill_parent"
  19.         android:layout_height="fill_parent"
  20.         android:layout_weight="3"
  21.         android:orientation="horizontal" >
  22.         <RelativeLayout
  23.             android:layout_width="fill_parent"
  24.             android:layout_height="wrap_content"
  25.             android:padding="10px" >
  26.             <ImageButton
  27.                 android:id="@+id/begin"
  28.                 android:layout_width="wrap_content"
  29.                 android:layout_height="wrap_content"
  30.                 android:layout_alignParentBottom="true"
  31.                 android:layout_centerHorizontal="true"
  32.                 android:layout_marginLeft="15px"
  33.                 android:layout_marginRight="15px"
  34.                 android:src="@drawable/begin" />
  35.             <ImageButton
  36.                 android:id="@+id/pause"
  37.                 android:layout_width="wrap_content"
  38.                 android:layout_height="wrap_content"
  39.                 android:layout_alignParentBottom="true"
  40.                 android:layout_marginLeft="15px"
  41.                 android:layout_marginRight="15px"
  42.                 android:layout_toLeftOf="@id/begin"
  43.                 android:src="@drawable/pause" />
  44.             <ImageButton
  45.                 android:id="@+id/stop"
  46.                 android:layout_width="wrap_content"
  47.                 android:layout_height="wrap_content"
  48.                 android:layout_alignParentBottom="true"
  49.                 android:layout_marginLeft="15px"
  50.                 android:layout_marginRight="15px"
  51.                 android:layout_toRightOf="@id/begin"
  52.                 android:src="@drawable/stop" />
  53.         </RelativeLayout>
  54.     </LinearLayout>
  55. </LinearLayout>

阅读(516) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~