android 之 service+contentProvider 音乐播放示例

原创 2012年03月28日 00:23:15
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ethan.service"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ServiceDemo2Activity"
            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=".MusicService" android:exported="true" android:enabled="true"/>  
    </application>

</manifest>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView    
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
        android:text="service............."  
        />  
    <LinearLayout  
        android:orientation="horizontal"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
    >  
        <Button  
            android:id="@+id/previous"  
            android:layout_height="fill_parent"  
            android:layout_width="wrap_content"  
            android:layout_weight="1"  
            android:text="上一首"  
        />  
        <Button  
            android:id="@+id/play"  
            android:layout_height="fill_parent"  
            android:layout_width="wrap_content"  
            android:layout_weight="1"  
            android:text="播放"  
        />  
        <Button  
            android:id="@+id/next"  
            android:layout_height="fill_parent"  
            android:layout_width="wrap_content"  
            android:layout_weight="1"  
            android:text="下一首"  
        />  
        <Button  
            android:id="@+id/pause"  
            android:layout_height="fill_parent"  
            android:layout_width="wrap_content"  
            android:layout_weight="1"  
            android:text="暂停"  
        />  
    </LinearLayout>  

</LinearLayout>

package com.ethan.service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.IBinder;
import android.provider.MediaStore;
import android.widget.Toast;

public class MusicService extends Service {

	String[] cursorCols = new String[] {
		"audio._id AS _id",
		MediaStore.Audio.Media.ARTIST,//艺术家
		MediaStore.Audio.Media.ALBUM,
		MediaStore.Audio.Media.TITLE,
		MediaStore.Audio.Media.DATA,//歌曲文件的路径
		MediaStore.Audio.Media.MIME_TYPE,
		MediaStore.Audio.Media.ALBUM_ID,
		MediaStore.Audio.Media.ARTIST_ID,
		MediaStore.Audio.Media.DURATION
	};
	
	private MediaPlayer player;
	private Cursor cursor;
	private int playPosition = 0;
	
	boolean pause = false;
	
	public static final String PLAY_ACTION = "com.tutor.music.PLAY_ACTION";  
    public static final String PAUSE_ACTION = "com.tutor.music.PAUSE_ACTION";  
    public static final String NEXT_ACTION = "com.tutor.music.NEXT_ACTION";  
    public static final String PREVIOUS_ACTION = "com.tutor.music.PREVIOUS_ACTION";  
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	@Override
	public void onCreate() {
		player = new MediaPlayer();
		 //通过一个URI可以获取所有音频文件 
		Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
		//这里我过滤了一下,有些音频文件是游戏音频,很短  
        //播放不到一秒钟,我这里作了处理,默认大于10秒的可以看作是歌  
		cursor = getContentResolver().query(uri, cursorCols, "duration > 10000", null, null); 
		super.onCreate();
	}
	@Override
	public void onDestroy() {
		player.release();
		super.onDestroy();
	}
	@Override
	public void onStart(Intent intent, int startId) {
		String action = intent.getAction();
		if(action.equals(PLAY_ACTION)){  
            play();  
        }else if(action.equals(PAUSE_ACTION)){  
            pause();  
            pause = true;
        }else if(action.equals(NEXT_ACTION)){  
            next();  
        }else if(action.equals(PREVIOUS_ACTION)){  
            previous();  
        }  
		super.onStart(intent, startId);
	}
	private void previous() {
		if(playPosition==0) {
			playPosition = cursor.getCount()-1;
		} else {
			playPosition--;
		}
		init();
	}
	private void next() {
		if (playPosition == cursor.getCount() - 1) {  
			playPosition = 0;  
        } else {  
        	playPosition++;  
        }  
		init();
	}
	private void pause() {
	//	stopSelf(); //结束service
		player.pause();
		pause = true;
	}
	private void play() {
		//暂停之后回来继续播放
		if(pause) {
			player.start();//不会接着播放
			pause = false;
		}
		init();
	}
	private void init() {
		player.reset();
		
		String dataSource = getDatalByPosition(cursor,playPosition);
		String info = getInfoByPosition(cursor,playPosition);
		
		try {
			Toast.makeText(getApplicationContext(), new String(info.getBytes(),"UTF-8"), Toast.LENGTH_SHORT).show();
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		
		try {
			player.setDataSource(dataSource);
			player.prepare();
			player.start();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private String getInfoByPosition(Cursor c, int playPosition2) {
		c.moveToPosition(playPosition2);
		int titleColumn = c.getColumnIndex(MediaStore.Audio.Media.TITLE);
		int artistColumn = c.getColumnIndex(MediaStore.Audio.Media.ARTIST);
		String info = c.getString(artistColumn)+" "+ c.getString(titleColumn);
		return info;
	}
	//获取路径
	private String getDatalByPosition(Cursor c, int playPosition2) {
		c.moveToPosition(playPosition2);
		//歌曲路径
		int dataColumn = c.getColumnIndex(MediaStore.Audio.Media.DATA);
		String data = c.getString(dataColumn);
		return data;
	}

}

package com.ethan.service;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ServiceDemo2Activity extends Activity implements OnClickListener {
	private Button mPrevious, mPlay, mNext, mPause;
	private ComponentName component;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		setupViews();
	}

	private void setupViews() {
		component = new ComponentName(this, MusicService.class);

		mPrevious = (Button) findViewById(R.id.previous);
		mPlay = (Button) findViewById(R.id.play);
		mNext = (Button) findViewById(R.id.next);
		mPause = (Button) findViewById(R.id.pause);
		
		
		mPrevious.setOnClickListener(this);  
        mPlay.setOnClickListener(this);  
        mNext.setOnClickListener(this);  
        mPause.setOnClickListener(this);  
	}

	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch(v.getId()) {
		case R.id.previous:
			intent = new Intent(MusicService.PREVIOUS_ACTION);
			intent.setComponent(component);
			startService(intent);
			break;
		case R.id.play:
			intent = new Intent(MusicService.PLAY_ACTION);
			intent.setComponent(component);
			startService(intent);
			break;
		case R.id.pause:
			intent = new Intent(MusicService.PAUSE_ACTION);
			intent.setComponent(component);
			startService(intent);
			break;
		case R.id.next:
			intent = new Intent(MusicService.NEXT_ACTION);
			intent.setComponent(component);
			startService(intent);
			break;
		}
	}
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Android中利用ContentResolver获取本地音乐和相片

在Android中,例如图库和音乐播放器,它们能够扫描我们的系统,获取到本地的相片跟音乐,这一点是如何做到的呢? 当我们往手机上放图片或者音乐的时候,会在手机内存中某个位置上的某个database中存...

Android应用实例之----基于Service与ContentProvider的音乐播放实例!

今天与大家分享基于Service与ContentProvider的音乐播放实例,对于接触Android有一些时日的人来说,Android的核心也就是Activity,Service,ContentPr...

Android应用实例之----基于Service与ContentProvider的音乐播放实例!

今天与大家分享基于Service与ContentProvider的音乐播放实例,对于接触Android有一些时日的人来说,Android的核心也就是Activity,Service,ContentPr...

基于Service与ContentProvider的音乐播放实例!

今天我将为大家分享基于Service与ContentProvider的音乐播放实例,对于接触Android有一些时日的人来 说,Android的核心也就是Activity,Service,Conten...

基于Service与ContentProvider的音乐播放实例

来自:http://blog.csdn.net/Android_Tutor/article/details/5743183 大家好今天我将为大家分享基于Service与ContentProvid...

Android使用Service播放音乐

  • 2017-08-11 15:21
  • 208KB
  • 下载

android_56_Service_音乐播放demo

效果: 清单: <uses-sdk

Android多媒体学习六:利用Service实现背景音乐的播放

Android允许我们使用Service组件来完成后台任务,这些任务的允许不会影响到用户其他的交互。 1、Activity类package demo.camera; import android.a...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)