溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

android實現(xiàn)背景音樂播放功能

發(fā)布時間:2020-09-28 03:29:33 來源:腳本之家 閱讀:241 作者:liu1164316159 欄目:移動開發(fā)

參考了一下網(wǎng)上別人寫的,再使用的時候是放在新開的線程中來播放音樂的,后來發(fā)現(xiàn)每次進入Activity后就會重復開始一個音樂播放的聲音。為了避免重復開啟播放功能我在原來代碼的基礎上增加了單例模式。這樣就避免了出現(xiàn)重復播放。

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; 
  } 
 
  // 初始化一些數(shù)據(jù) 
  private void initData() { 
    mLeftVolume = 0.5f; 
    mRightVolume = 0.5f; 
    mBackgroundMediaPlayer = null; 
    mIsPaused = false; 
    mCurrentPath = null; 
  } 
 
  /** 
   * 根據(jù)path路徑播放背景音樂 
   * 
   * @param path 
   *      :assets中的音頻路徑 
   * @param isLoop 
   *      :是否循環(huán)播放 
   */ 
  public void playBackgroundMusic(String path, boolean isLoop) { 
    if (mCurrentPath == null) { 
      // 這是第一次播放背景音樂--- it is the first time to play background music 
      // 或者是執(zhí)行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; 
    } 
  } 
 
  /** 
   * 繼續(xù)播放背景音樂 
   */ 
  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; 
  } 
 
  /** 
   * 結(jié)束背景音樂,并釋放資源 
   */ 
  public void end() { 
    if (mBackgroundMediaPlayer != null) { 
      mBackgroundMediaPlayer.release(); 
    } 
    // 重新“初始化數(shù)據(jù)” 
    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; 
  } 
} 

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI