该播放器只是对本地的MP3文件进行简单的播放

布局文件


布局文件中设置了三个按钮分别来进行播放、暂停和继续播放

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始播放"
android:onClick="play"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停播放"
android:onClick="pause"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="继续播放"
android:onClick="continuePlay"/>
</LinearLayout>

Activity


在Activity中开启服务, 在服务中实现MP3的播放功能,然后通过一个Binder对象将该功能暴露给Activity。则可以在Activity中调用Service中的方法。

package xidian.dy.com.chujia;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.view.View; public class MainActivity extends AppCompatActivity {
IMusic music;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(this, MusicService.class);
startService(intent);
bindService(intent, new MyConnection(), BIND_AUTO_CREATE);
} public void play(View v){
music.play();
} public void pause(View v){
music.pause();
}
public void continuePlay(View v){
music.continuePlay();
} class MyConnection implements ServiceConnection{ @Override
public void onServiceConnected(ComponentName name, IBinder service) {
music = (IMusic) service;
} @Override
public void onServiceDisconnected(ComponentName name) { }
}
}

Service


在Service中创建一个MediaPlayer来控制音乐播放

package xidian.dy.com.chujia;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable; import java.io.IOException; /**
* Created by dy on 2016/7/29.
*/
public class MusicService extends Service {
MediaPlayer player; @Override
public void onCreate() {
player = new MediaPlayer();
super.onCreate();
} @Nullable
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
} public void continuePlay(){
player.start();
} public void pause(){
player.pause();
} public void play(){
player.reset();
try {
player.setDataSource("/sdcard/bc.mp3");
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
} @Override
public void onDestroy() {
super.onDestroy();
player.stop();
player.release();
} class MyBinder extends Binder implements IMusic{
@Override
public void play() {
MusicService.this.play();
} @Override
public void pause() {
MusicService.this.pause(); } @Override
public void continuePlay() {
MusicService.this.continuePlay();
}
}
}

IMusic


IMusic是一个接口类,主要是为了能在Activity中调用service中的方法。

package xidian.dy.com.chujia;

/**
* Created by dy on 2016/7/29.
*/
public interface IMusic {
void play();
void pause();
void continuePlay();
}

清单文件


在清单文件中需要对服务进行注册

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xidian.dy.com.chujia"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="主界面">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MusicService"/>
</application>
</manifest>

android之MP3播放器(1)的更多相关文章

  1. 【Android】利用安卓的数据接口、多媒体处理编写内存卡Mp3播放器app

    通过调用安卓的MediaPlayer能够直接完毕Mp3等主流音频的播放,同一时候利用ContentResolver与Cursor能够直接读取安卓内在数据库的信息.直接获取当前sdcard中全部音频的列 ...

  2. Android——简单音乐播放器

    使用MediaPlayer做的简单音乐播放器,更多内容请到百度经验查看   http://jingyan.baidu.com/article/60ccbceb63452364cab197f1.html ...

  3. 安卓MP3播放器开发实例(1)之音乐列表界面

    学习安卓开发有一年了,想想这一年的努力,确实也收获了不少.也找到了比較如意的工作. 今天准备分享一个以前在初学阶段练习的一个项目.通过这个项目我真正的找到了开发安卓软件的感觉,从此逐渐步入安卓开发的正 ...

  4. MP3播放器团队项目

    一.设计思路 程序要求能播放MP3文件,因此需调用库中的播放方法:右键工具箱选择项,添加com组件,选择window media player后工具箱就会多一个控件,然后拖到窗体中就OK了.另在窗体中 ...

  5. 你也可以用java的swing可以做出这么炫的mp3播放器_源码下载

    I had published the blog : 你用java的swing可以做出这么炫的mp3播放器吗? and to display some screenshots about this M ...

  6. 你用java的swing可以做出这么炫的mp3播放器吗?

    这个mp3播放器是基于java的swing编写的,我认为界面还是可以拿出来和大家看一看评一评. 先说说创作的初衷,由于前段时间工作不是很忙,与其闲着,还不如找一些东西来给自己捣腾捣腾,在 之前写的 j ...

  7. MP3播放器的实现

    今天,基本上实现了MP3播放器的基本功能,现在总结一下. 首先,下载服务器端的MP3列表,这里用到了下载技术和解析XML文件技术. 下载参考(http://blog.csdn.net/huim_lin ...

  8. 开源mp3播放器--madplay 编译和移植 简记

    madplay是一款开源的mp3播放器. http://madplay.sourcearchive.com/ 下面简单记录一下madplay的编译与移植到ARM开发板上的过程 一.编译x86版本的ma ...

  9. 基于Stm32的MP3播放器设计与实现

    原创博文,转载请注明出处 这是我高级电子技术试验课做的作业,拿来共享一下.项目在安福莱例程基础之上进行的功能完善,里面的部分内容可参考安福莱mp3例程.当然用的板子也是安福莱的板子,因为算起来总共做了 ...

随机推荐

  1. yum或apt基本源设置指南

    关于: 管理Linux服务器的运维或开发人员经常需要安装软件,最常用方式应该是通过Linux系统提供的包管理工具来在线安装,比如centos的yum,ubuntu或debian的apt-get.当然这 ...

  2. CentOS6.5搭建LNMP

      1:查看环境: ? 1 2 [root@10-4-14-168 html]# cat /etc/redhat-release CentOS release 6.5 (Final) 2:关掉防火墙 ...

  3. ELF Format 笔记(三)—— Section Types

    ilocker:关注 Android 安全(新入行,0基础) QQ: 2597294287 ELF 文件中会包含很多 section,所有的 section 都在 section header tab ...

  4. jQuery入门第二天&&&正则表达式完结篇——仿smarty引擎的制作

    hi 周一完全的不在状态...中午还去观战,没有睡觉的我,晚上的smarty不知道能不能做完,加油吧 1.jQuery ---过滤性选择器(二)--- --[attribute=value]属性选择器 ...

  5. Android+Sqlite 实现古诗阅读应用(二)

    传送门:Android+Sqlite 实现古诗阅读应用(一) Hi,又回来了,最近接到很多热情洋溢的小伙伴们的来信,吼开心哈,我会继续努力的=-=! 上回的东西我们做到了有个textview能随机选择 ...

  6. BZOJ1216[HNOI2003]操作系统 [模拟 优选队列]

    1216: [HNOI2003]操作系统 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 754  Solved: 421[Submit][Status ...

  7. POJ3783Balls[DP 最坏情况最优解]

    Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 907   Accepted: 598 Description T ...

  8. 第64课 C++中的异常处理(上)

    1. C++内置的异常处理:try-catch (1)try语句处理正常代码逻辑 (2)catch语句处理异常情况 (3)try语句中的异常由对应的catch语句处理,如果对应的catch中没有处理该 ...

  9. TCP聊天工具

    //前台书写 import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java ...

  10. fragment的切换(解决REPLACE的低效)

    在项目中切换Fragment,一直都是用replace()方法来替换Fragment.但是这样做有一个问题,每次切换的时候Fragment都会重新实列化,重新加载一次数据,这样做会非常消耗性能用用户的 ...