android-----简单的音乐播放器

<RelativeLayout 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:gravity="center" >
    
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical" >
        
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/pin"
            />
        
        <ProgressBar
            android:id="@+id/mProgressBar"
            android:layout_marginTop="5dp"
            android:layout_width="250dp"
            android:layout_height="10dp"
            android:max="100"
            style="@android:style/Widget.ProgressBar.Horizontal"
            />
    
    	<LinearLayout
        	android:layout_height="wrap_content"
        	android:layout_width="match_parent"
        	android:gravity="center_horizontal" >
        	<Button
        	    android:id="@+id/play"
            	android:layout_width="wrap_content"
            	android:layout_height="wrap_content"
            	android:text="播  放"/>
        	<Button
        	    android:id="@+id/pause"
            	android:layout_width="wrap_content"
            	android:layout_height="wrap_content"
            	android:text="暂  停"/>
        	
        	<Button
        	    android:id="@+id/exit"
            	android:layout_width="wrap_content"
            	android:layout_height="wrap_content"
            	android:text="退  出"/>
    	</LinearLayout>
    	
    </LinearLayout>

</RelativeLayout>

package com.example.mp3player;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends Activity implements OnClickListener{
	ActionBar mActionBar;
	Button pause,play,exit;
	ProgressBar mProgressBar;
	static Handler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mActionBar=getActionBar();
		mActionBar.hide();
		pause=(Button) findViewById(R.id.pause);
		play=(Button) findViewById(R.id.play);
		exit=(Button) findViewById(R.id.exit);
		mProgressBar=(ProgressBar) findViewById(R.id.mProgressBar);
		
		pause.setOnClickListener(this);
		play.setOnClickListener(this);
		exit.setOnClickListener(this);
		handler=new Handler(){
			@Override
			public void handleMessage(Message msg) {
				Bundle mBundle=new Bundle();
				mBundle=msg.getData();
				mProgressBar.setProgress(mBundle.getInt("progress",0));
		    }
		};
		
	}

	@Override
	public void onClick(View v) {
		int p=0;
		Intent intent=new Intent("com.example.mp3player.musicService");
		
		if(v==play)
			p=1;
		else if(v==pause)
			p=2;
		else if(v==exit)
		{
			stopService(intent);
			this.finish();
		}
		
		Bundle bundle=new Bundle();
		bundle.putInt("op", p);
		intent.putExtras(bundle);
		
		startService(intent);
	}

}

package com.example.mp3player;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;

public class MusicService extends Service {
	private MediaPlayer mediaPlayer=null;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	@Override
	public void onCreate(){
		if(mediaPlayer==null)
		{
			mediaPlayer=MediaPlayer.create(this, R.raw.pin);
			mediaPlayer.setLooping(true);
		}
		
	}
	
	
	@Override
	public void onDestroy(){
		if(mediaPlayer!=null)
		{
			mediaPlayer.pause();
			mediaPlayer.stop();
			mediaPlayer.release();
		}
	}
	
	@Override
	public void onStart(Intent intent,int id){
		if(intent==null) return ;
		int p=intent.getIntExtra("op", 1);
		
		switch(p)
		{
			case 1:
				play();break;
			case 2:
				pause();break;
		}
	}
	
	public void play(){
		if(!mediaPlayer.isPlaying())
		{
			mediaPlayer.start();
			
			Thread thread=new Thread(new Runnable(){
				@Override
				public void run() {
					while(mediaPlayer!=null&&mediaPlayer.isPlaying())
					{
						Bundle bundle=new Bundle();
						bundle.putInt("progress", mediaPlayer.getCurrentPosition()*100/mediaPlayer.getDuration());
						Message message=new Message();
						message.setData(bundle);
						MainActivity.handler.sendMessage(message);
					}
				}});
			thread.start();
		}
			

	}
	
	public void pause(){
		if(mediaPlayer.isPlaying())
		{
			mediaPlayer.pause();
		}
			

	}
	
	

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值