安卓 实现背景音乐的播放与关闭

来源:互联网 发布:24小时学通qt编程 pdf 编辑:程序博客网 时间:2024/05/17 15:20

运用Services来实现
概述
Android中的服务和windows中的服务是类似的东西,服务一般没有用户操作界面,它运行于
系统中不容易被用户发觉,可以使用它开发如监控之类的程序。
Service在后台运行,在默认情况下,Service运行在应用程序进程的主线程中,如果需要在Service中处理一些比较耗时的操作,那么应该将这些任务放在单独的线程中处理,避免阻塞用户界面。

设计思路

  1. 在页面中添加一个播放音乐和停止音乐的按钮。
  2. 编写多线程实现后台播放背景音乐的service。
  3. 在MainActivity中添加开始播放和停止的方法。
  4. 在AndroidManifest.xml文件中的节点里对服务进行配置:
    <service android:name=" SMSService "></service>

注意问题

  1. 要注意两个按钮的查找。
  2. 在对应的方法中添加监视器,调用service的开始和结束方法。
  3. 为了实现交互,需要定义一个Binder对象。
  4. 正确的配置service属性。

主界面

主要代码:AudioServicepublic class AudioService extends Service implements        MediaPlayer.OnCompletionListener {    // 实例化MediaPlayer对象    MediaPlayer player;    private final IBinder binder = new AudioBinder();    @Override    public IBinder onBind(Intent intent) {        return binder;    }    public void onCreate() {        super.onCreate();        // 从raw文件夹中获取一个应用自带的mp3文件        player = MediaPlayer.create(this, R.raw.qq);        player.setOnCompletionListener(this);        player.setLooping(true);    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        super.onStartCommand(intent, flags, startId);        if (!player.isPlaying()) {            new MusicPlayThread().start();        }        else player.isPlaying();        return START_STICKY;    }    /**     * 当Audio播放完的时候触发该动作     */    public void onCompletion(MediaPlayer mp) {        stopSelf();// 结束了,则结束Service    }    public void onDestroy() {        super.onDestroy();        if (player.isPlaying()) {            player.stop();        }        player.release();    }    // 为了和Activity交互,我们需要定义一个Binder对象    public class AudioBinder extends Binder {        // 返回Service对象        public AudioService getService() {            return AudioService.this;        }    }    private class MusicPlayThread extends Thread {        public void run() {            if (!player.isPlaying()) {                player.start();            }        }    }}MainActivitypublic class MainActivity extends Activity {    private ImageButton StartBtn=null;    private ImageButton StopBtn=null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        StartBtn =(ImageButton)findViewById(R.id.StartBtn);        StopBtn =(ImageButton)findViewById(R.id.StopBtn);    }    //开始播放音乐    public void start(View v){        Intent intent = new Intent(MainActivity.this,AudioService.class);        startService(intent);    }    //停止音乐    public void stop(View v){        Intent intent = new Intent(MainActivity.this,AudioService.class);        stopService(intent);    }   /* @Override    protected void onResume() {    super.onResume();    startService(new Intent(this,AudioService.class));    }    @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;    }}

详细代码

0 0