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. 微分方程——包络和奇解

    对某些微分方程,存在一条(也可能多条)特殊的积分曲线,它并不属于方程的积分曲线族.但是,在这条特殊的积分曲线上的每一点处,都有积分曲线族中的一条曲线和它在此点相切.在几何学上,这条特殊的积分曲线称为上 ...

  2. U盘安装ubuntu server 12.04的问题检测不到CDROM的解决

    U盘安装ubuntu server 12.04的问题检测不到CDROM的解决 ========================== 我是u盘安装ubuntu 14 64Bit 也是出现同样的问题 用u ...

  3. Zerojudge解题经验交流

    题号:a001: 哈囉 背景知识:输出语句,while not eof 题号:a002: 簡易加法 背景知识:输出语句,while not eof,加法运算 题号:a003: 兩光法師占卜術 背景知识 ...

  4. winform建立非矩形窗体

    非规则窗体可能会需要加的功能代码: 1:因为没有了最上边的标题栏,所以需要加窗体鼠标拖动功能,在Form里面加如下代码: #region 移动窗体 // 移动窗体 const int WM_NCLBU ...

  5. React 附件动画API ReactCSSTransitionGroup

    React为动画提供了一个附加组件ReactTransitionGroup,这个附加组件是动画的底层API,并且还提供了一个附件组件ReactCSSTransitionGroup,ReactCSSTr ...

  6. Linux下grep命令

    2.grep命令 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本 ...

  7. windows无提示关闭页面

    今天碰到个问题,需要自动关闭网页,网上找了方法,一直在火狐测试,一直没反应,还以为写错了,后来发现用火狐需要进行设置(后文有提供方法),IE可正常使用... 下面提供部分代码: 需要自动关闭网页,可以 ...

  8. 如何在不同编程语言中获取现在的Unix时间戳(Unix timestamp)

    Java time JavaScript Math.round(new Date().getTime()/1000)getTime()返回数值的单位是毫秒 Microsoft .NET / C# ep ...

  9. js节点属性

    在文档对象模型 (DOM) 中,每个节点都是一个对象.DOM 节点有三个重要的属性 : 1. nodeName : 节点的名称 2. nodeValue :节点的值 3. nodeType :节点的类 ...

  10. spring mvc 初始化错误

    java.lang.NoSuchMethodError: org.springframework.util.ClassUtils.isPresent(Ljava/lang/String;Ljava/l ...