AndroidでBGM再生機能を実現


インターネット上で他の人が書いているのを参考にして、新しく開いたスレッドの中に置いて音楽を流していましたが、Activityに入るたびに同じ音楽の再生音を繰り返すことに気づきました。再生機能の重複を避けるために、従来のコードに加えてワンパターンを追加しました。このように繰り返し放送が発生することを避けました。

package com.liu.zhen.utils; 
 
import android.content.Context; 
import android.content.res.AssetFileDescriptor; 
import android.media.MediaPlayer; 
import android.util.Log; 
 
/** 
 * 
 * This class is used for controlling background music 
 * 
 */ 
public class BackgroundMusic { 
  private static BackgroundMusic backgroundMusic = null; 
  private static final String TAG = "Bg_Music"; 
  private float mLeftVolume; 
  private float mRightVolume; 
  private Context mContext; 
  private MediaPlayer mBackgroundMediaPlayer; 
  private boolean mIsPaused; 
  private String mCurrentPath; 
 
  private BackgroundMusic(Context context) { 
    this.mContext = context; 
    initData(); 
  } 
 
  public static BackgroundMusic getInstance(Context context) { 
    if (backgroundMusic == null) { 
      backgroundMusic = new BackgroundMusic(context); 
    } 
    return backgroundMusic; 
  } 
 
  //         
  private void initData() { 
    mLeftVolume = 0.5f; 
    mRightVolume = 0.5f; 
    mBackgroundMediaPlayer = null; 
    mIsPaused = false; 
    mCurrentPath = null; 
  } 
 
  /** 
   *   path         
   * 
   * @param path 
   *      :assets       
   * @param isLoop 
   *      :       
   */ 
  public void playBackgroundMusic(String path, boolean isLoop) { 
    if (mCurrentPath == null) { 
      //            --- it is the first time to play background music 
      //      end()   ,    ---or end() was called 
      mBackgroundMediaPlayer = createMediaplayerFromAssets(path); 
      mCurrentPath = path; 
    } else { 
      if (!mCurrentPath.equals(path)) { 
        //           --- play new background music 
        //              ----release old resource and create a new one 
        if (mBackgroundMediaPlayer != null) { 
          mBackgroundMediaPlayer.release(); 
        } 
        mBackgroundMediaPlayer = createMediaplayerFromAssets(path); 
        //       ---record the path 
        mCurrentPath = path; 
      } 
    } 
 
    if (mBackgroundMediaPlayer == null) { 
      Log.e(TAG, "playBackgroundMusic: background media player is null"); 
    } else { 
      //              ,   ---if the music is playing or paused, stop it 
      mBackgroundMediaPlayer.stop(); 
      mBackgroundMediaPlayer.setLooping(isLoop); 
      try { 
        mBackgroundMediaPlayer.prepare(); 
        mBackgroundMediaPlayer.seekTo(0); 
        mBackgroundMediaPlayer.start(); 
        this.mIsPaused = false; 
      } catch (Exception e) { 
        Log.e(TAG, "playBackgroundMusic: error state"); 
      } 
    } 
  } 
 
  /** 
   *          
   */ 
  public void stopBackgroundMusic() { 
    if (mBackgroundMediaPlayer != null) { 
      mBackgroundMediaPlayer.stop(); 
      // should set the state, if not , the following sequence will be 
      // error 
      // play -> pause -> stop -> resume 
      this.mIsPaused = false; 
    } 
  } 
 
  /** 
   *          
   */ 
  public void pauseBackgroundMusic() { 
    if (mBackgroundMediaPlayer != null 
        && mBackgroundMediaPlayer.isPlaying()) { 
      mBackgroundMediaPlayer.pause(); 
      this.mIsPaused = true; 
    } 
  } 
 
  /** 
   *          
   */ 
  public void resumeBackgroundMusic() { 
    if (mBackgroundMediaPlayer != null && this.mIsPaused) { 
      mBackgroundMediaPlayer.start(); 
      this.mIsPaused = false; 
    } 
  } 
 
  /** 
   *          
   */ 
  public void rewindBackgroundMusic() { 
    if (mBackgroundMediaPlayer != null) { 
      mBackgroundMediaPlayer.stop(); 
      try { 
        mBackgroundMediaPlayer.prepare(); 
        mBackgroundMediaPlayer.seekTo(0); 
        mBackgroundMediaPlayer.start(); 
        this.mIsPaused = false; 
      } catch (Exception e) { 
        Log.e(TAG, "rewindBackgroundMusic: error state"); 
      } 
    } 
  } 
 
  /** 
   *              
   * 
   * @return:   boolean          
   */ 
  public boolean isBackgroundMusicPlaying() { 
    boolean ret = false; 
    if (mBackgroundMediaPlayer == null) { 
      ret = false; 
    } else { 
      ret = mBackgroundMediaPlayer.isPlaying(); 
    } 
    return ret; 
  } 
 
  /** 
   *       ,      
   */ 
  public void end() { 
    if (mBackgroundMediaPlayer != null) { 
      mBackgroundMediaPlayer.release(); 
    } 
    //   “     ” 
    initData(); 
  } 
 
  /** 
   *        “  ” 
   * 
   * @return 
   */ 
  public float getBackgroundVolume() { 
    if (this.mBackgroundMediaPlayer != null) { 
      return (this.mLeftVolume + this.mRightVolume) / 2; 
    } else { 
      return 0.0f; 
    } 
  } 
 
  /** 
   *           
   * 
   * @param volume 
   *      :       ,float   
   */ 
  public void setBackgroundVolume(float volume) { 
    this.mLeftVolume = this.mRightVolume = volume; 
    if (this.mBackgroundMediaPlayer != null) { 
      this.mBackgroundMediaPlayer.setVolume(this.mLeftVolume, 
          this.mRightVolume); 
    } 
  } 
 
  /** 
   * create mediaplayer for music 
   * 
   * @param path 
   *      the path relative to assets 
   * @return 
   */ 
  private MediaPlayer createMediaplayerFromAssets(String path) { 
    MediaPlayer mediaPlayer = null; 
    try { 
      AssetFileDescriptor assetFileDescritor = mContext.getAssets() 
          .openFd(path); 
      mediaPlayer = new MediaPlayer(); 
      mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(), 
          assetFileDescritor.getStartOffset(), 
          assetFileDescritor.getLength()); 
      mediaPlayer.prepare(); 
      mediaPlayer.setVolume(mLeftVolume, mRightVolume); 
    } catch (Exception e) { 
      mediaPlayer = null; 
      Log.e(TAG, "error: " + e.getMessage(), e); 
    } 
    return mediaPlayer; 
  } 
} 
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。