这篇博客只是一个Surface+MediaPlayer简单使用方法的说明。至于其中自定义界面,功能增加之类的可以查看之前的博客:
https://blog.csdn.net/lpCrazyBoy/article/details/79842906
可以直接new或者调用create方法创建:
MediaPlayer mp = new MediaPlayer();
MediaPlayer mp = MediaPlayer.create(this, R.raw.test); //无需再调用setDataSource
另外create还有这样的形式: create(Context context, Uri uri, SurfaceHolder holder) 通过Uri和指定 SurfaceHolder 【抽象类】 创建一个多媒体播放器
//①raw下的资源:
MediaPlayer.create(this, R.raw.test);
//②本地文件路径:
mp.setDataSource("/sdcard/test.mp3");
//③网络URL文件:
mp.setDataSource("http://www.xxx.com/music/test.mp3");
另外setDataSource()方法有多个,里面有这样一个类型的参数:FileDescriptor,在使用这个 API的时候,需要把文件放到res文件夹平级的assets文件夹里,然后使用下述代码设置DataSource:
AssetFileDescriptor fileDescriptor = getAssets().openFd("rain.mp3");
m_mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),fileDescriptor.getStartOffset(), fileDescriptor.getLength());
- getCurrentPosition( ):得到当前的播放位置
- getDuration() :得到文件的时间
- getVideoHeight() :得到视频高度
- getVideoWidth() :得到视频宽度
- isLooping():是否循环播放
- isPlaying():是否正在播放
- pause():暂停
- prepare():准备(同步)
- prepareAsync():准备(异步)
- release():释放MediaPlayer对象
- reset():重置MediaPlayer对象
- seekTo(int msec):指定播放的位置(以毫秒为单位的时间)
- setAudioStreamType(int streamtype):指定流媒体的类型
- setDisplay(SurfaceHolder sh):设置用SurfaceHolder来显示多媒体
- setLooping(boolean looping):设置是否循环播放
- setOnBufferingUpdateListener(MediaPlayer.OnBufferingUpdateListener listener): 网络流媒体的缓冲监听
- setOnCompletionListener(MediaPlayer.OnCompletionListener listener): 网络流媒体播放结束监听
- setOnErrorListener(MediaPlayer.OnErrorListener listener): 设置错误信息监听
- setOnVideoSizeChangedListener(MediaPlayer.OnVideoSizeChangedListener listener): 视频尺寸监听
- setScreenOnWhilePlaying(boolean screenOn):设置是否使用SurfaceHolder显示
- setVolume(float leftVolume, float rightVolume):设置音量
- start():开始播放
- stop():停止播放
附加两个播放本地音乐和网络音乐的方法:
/** * 播放本地音乐 * @param filePath */ private void playLocalSound(String filePath) { // initialize Uri here Uri myUri = Uri.fromFile(new File(filePath)); MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); try { mediaPlayer.setDataSource(getApplicationContext(), myUri); mediaPlayer.prepare(); mediaPlayer.start(); } catch (IOException e) { e.printStackTrace(); } } /** * 播放网络音乐(加载会比较慢,可以考虑使用异步任务) * @param url */ private void playUrlSound(String url) { MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); try { mediaPlayer.setDataSource(url); mediaPlayer.prepare(); // might take long! (for buffering, etc) mediaPlayer.start(); } catch (IOException e) { e.printStackTrace(); } }
1、初始化SurfaceHolder对象,SurfaceHolder是SurfaceView的控制器。
2、实现SurfaceHolder的Callback接口。
3、在接口方法SurfaceCreated()中创建MediaPlayer对象并赋值。
直接上代码:
package com.deepreality.mediaplayerdemo;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
public class SurfaceViewActivity extends AppCompatActivity implements View.OnClickListener, SurfaceHolder.Callback {
private Context mContext;
private SurfaceView mSurfaceView;
private MediaPlayer mMediaPlayer;
private SurfaceHolder mSurfaceHolder;
private Button btnPlayVideo, btnPauseVideo, btnStopVideo;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_surfaceview);
mContext = this;
bindViews();
viewsAddListener();
surfaceHolderInit();
}
private void bindViews() {
mSurfaceView = findViewById(R.id.Activity_SurfaceView_svVideo);
btnPlayVideo = findViewById(R.id.Activity_SurfaceView_btnPlayVideo);
btnPauseVideo = findViewById(R.id.Activity_SurfaceView_btnPauseVideo);
btnStopVideo = findViewById(R.id.Activity_SurfaceView_btnStopVideo);
}
private void viewsAddListener() {
btnPlayVideo.setOnClickListener(this);
btnPauseVideo.setOnClickListener(this);
btnStopVideo.setOnClickListener(this);
}
/**
* 初始化SurfaceHolder类,SurfaceView的控制器
*/
private void surfaceHolderInit() {
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setFixedSize(320, 220);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
}
mMediaPlayer.release();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Activity_SurfaceView_btnPlayVideo: {
mMediaPlayer.start();
break;
}
case R.id.Activity_SurfaceView_btnPauseVideo: {
mMediaPlayer.pause();
break;
}
case R.id.Activity_SurfaceView_btnStopVideo: {
mMediaPlayer.stop();
break;
}
default:break;
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mMediaPlayer = MediaPlayer.create(mContext, R.raw.lesson);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//设置显示视频显示在SurfaceView上
mMediaPlayer.setDisplay(holder);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}