Service实现音乐播放
来源:互联网 发布:仿链家房产网源码 编辑:程序博客网 时间:2024/05/23 15:47
package com.songjie.music_service.activity;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.util.Log;import android.view.View;import android.widget.Button;import com.songjie.music_service.R;/** * 界面处理 */public class Acty_Main extends ActionBarActivity implements View.OnClickListener { private Button play; private Button stop; private Button exit; private Button close; private Button pause; private Bundle bundle; private static final String TAG = "PlayMusic"; private void init_View() { play = (Button) findViewById(R.id.play); stop = (Button) findViewById(R.id.stop); pause = (Button) findViewById(R.id.pause); close = (Button) findViewById(R.id.close); exit = (Button)findViewById(R.id.exit); bundle = new Bundle(); play.setOnClickListener(this); stop.setOnClickListener(this); pause.setOnClickListener(this); close.setOnClickListener(this); exit.setOnClickListener(this); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.acty_main); init_View(); } @Override public void onClick(View v) { int op = -1; Intent intent = new Intent("org.allin.android.musicService"); switch (v.getId()) { case R.id.play: Log.i(TAG, "onClick:playing music"); op = 1; break; case R.id.stop: Log.i(TAG, "onClick:stoping music"); op = 2; break; case R.id.pause: Log.i(TAG, "onClick:pauseing music"); op = 3; break; case R.id.close: Log.i(TAG, "onClick:close"); this.finish(); break; case R.id.exit: Log.i(TAG, "onClick:exit"); op = 4; this.finish(); stopService(intent); break; } bundle = new Bundle(); bundle.putInt("op", op); intent.putExtras(bundle); startService(intent); }}
package com.songjie.music_service.service;import android.app.Service;import android.content.Intent;import android.media.MediaPlayer;import android.os.Bundle;import android.os.IBinder;import android.util.Log;import com.songjie.music_service.R;import java.io.IOException;/** * 服务的实现 */public class Serv_Music extends Service { private Bundle bundle; private MediaPlayer media_player; private static final String TAG = "Serv_Music"; public Serv_Music() { } @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { Log.v(TAG, "onCreate"); if (media_player == null) { media_player = MediaPlayer.create(this, R.raw.tmp); media_player.setLooping(false); } super.onCreate(); } @Override public void onDestroy() { Log.v(TAG, "onDestroy"); if (media_player != null) { media_player.stop(); media_player.release(); } } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.v(TAG, "onStartCommand"); if (intent != null) { bundle = intent.getExtras(); if (bundle != null) { int op = bundle.getInt("op"); switch (op) { case 1: play(); break; case 2: stop(); break; case 3: pause(); break; } } } return super.onStartCommand(intent, flags, startId); } private void pause() { if (media_player != null && media_player.isPlaying()) { media_player.pause(); } } private void play() { if (!media_player.isPlaying()) { media_player.start(); } } public void stop() { if (media_player != null) { media_player.stop(); try { media_player.prepare(); } catch (IOException e) { e.printStackTrace(); } } }}
<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" tools:context=".Acty_Main"> <Button android:id="@+id/play" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/play"/> <Button android:id="@+id/stop" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/stop"/> <Button android:id="@+id/pause" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/pause"/> <Button android:id="@+id/close" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/close"/> <Button android:id="@+id/exit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/exit"/></LinearLayout>
0 0
- Service实现音乐播放
- 利用service实现音乐的后台播放
- android 利用 service 实现音乐播放
- Android通过Service实现音乐后台播放
- Android音乐播放器 -- MediaPlayer Service实现
- 用service实现音乐播放器
- 4.1.2.1 Service 实现音乐播放
- 利用service实现音乐的后台播放
- 利用ContentProvider和service实现音乐播放
- Android实现后台播放音乐(Service方式)
- 使用Service播放音乐
- 使用service播放音乐
- android 音乐播放service
- Android Service播放音乐
- service 音乐播放
- Service后台播放音乐
- Service播放音乐实例
- Service + 音乐播放
- Tomcat用到的设计模式
- ZOJ 2674-Strange Limit(指数循环节)
- oracle游标应用难点 sys_refcursor 和 cursor(转)
- Cortex M3处理器工作模式及中断过程
- Java开发web的几种开发模式
- Service实现音乐播放
- 获得所有已安装程序的信息
- LintCode 数组剔除元素后的乘积
- 弱校联盟国庆第一天 I题
- 夕拾-数据结构-直接插入&希尔排序
- Valid Sudoku
- 用KVO监听实现collectionView滚动一致,完整项目movie
- C#学习日记08---数据类型 之 字符类型
- 红黑的插入java版本