AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hanqi.testservice"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MusicPlay2">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MusicPlay1">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <service
android:name=".TestService1"
android:enabled="true"
android:exported="true" />
<service
android:name=".MusicService"
android:enabled="true"
android:exported="true"></service>
</application> </manifest>

activity_music_play1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hanqi.blacklist.MusicPlay1"
android:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="播放状态:"
android:id="@+id/tv_1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="播放"
android:onClick="play_onclick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="暂停"
android:onClick="pause_onclick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="停止"
android:onClick="stop_onclick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="退出"
android:onClick="exit_onclick"/>
</LinearLayout> </LinearLayout>

MusicPlay1.java

package com.hanqi.testservice;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView; public class MusicPlay1 extends AppCompatActivity {
TextView tv1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music_play1); tv1 = (TextView)findViewById(R.id.tv_1);
tv1.setText("播放状态:停止");
}
//媒体播放器的类
private MediaPlayer mediaPlayer;
public void play_onclick(View v)
{
if (mediaPlayer == null) {
//调用MediaPlayer的静态方法create()
mediaPlayer = MediaPlayer.create(this, R.raw.shinain);
}
mediaPlayer.start(); tv1.setText("播放状态:正在播放");
}
public void stop_onclick(View v)
{
if (mediaPlayer != null)
{
mediaPlayer.stop();//停止
mediaPlayer.reset();//重置
mediaPlayer.release();//释放
mediaPlayer = null;
}
tv1.setText("播放状态:停止");
}
public void pause_onclick(View v)
{
if (mediaPlayer != null&& mediaPlayer.isPlaying())
{
mediaPlayer.pause();
tv1.setText("播放状态:暂停");
} }
public void exit_onclick(View v)
{
stop_onclick(v);
//退出
finish(); } }

MusicService.java

package com.hanqi.testservice;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder; public class MusicService extends Service {
public MusicService() {
} @Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
} private MediaPlayer mediaPlayer;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//获取意图传递的信息
String action = intent.getStringExtra("action");
switch (action)
{
case "play":
if (mediaPlayer == null)
{
mediaPlayer = MediaPlayer.create(this,R.raw.shinain);
}
mediaPlayer.start();
break;
case "stop":
if (mediaPlayer != null)
{
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
break;
case "pause":
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
break;
} return super.onStartCommand(intent, flags, startId);
}
}

MusicPlay2.java

package com.hanqi.testservice;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView; public class MusicPlay2 extends AppCompatActivity {
TextView tv1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music_play1); tv1 = (TextView)findViewById(R.id.tv_1);
tv1.setText("播放状态1:停止");
} public void play_onclick(View v)
{
Intent intent = new Intent(this,MusicService.class);
intent.putExtra("action", "play");
startService(intent);
tv1.setText("播放状态1:正在播放...");
}
public void stop_onclick(View v)
{
Intent intent = new Intent(this,MusicService.class);
intent.putExtra("action","stop");
startService(intent);
tv1.setText("播放状态1:停止");
}
public void pause_onclick(View v)
{
Intent intent = new Intent(this,MusicService.class);
intent.putExtra("action","pause");
startService(intent);
tv1.setText("播放状态1:暂停");
}
public void exit_onclick(View v)
{
stop_onclick(v);
//退出
finish(); } }

andorid Activity和Service音乐播放器的更多相关文章

  1. Android基于发展Service音乐播放器

    这是一个基于Service组件的音乐播放器,程序的音乐将会由后台的Service组件负责播放,当后台的播放状态改变时,程序将会通过发送广播通知前台Activity更新界面:当用户单击前台Activit ...

  2. Android开发6:Service的使用(简单音乐播放器的实现)

    前言 啦啦啦~各位好久不见啦~博主最近比较忙,而且最近一次实验也是刚刚结束~ 好了不废话了,直接进入我们这次的内容~ 在这篇博文里我们将学习Service(服务)的相关知识,学会使用 Service ...

  3. Android(java)学习笔记234: 服务(service)之音乐播放器

    1.我们播放音乐,希望在后台长期运行,不希望因为内存不足等等原因,从而导致被gc回收,音乐播放终止,所以我们这里使用服务Service创建一个音乐播放器. 2.创建一个音乐播放器项目(使用服务) (1 ...

  4. Android(java)学习笔记177: 服务(service)之音乐播放器

    1.我们播放音乐,希望在后台长期运行,不希望因为内存不足等等原因,从而导致被gc回收,音乐播放终止,所以我们这里使用服务Service创建一个音乐播放器. 2.创建一个音乐播放器项目(使用服务) (1 ...

  5. android 音乐播放器

    本章以音乐播放器为载体,介绍android开发中,通知模式Notification应用.主要涉及知识点Notification,seekbar,service. 1.功能需求 完善音乐播放器 有播放列 ...

  6. Android音乐播放器的开发实例

    本文将引导大家做一个音乐播放器,在做这个Android开发实例的过程中,能够帮助大家进一步熟悉和掌握学过的ListView和其他一些组件.为了有更好的学习效果,其中很多功能我们手动实现,例如音乐播放的 ...

  7. Android实现简单音乐播放器(MediaPlayer)

    Android实现简单音乐播放器(MediaPlayer) 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 工程内容 实现一个简单的音乐播放器,要求功能 ...

  8. Android开发之音乐播放器的实现

    Android音乐播放器 使用到Android的Actiivity和Service组件 播放音频的代码应该运行在服务中,定义一个播放服务MusicService,服务里定义play.stop.paus ...

  9. android快捷简单的实现音乐播放器

    自己做了一个相对完整的音乐播放器,现在把播放模块提取出来,分享给大家.音乐播放器基本功能都实现了的,可能有些BUG,希望谅解. 播放器功能如下: 1.暂停,播放 2.拖动条实现,快进,快退 3.歌词同 ...

随机推荐

  1. php开发api接口

    做过 API 的人应该了解,其实开发 API 比开发 WEB 更简洁,但可能逻辑更复杂,因为 API 其实就是数据输出,不用呈现页面,所以也就不存在 MVC(API 只有 M 和 C),那么我们来探讨 ...

  2. 基于Spring框架的Shiro配置

    一.在web.xml中添加shiro过滤器  <!-- Shiro filter--> <filter> <filter-name>shiroFilter</ ...

  3. [uboot]E9-i.MX6Q-uboot移植

      参照:http://blog.csdn.net/girlkoo/article/details/45420977 文档参照: <i.MX BSP Porting Guide-2015/12, ...

  4. 原生+jquery 实现好看滚动条。

    //原生 <!doctype html> <html> <head> <meta charset="utf-8"> <titl ...

  5. Python正则表达式指南

    1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分.正则表达式是用于处理字符串的强大工具,拥有自己独特的语法以及一个独立的处理引擎,效率上可能不如str自带的方法,但功能十 ...

  6. android studio新项目时提示:Plugin is too old, please update to a more recent version

    今天想写一个程序来测试一下android studo代码,但是创建好项目后,提示: Error:(1, 0) Plugin is too old, please update to a more re ...

  7. 慕课网-安卓工程师初养成-5-4 使用 Eclipse 调试程序

    来源:http://www.imooc.com/video/1627 IDE断点调试功能 比如 之前的程序,写错了,变成如下 package com.imooc; import java.util.S ...

  8. IEEE浮点数表示法之出小数

    纯小数的表示方法-------------------------------------------------    下面再来讲如何将纯小数转化为十六进制.对于纯小数,比如0.0456,我们需要把 ...

  9. 在python中处理XML

    XML是实现不同语言或程序之间进行数据交换的协议,XML文件格式如下: <data> <country name="Liechtenstein"> < ...

  10. The Magic only works with total devotion of one's heart

    The Magic only works with total devotion of one's heart All tools and equipments are useless without ...