最新文章:

首页 android

service实现启动,停止,暂停/继续播放音乐

发布时间:2016年05月10日 评论数:抢沙发 阅读数:388

    1.创建界面(通过onClick实现侦听)

    <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="18dp"
            android:onClick="onClick"
            android:text="播放" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button1"
            android:layout_alignRight="@+id/button1"
            android:layout_below="@+id/button1"
            android:layout_marginTop="20dp"        
            android:onClick="onClick"
            android:text="停止" />
    
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button2"
            android:layout_alignRight="@+id/button2"
            android:layout_below="@+id/button2"
            android:layout_marginTop="24dp"
            android:onClick="onClick"
            android:text="暂停/继续" />

    2.MainActivity

    package com.example.firstapp;
    
    import com.example.firstapp.MusicService.LocalBinder;
    
    import android.app.Activity;
    import android.app.Service;
    import android.content.ComponentName;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.IBinder;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    	MusicService muservice = null;
    	
    	static TextView tv = null;
    	static Handler handler = new Handler();
    	static String mstr = null;
    	public static void updateUI(String str){//更新到TextView
    		mstr = str;
    		handler.post(new Runnable() {
    			@Override
    			public void run() {
    				tv.setText(mstr);
    			}
    		});
    	}
    	
    	ServiceConnection conn = new ServiceConnection() {
    		@Override
    		public void onServiceDisconnected(ComponentName name) {			
    		}
    		@Override
    		public void onServiceConnected(ComponentName name, IBinder service) {
    			muservice = ((LocalBinder)service).getService();
    		}
    	};
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		tv =(TextView) findViewById(R.id.textView1);
    	}
    	public void onClick(View v){
    		switch(v.getId()){
    		case R.id.button1:
    			Intent intent = new Intent(MainActivity.this,MusicService.class);
    			bindService(intent, conn, BIND_AUTO_CREATE);
    			break;
    		case R.id.button2:
    			unbindService(conn);
    			break;
    		case R.id.button3:
    			muservice.PauseOrGo();
    			break;
    		default:
    			break;
    		}
    	}
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
    	@Override
    	public boolean onOptionsItemSelected(MenuItem item) {
    		// Handle action bar item clicks here. The action bar will
    		// automatically handle clicks on the Home/Up button, so long
    		// as you specify a parent activity in AndroidManifest.xml.
    		int id = item.getItemId();
    		if (id == R.id.action_settings) {
    			return true;
    		}
    		return super.onOptionsItemSelected(item);
    	}
    }
    

    3.MusicService(记得注册)

    package com.example.firstapp;
    
    import android.app.Service;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.os.Binder;
    import android.os.IBinder;
    import android.provider.MediaStore.Audio.Media;
    import android.text.style.UpdateAppearance;
    import android.widget.TextView;
    
    public class MusicService extends Service {
    	MediaPlayer player = null;
    	class LocalBinder extends Binder{
    		public MusicService getService(){
    			return MusicService.this;
    		}
    	}
    	@Override
    	public IBinder onBind(Intent intent) {
    		player.start();
    		MainActivity.updateUI("音乐开始播放");
    		return new LocalBinder();
    	}
    	@Override
    	public void onCreate() {
    		player = MediaPlayer.create(this, R.raw.song);
    		super.onCreate();
    	}
    	@Override
    	public void onDestroy() {
    		super.onDestroy();
    	}
    	@Override
    	public boolean onUnbind(Intent intent) {
    		player.stop();
    		MainActivity.updateUI("音乐停止播放");
    		return super.onUnbind(intent);
    	}
    	public void PauseOrGo(){
    		if(player.isPlaying()){
    			player.pause();
    			MainActivity.updateUI("音乐暂停播放");
    		}else{
    			player.start();
    			MainActivity.updateUI("音乐继续播放");
    		}
    	}
    }
    

二维码加载中...
本文作者:HDC      文章标题: service实现启动,停止,暂停/继续播放音乐
本文地址:http://hdcin.cn/?post=55
版权声明:若无注明,本文皆为“小胖Blog's”原创,转载请保留文章出处。
挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
提交评论

清空信息
关闭评论