Mp3Player(4) PlayerActivity、Intent、BroadcastReceiver、Service
-
//PlayerActivity.java
-
package com.lwb.mp3player;
-
-
import java.io.File;
-
import java.io.FileInputStream;
-
import java.io.FileNotFoundException;
-
import java.io.InputStream;
-
import java.util.ArrayList;
-
import java.util.LinkedList;
-
import java.util.Queue;
-
-
import com.lwb.lrc.LrcProcessor;
-
import com.lwb.model.Mp3Info;
-
import com.lwb.mp3player.AppConstant.PlayerMsg;
-
import com.lwb.mp3player.service.PlayerService;
-
-
import android.app.Activity;
-
import android.content.BroadcastReceiver;
-
import android.content.Context;
-
import android.content.Intent;
-
import android.content.IntentFilter;
-
import android.media.MediaPlayer;
-
import android.net.Uri;
-
import android.os.Bundle;
-
import android.os.Environment;
-
import android.os.Handler;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.widget.ImageButton;
-
import android.widget.TextView;
-
-
/*PlayerActivity播放界面Activity
-
*主要内容:
-
*1、开始、暂停、结束 歌曲播放 播放歌曲是通过启动另一个Service中实现的
-
*2、同步更新显示歌词 有播放歌曲中通过BroadcastReceiver发送来更新歌词的信息
-
**/
-
public class PlayerActivity extends Activity{
-
-
private IntentFilter intentFilter = null;
-
//IntentFilter详见:http://blog.csdn.net/today520/article/details/7000048
-
private BroadcastReceiver receiver = null;
-
-
/*onPause()该Activity暂停时
-
* 注销广播接收器BroadcastReceiver receiver:unregisterReceiver(receiver)
-
* */
-
@Override
-
protected void onPause() {
-
super.onPause();
-
unregisterReceiver(receiver);
-
}
-
-
/*onResume显示、再次显示时得
-
* 1、创建广播接收器receiver=new LrcMessageBroadcastreceiver()
-
* 2、注册BroadcastReceiver receiver: registerReceiver(receiver,getIntentFileter())
-
* getIntentFileter是注册的意思,在AndroidMainfest.xml也可以注册*/
-
@Override
-
protected void onResume() {
-
super.onResume();
-
receiver = new LrcMessageBroadcastReceiver();
-
registerReceiver(receiver,getIntentFileter());
-
}
-
-
/*IntentFilter getIntentFileter()获得IntentFilter
-
* 1、创建一个IntentFilter:intentFilter = new IntentFilter()
-
* 2、为IntentFilter添加Action:IntentFilter.addAction(AppConstant.LRC_MESSAGE_ACTION)
-
* */
-
private IntentFilter getIntentFileter(){
-
if(intentFilter==null){
-
intentFilter = new IntentFilter();
-
intentFilter.addAction(AppConstant.LRC_MESSAGE_ACTION);
-
}
-
return intentFilter;
-
}
-
-
/*LrcMessageBroadcastReceiver继承广播接收器BroadcastReceiver
-
* 并复写onRTeceiver(Context arg0,Intent intent)方法
-
* 1、获得Intent中的歌词:lrcMessage = intent.getStringExtra("lrcMessage");
-
* 2、显示歌词到屏幕:lrcTextView.setText(lrcMessage)
-
* 广播接收器,主要作用是接受Service所返回的广播,并且更新UI,也就是放置歌词的TextView*/
-
class LrcMessageBroadcastReceiver extends BroadcastReceiver{
-
@Override
-
public void onReceive(Context arg0, Intent intent) {
-
//从Intent对象中取出歌词信息,然后更新TexView
-
String lrcMessage = intent.getStringExtra("lrcMessage");
-
lrcTextView.setText(lrcMessage);
-
}
-
}
-
-
ImageButton beginButton = null ;
-
ImageButton pauseButton = null;
-
ImageButton stopButton = null;
-
-
private TextView lrcTextView = null;
-
private Mp3Info mp3Info = null;
-
private boolean isPlaying = false;
-
-
/*onCreate(Bundle savedInstanceState)获得用户点击了本地音乐列表中歌曲进行播放界面的显示
-
*1、获得用户点击要播放歌的信息
-
* Intent intent = getIntent();
-
* mp3Info = (Mp3Info)intent.getSerializableExtra("mp3Info");
-
*2、曲其中巩固一下的是为按钮设置点击监听事件
-
* beginButton = (ImageButton)findViewById(R.id.begin)
-
* beginButton.setOnClickListener(new BeginButtonListener())
-
* */
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.player);
-
Intent intent = getIntent();
-
mp3Info = (Mp3Info)intent.getSerializableExtra("mp3Info");
-
beginButton = (ImageButton)findViewById(R.id.begin);
-
pauseButton = (ImageButton)findViewById(R.id.pause);
-
stopButton = (ImageButton)findViewById(R.id.stop);
-
beginButton.setOnClickListener(new BeginButtonListener());
-
pauseButton.setOnClickListener(new PauseButtonListener());
-
stopButton.setOnClickListener(new StopButtonListener());
-
lrcTextView = (TextView)findViewById(R.id.lrcText);
-
}
-
-
-
/*BeginButtonListener implements OnClickListener
-
* 实现了OnClickListener接口并复写了onClick(view arg0)方法
-
* 实现控制歌曲开始播放
-
* 1、创建一个Intent对象,用于通知Service播放歌曲
-
* 2、设置要从这个Activity启动的ServiceIntent.setClass(PlayerActivity.this,PlayerService.class)
-
* 3、通过Intent传递播放歌曲信息
-
* intent.putExtra("mp3Info",mp3Info)
-
* intent.putExtra("MSG",AppConstant.PlayerMsg.PLAY_MSG
-
* 4、启动Service:startService(intent)
-
* */
-
class BeginButtonListener implements OnClickListener{
-
@Override
-
public void onClick(View arg0) {
-
//创建一个Intent对象,用于通知Service开始播放MP3
-
Intent intent = new Intent();
-
intent.setClass(PlayerActivity.this,PlayerService.class);
-
intent.putExtra("mp3Info", mp3Info);
-
intent.putExtra("MSG", AppConstant.PlayerMsg.PLAY_MSG);
-
//启动Service
-
startService(intent);
-
}
-
}
-
-
class PauseButtonListener implements OnClickListener{
-
@Override
-
public void onClick(View arg0) {
-
//通知Service停止播放MP3文件
-
Intent intent = new Intent();
-
intent.setClass(PlayerActivity.this, PlayerService.class);
-
intent.putExtra("mp3Info", mp3Info);
-
intent.putExtra("MSG", AppConstant.PlayerMsg.PAUSE_MSG);
-
startService(intent);
-
}
-
}
-
-
class StopButtonListener implements OnClickListener{
-
@Override
-
public void onClick(View arg0) {
-
//通知Service停止播放MP3文件
-
Intent intent = new Intent();
-
intent.setClass(PlayerActivity.this, PlayerService.class);
-
intent.putExtra("mp3Info", mp3Info);
-
intent.putExtra("MSG", AppConstant.PlayerMsg.STOP_MSG);
-
startService(intent);
-
lrcTextView.setText("");
-
}
-
}
-
}
布局 player.xml:
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android=""
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:orientation="vertical" >
-
<LinearLayout
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:layout_weight="1"
-
android:orientation="horizontal" >
-
<TextView
-
android:id="@+id/lrcText"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:singleLine="false" />
-
</LinearLayout>
-
<LinearLayout
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:layout_weight="3"
-
android:orientation="horizontal" >
-
<RelativeLayout
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:padding="10px" >
-
<ImageButton
-
android:id="@+id/begin"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_alignParentBottom="true"
-
android:layout_centerHorizontal="true"
-
android:layout_marginLeft="15px"
-
android:layout_marginRight="15px"
-
android:src="@drawable/begin" />
-
<ImageButton
-
android:id="@+id/pause"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_alignParentBottom="true"
-
android:layout_marginLeft="15px"
-
android:layout_marginRight="15px"
-
android:layout_toLeftOf="@id/begin"
-
android:src="@drawable/pause" />
-
<ImageButton
-
android:id="@+id/stop"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_alignParentBottom="true"
-
android:layout_marginLeft="15px"
-
android:layout_marginRight="15px"
-
android:layout_toRightOf="@id/begin"
-
android:src="@drawable/stop" />
-
</RelativeLayout>
-
</LinearLayout>
-
</LinearLayout>
阅读(561) | 评论(0) | 转发(0) |