现在的位置: 首页 > 综合 > 正文

Service播放MP3

2013年01月18日 ⁄ 综合 ⁄ 共 3089字 ⁄ 字号 评论关闭

TestActivity.java

public class TestActivity extends Activity {


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		Button startServiceButton=(Button)findViewById(R.id.startService);
		startServiceButton.setOnClickListener(new StartServiceListener());
		
		Button stopServiceButton=(Button)findViewById(R.id.stopService);
		stopServiceButton.setOnClickListener(new StopServiceListener());
		
		Button startMusicButton=(Button)findViewById(R.id.start);
		startMusicButton.setOnClickListener(new StartMusicListener());
		
		Button stopMusicButton=(Button)findViewById(R.id.stop);
		stopMusicButton.setOnClickListener(new StopMusicListener());
	}
	
	class StartServiceListener implements  OnClickListener{


		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent=new Intent();
			intent.setClass(TestActivity.this, FirstService.class);
			startService(intent);
		}}
	class StopServiceListener implements OnClickListener{


		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = new Intent();
			intent.setClass(TestActivity.this, FirstService.class);
			stopService(intent);
		}}
	class StartMusicListener implements OnClickListener{


		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			startService(new Intent(TestActivity.this, FirstService.class));  
			//启动服务
		}}
	class StopMusicListener implements OnClickListener{


		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			stopService(new Intent(TestActivity.this,FirstService.class));
			//停止服务
		}
		
	}
}

FirstService.java

public class FirstService extends Service {
	private MediaPlayer mp;
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("Service onBind");
		return null;
	}
    /** Called when the activity is first created. */
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		System.out.println("Service onCreate");
		super.onCreate();
		mp=MediaPlayer.create(this,Uri.parse("/sdcard/More Than I Can Say.mp3"));

	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		System.out.println("Service onDestroy");;
		super.onDestroy();
		mp.stop();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		mp.start();
		System.out.println("intent="+intent);
		System.out.println("flags="+flags);
		System.out.println("startId="+startId);
		System.out.println("Service onStartCommand");
		return START_NOT_STICKY;
	}
    
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button
	android:id="@+id/startService"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:text="@string/startService"
    />
<Button
	android:id="@+id/stopService"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/stopService"
	/>
<Button
	android:id="@+id/start"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="开始"
	/>
<Button
	android:id="@+id/stop"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="停止"
	/>
	
</LinearLayout>

截图:

抱歉!评论已关闭.