用service实现音乐播放器
来源:互联网 发布:爱神巧克力夏紫瞳h本知 编辑:程序博客网 时间:2024/05/17 07:45
MainActivity.java
package com.example.musicplayservice;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;/*以service实现的简单的播放MP3功能 * 需要在res/raw中添加MP3文件 * 需要在andoridMainfeist中注册service以及aciton * */public class MainActivity extends Activity { private Button mBtn_play; private Button mBtn_stop;// 定义一个Action private String MusicAciton = "com.example.service.action.MUSIC_SERVICE"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBtn_play = (Button) findViewById(R.id.btn_play); mBtn_stop = (Button) findViewById(R.id.btn_stop); mBtn_play.setOnClickListener(new PlayBtnListener()); mBtn_stop.setOnClickListener(new StopBtnListener()); }// 播放按钮// 实现这个类时,首先执行的是service.create的方法,然后执行service.start方法, class PlayBtnListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent_play = new Intent(MusicAciton); intent_play.setClass(MainActivity.this, MusicPlayService.class);// 启动服务 startService(intent_play); } }// 停止播放并退出按钮// 实现这个类时,执行的是service.destroy方法 class StopBtnListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent_stop = new Intent(MusicAciton); intent_stop.setClass(MainActivity.this, MusicPlayService.class);// 停止服务 stopService(intent_stop);// 退出程序 finish(); } }}
MusicPlayerService.java
package com.example.musicplayservice;import android.app.Service;import android.content.Intent;import android.media.MediaPlayer;import android.os.IBinder;import android.util.Log;import android.view.View.OnCreateContextMenuListener;public class MusicPlayService extends Service { private MediaPlayer MusicPlayer = null; private String TAG = "MusicPlay"; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub Log.i(TAG, "onBind"); return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); Log.i(TAG, "onCreate"); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); MusicPlayer.stop(); } @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); Log.i(TAG, "onStart");// 创建一个Mediaplayer实例 MusicPlayer = MediaPlayer.create(this, R.raw.sky);// 播放 MusicPlayer.start(); }}
activity_main.xml
<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:orientation="vertical" > <Button android:id="@+id/btn_play" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/btn_play"/> <Button android:id="@+id/btn_stop" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/btn_stop"/></LinearLayout>
androidMainfeist.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.musicplayservice" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.example.musicplayservice.MusicPlayService"> <intent-filter > <action android:name="com.example.service.Action.MUSIC_SERVICE"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </service> </application></manifest>
0 0
- 用service实现音乐播放器
- Service实现音乐播放
- Android音乐播放器 -- MediaPlayer Service实现
- android-service音乐播放器
- Service (bindService音乐播放器)
- Android Service 音乐播放器
- Android学习笔记(七) 音乐播放器 Service实现
- Android应用开发--MP3音乐播放器Service实现
- Android应用开发--MP3音乐播放器Service实现
- Android应用开发--MP3音乐播放器Service实现
- notification+service+broadcastreceiver实现简单的音乐播放器
- android 四大组件service 音乐播放器的实现
- android音乐播放器实现(Service+BroadcastReceiver+Notification)
- Android之Service服务实现音乐播放器
- Android 使用Service实现简易音乐播放器
- 利用service实现音乐的后台播放
- android 利用 service 实现音乐播放
- Android通过Service实现音乐后台播放
- 原装win8系统电脑崩溃问题解决
- UVA_10935: Throwing cards away I
- UVa 11292.Dragon of Loowater【贪心】【10月6】
- 使用enca
- RESTFUL API 的规范
- 用service实现音乐播放器
- 获取当前时间
- 再谈工科研究生如何做研究 (第一部分:关于研究选题)
- C++ const关键字
- c# 事件学习
- 《机器学习》__note3
- 修改linux shell 提示符
- Python linux 下的python调试
- java反射的学习