SufaceView 结合MediaPlayer播放视频 案例如下:

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:id=”@+id/activity_main”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”com.example.zipdemo.MainActivity”>

<SurfaceView
android:id=”@+id/surface_vidio”
android:layout_width=”300dp”
android:layout_height=”300dp”
android:layout_centerInParent=”true” />

<LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:orientation=”vertical”>

 

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”

android:background=”@drawable/shape”
android:onClick=”onClick”
android:text=”播放” />

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:onClick=”onClick2″ />

<Button
android:layout_width=”wrap_content”

android:layout_height=”wrap_content”
android:onClick=”onClick3″ />

<View

android:layout_width=”match_parent”
android:layout_height=”match_parent” />

</LinearLayout>

<TextView
android:layout_width=”match_parent”
android:layout_height=”10dp”
android:layout_centerInParent=”true”
android:shadowRadius=”10″
android:shadowColor=”#f0f”
android:shadowDx=”20″
android:background=”#0Ff0″
android:shadowDy=”-20″ />
</RelativeLayout> 
逻辑代码:

package com.example.zipdemo;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;

/**
* 时间:2017年2月9日10:38:50
* 范明毅
*/
public class MainActivity extends AppCompatActivity {

private static final String TAG = “MainActivity”;
private SurfaceView surfaceView;
private MediaPlayer mediaPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initView();
}

private void initView() {

surfaceView = ((SurfaceView) findViewById(R.id.surface_vidio));

}

public void onClick(View view) {

mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
File file = new File(Environment.getExternalStorageDirectory(), “test.mp4”);
if (file.exists()) {
Log.e(TAG, “播放视屏 “);
try {
mediaPlayer.setDataSource(file.getPath());

mediaPlayer.setDisplay(surfaceView.getHolder());

mediaPlayer.prepareAsync();

mediaPlayer.setLooping(true);

mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Log.e(TAG, “装载完成”);
mediaPlayer.start();
flag = true;
}
});

} catch (IOException e) {
e.printStackTrace();
}

} else {

Toast.makeText(this, “无视屏”, Toast.LENGTH_SHORT).show();

}

}

boolean flag;

public void onClick2(View view) {
mediaPlayer.pause();
}

public void onClick3(View view) {
mediaPlayer.start();
}

@Override
protected void onResume() {
super.onResume();

}

上面的代码运行后,可以正常播放视频,但是返回后台(按下Home键)再返回的时候发现停止了视频 surfaceview就黑漆漆一片。

—解决方案—

package com.example.zipdemo;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;

/**
* 时间:2017年2月9日10:38:50
* 范明毅
*/
public class MainActivity extends AppCompatActivity {

private static final String TAG = “MainActivity”;
private SurfaceView surfaceView;
private MediaPlayer mediaPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initView();
}

private void initView() {

surfaceView = ((SurfaceView) findViewById(R.id.surface_vidio));

}

public void onClick(View view) {

mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
File file = new File(Environment.getExternalStorageDirectory(), “test.mp4”);
if (file.exists()) {
Log.e(TAG, “播放视屏 “);
try {
mediaPlayer.setDataSource(file.getPath());

 

mediaPlayer.setDisplay(surfaceView.getHolder());
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (flag){
boolean playing = mediaPlayer.isPlaying();
mediaPlayer.setDisplay(holder);
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.e(TAG, “surfaceCreated: ” );
}
});
mediaPlayer.prepareAsync();

mediaPlayer.setLooping(true);

mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Log.e(TAG, “装载完成”);
mediaPlayer.start();
flag =true;
}
});

 

} catch (IOException e) {
e.printStackTrace();
}

} else {

Toast.makeText(this, “无视屏”, Toast.LENGTH_SHORT).show();

}

}
boolean flag ;
public void onClick2(View view) {
mediaPlayer.pause();
}

public void onClick3(View view) {
mediaPlayer.start();
}

@Override
protected void onResume() {
super.onResume();

}

核心代码:

surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (flag){
boolean playing = mediaPlayer.isPlaying();
mediaPlayer.setDisplay(holder);
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.e(TAG, “surfaceCreated: ” );
}
}); 
原因:

SurfaceView进入后台的时候会销毁,同时回调surfaceDestroyed方法。而创建的时候回调surfaceCreated方法 所以在这里重新设置画面即可

————————————————
版权声明:本文为CSDN博主「不会写代码的丝丽」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qfanmingyiq/article/details/56669184