Android开发 MediaPlayer播放raw资源MP3文件

时间:2021-10-26 06:28:33

代码

  

private MediaPlayer mRingPlayer;
    /**
     * 播放铃声
     */
    private void startRing(){
        if (mRingPlayer != null){
            mRingPlayer.stop();
            mRingPlayer.release();
            mRingPlayer = null;
        }
        mRingPlayer = MediaPlayer.create(this, R.raw.video_request);
        mRingPlayer.setLooping(true);
        mRingPlayer.start();

    }

    /**
     * 停止铃声
     */
    private void stopRing(){
        if (mRingPlayer != null){
            mRingPlayer.stop();
            mRingPlayer.release();
            mRingPlayer = null;
        }
    }