背景音乐

来源:互联网 发布:centos挂载磁盘 编辑:程序博客网 时间:2024/05/17 07:39

mainActivity:

package com.example.service;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;

public 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;
    }
   
}

AudioService:

package com.example.service;

/**
 * 多线程实现后台播放背景音乐的service
 */
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;

public 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();
   }
  }
 }
  
}

activity_main.xml:

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:background="@drawable/bk"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageButton
        android:id="@+id/StartBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="66dp"
        android:onClick="start"
        android:src="@drawable/start" />

    <ImageButton
        android:id="@+id/StopBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/StartBtn"
        android:layout_alignTop="@+id/StartBtn"
        android:layout_marginLeft="44dp"
        android:layout_toRightOf="@+id/StartBtn"
        android:onClick="stop"
        android:src="@drawable/stop" />

</RelativeLayout>

 

0 0
原创粉丝点击