乐趣区

Android简易音乐播放器

功能:播放,暂停,上一首,下一首

部分 demo:

MainActivity:

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

// 定义音乐的播放状态,1 代表没有播放;2 代表正在播放;3 代表暂停

int status = 1;

String[] titleStrs = new String[] {“ 无羁 ”, “ 莲花坞 ”, “ 荒城渡 ”};

String[] authorStrs = new String[] {“ 王一博 ”, “ 林海 ”, “ 周深 ”};

Integer[] covers = new Integer[] {R.drawable.unrestrained, R.drawable.lotus,

R.drawable.barrencity};

@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 1:

play.setImageResource(R.drawable.play);

status = 1;

break;

// 控制系统进入播放状态

case 2:

// 播放状态下设置使用暂停图标

play.setImageResource(R.drawable.pause);

// 设置当前状态
外汇经纪商对比 http://www.fx61.com/brokerlist

status = 2;

break;

// 控制系统进入暂停状态

case 3:

// 暂停状态下设置使用播放图标

play.setImageResource(R.drawable.play);

// 设置当前状态

status = 3;

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

}

}

MusicService:

public class MusicService extends Service

{

MyReceiver serviceReceiver;

AssetManager am;

String[] musics = new String[] {“unrestrained.mp3” +

“”, “lotus.mp3”,

“barrencity.mp3”};

MediaPlayer mPlayer;

// 当前的状态,1 代表没有播放;2 代表正在播放;3 代表暂停

int status = 1;

// 记录当前正在播放的音乐

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 == 1)

{

// 准备并播放音乐

prepareAndPlay(musics[current]);

status = 2;

}

// 原来处于播放状态

else if (status == 2)

{

// 暂停

mPlayer.pause();

// 改变为暂停状态

status = 3;

}

// 原来处于暂停状态

else if (status == 3)

{

// 播放

mPlayer.start();

// 改变状态

status = 2;

}

break;

// 停止声音

case 2:

// 如果原来正在播放或暂停

if (status == 2 || status == 3)

{

// 停止播放

mPlayer.stop();

status = 1;

}

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

}

}

}

退出移动版