背景音乐

来源:互联网 发布:矩阵互动 开盘造假 编辑:程序博客网 时间:2024/04/26 18:28

通过多线程实现背景音乐的开始和结束

一、主要内容

主要部分包括MainActivity.java 、AudioService.java和activity_main.xml文件。

二、MainActivity.java代码

  1. package com.example.service;  
  2.   
  3. import android.media.MediaPlayer;  
  4. import android.os.Bundle;  
  5. import android.os.IBinder;  
  6. import android.app.Activity;  
  7. import android.content.Intent;  
  8. import android.view.Menu;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.ImageButton;  
  12.   
  13. public class MainActivity extends Activity {  
  14.     ImageButton btn1= null;  
  15.     ImageButton btn2= null;  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.         getViews();  
  22.         // 开始音乐按钮  
  23.         btn1.setOnClickListener(new OnClickListener() {  
  24.             @Override  
  25.             public void onClick(View v) {  
  26.                 Intent intent = new Intent(MainActivity.this,  
  27.                         AudioService.class);  
  28.                 startService(intent);  
  29.             }  
  30.         });  
  31.         // 停止音乐按钮  
  32.         btn2.setOnClickListener(new OnClickListener() {  
  33.   
  34.             @Override  
  35.             public void onClick(View v) {  
  36.                 // TODO Auto-generated method stub  
  37.                 Intent intent = new Intent(MainActivity.this,  
  38.                         AudioService.class);  
  39.                 stopService(intent);  
  40.             }  
  41.         });  
  42.     }  
  43.   
  44.     /* 
  45.      * 获取控件 
  46.      */  
  47.     private void getViews() {  
  48.         btn1= (ImageButton) findViewById(R.id.btn1);  
  49.         btn2= (ImageButton) findViewById(R.id.btn2);  
  50.     }  
  51.   
  52.     /* 
  53.      * 自动播放 
  54.      */  
  55.     // @Override  
  56.     // protected void onResume() {  
  57.     // super.onResume();  
  58.     // startService(new Intent(this,AudioService.class));  
  59.     // }  
  60.     @Override  
  61.     public boolean onCreateOptionsMenu(Menu menu) {  
  62.         // Inflate the menu; this adds items to the action bar if it is present.  
  63.         getMenuInflater().inflate(R.menu.main, menu);  
  64.         return true;  
  65.     }    
  66. }  

三、AudioService.java

  1. package com.example.service;  
  2.   
  3. /** 
  4.  * 多线程实现后台播放背景音乐的service 
  5.  */  
  6. import android.app.Service;  
  7. import android.content.Intent;  
  8. import android.media.MediaPlayer;  
  9. import android.os.Binder;  
  10. import android.os.IBinder;  
  11.   
  12. public class AudioService extends Service implements  
  13.         MediaPlayer.OnCompletionListener {  
  14.     // 实例化MediaPlayer对象  
  15.     MediaPlayer player;  
  16.     private final IBinder binder = new AudioBinder();  
  17.   
  18.     @Override  
  19.     public IBinder onBind(Intent intent) {  
  20.         return binder;  
  21.     }  
  22.   
  23.     public void onCreate() {  
  24.         super.onCreate();  
  25.         // 从raw文件夹中获取一个应用自带的mp3文件  
  26.         player = MediaPlayer.create(this, R.raw.qq);  
  27.         player.setOnCompletionListener(this);  
  28.         player.setLooping(true);  
  29.     }  
  30.   
  31.     @Override  
  32.     public int onStartCommand(Intent intent, int flags, int startId) {  
  33.         super.onStartCommand(intent, flags, startId);  
  34.         if (!player.isPlaying()) {  
  35.             new MusicPlayThread().start();  
  36.         } else  
  37.             player.isPlaying();  
  38.         return START_STICKY;  
  39.     }  
  40.   
  41.     /** 
  42.      * 当Audio播放完的时候触发该动作 
  43.      */  
  44.     public void onCompletion(MediaPlayer mp) {  
  45.         stopSelf();// 结束了,则结束Service  
  46.   
  47.     }  
  48.   
  49.     public void onDestroy() {  
  50.         super.onDestroy();  
  51.         if (player.isPlaying()) {  
  52.             player.stop();  
  53.         }  
  54.         player.release();  
  55.     }  
  56.   
  57.     // 为了和Activity交互,我们需要定义一个Binder对象  
  58.     public class AudioBinder extends Binder {  
  59.         // 返回Service对象  
  60.         public AudioService getService() {  
  61.             return AudioService.this;  
  62.         }  
  63.     }  
  64.   
  65.     private class MusicPlayThread extends Thread {  
  66.         public void run() {  
  67.             if (!player.isPlaying()) {  
  68.                 player.start();  
  69.             }  
  70.         }  
  71.     }  
  72.   

四、activity_main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/LinearLayout2"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="horizontal"  
  7.     android:paddingBottom="@dimen/activity_vertical_margin"  
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  9.     android:paddingRight="@dimen/activity_horizontal_margin"  
  10.     android:paddingTop="@dimen/activity_vertical_margin"  
  11.     tools:context=".MainActivity" >  
  12.   
  13.     <ImageButton  
  14.         android:id="@+id/btn1"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_gravity="center"  
  18.         android:src="@android:drawable/ic_media_play" />  
  19.   
  20.     <ImageButton  
  21.         android:id="@+id/btn2"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:layout_gravity="center"  
  25.         android:src="@android:drawable/ic_media_pause" />  
  26.   
  27. </LinearLayout> 





0 0
原创粉丝点击