安卓中surfaceview播放完后,怎么恢复无图像状态
1个回答
展开全部
关于视频播放的程序
代码如下:
package com.example.mediavideoplayer;
import java.io.File;
import java.io.IOException;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText nameText;
private String path;
private MediaPlayer mediaPlayer;
private SurfaceView surfaceView;
private boolean pause;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = new MediaPlayer();//对mediaPlayer初始化化
nameText = (EditText)findViewById(R.id.filename);
surfaceView = (SurfaceView)findViewById(R.id.surfaceView);
//把输送给surfaceView视频画面,直接显示在屏幕上,不要维持它自身的缓冲区
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.getHolder().setFixedSize(176, 144);
surfaceView .getHolder().setKeepScreenOn(true);//使屏幕保持高亮状态
}
@Override//重写onDestory方法在此方法中对mediaPlayer进行摧毁
protected void onDestroy() {
mediaPlayer.release();
mediaPlayer = null;
super.onDestroy();
}
public void mediaplay(View v){
switch (v.getId()) {
case R.id.playbutoon:
String filename = nameText.getText().toString();
File file = new File(Environment.getExternalStorageDirectory(), filename);
//判断文件在SD卡中是否存在
if(file.exists()){
//取的文件路径
path = file.getAbsolutePath();
play();
}else{
Toast.makeText(this, R.string.filenoexsit, 1).show();
}
break;
case R.id.pausebutoon:
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
pause = true;
}else{
if(pause){
mediaPlayer.start();
pause = false;
}
}
break;
case R.id.resetbutoon:
if(mediaPlayer.isPlaying()){
mediaPlayer.seekTo(0);//如果正在播放,回到0时刻点
}else{
//如果没有在播放,则判断路径是否存在,如存在重新播放
if(path!=null){
play();
}
}
break;
case R.id.stopbutoon:
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
break;
}
}
private void play() {
try {
mediaPlayer.reset();//播放器恢复到最初始状态
mediaPlayer.setDataSource(path);
mediaPlayer.setDisplay(surfaceView.getHolder());//setDisplay用于设置视频的画面传输方向
mediaPlayer.prepare();//缓冲
mediaPlayer.setOnPreparedListener(new PrepareListener());
}
catch (Exception e) {
e.printStackTrace();
}
}
private final class PrepareListener implements OnPreparedListener{
public void onPrepared(MediaPlayer arg0) {
mediaPlayer.start();//播放视频
}
}
}
我运行模拟器点开始按钮没有反应
代码如下:
package com.example.mediavideoplayer;
import java.io.File;
import java.io.IOException;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText nameText;
private String path;
private MediaPlayer mediaPlayer;
private SurfaceView surfaceView;
private boolean pause;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = new MediaPlayer();//对mediaPlayer初始化化
nameText = (EditText)findViewById(R.id.filename);
surfaceView = (SurfaceView)findViewById(R.id.surfaceView);
//把输送给surfaceView视频画面,直接显示在屏幕上,不要维持它自身的缓冲区
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.getHolder().setFixedSize(176, 144);
surfaceView .getHolder().setKeepScreenOn(true);//使屏幕保持高亮状态
}
@Override//重写onDestory方法在此方法中对mediaPlayer进行摧毁
protected void onDestroy() {
mediaPlayer.release();
mediaPlayer = null;
super.onDestroy();
}
public void mediaplay(View v){
switch (v.getId()) {
case R.id.playbutoon:
String filename = nameText.getText().toString();
File file = new File(Environment.getExternalStorageDirectory(), filename);
//判断文件在SD卡中是否存在
if(file.exists()){
//取的文件路径
path = file.getAbsolutePath();
play();
}else{
Toast.makeText(this, R.string.filenoexsit, 1).show();
}
break;
case R.id.pausebutoon:
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
pause = true;
}else{
if(pause){
mediaPlayer.start();
pause = false;
}
}
break;
case R.id.resetbutoon:
if(mediaPlayer.isPlaying()){
mediaPlayer.seekTo(0);//如果正在播放,回到0时刻点
}else{
//如果没有在播放,则判断路径是否存在,如存在重新播放
if(path!=null){
play();
}
}
break;
case R.id.stopbutoon:
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
break;
}
}
private void play() {
try {
mediaPlayer.reset();//播放器恢复到最初始状态
mediaPlayer.setDataSource(path);
mediaPlayer.setDisplay(surfaceView.getHolder());//setDisplay用于设置视频的画面传输方向
mediaPlayer.prepare();//缓冲
mediaPlayer.setOnPreparedListener(new PrepareListener());
}
catch (Exception e) {
e.printStackTrace();
}
}
private final class PrepareListener implements OnPreparedListener{
public void onPrepared(MediaPlayer arg0) {
mediaPlayer.start();//播放视频
}
}
}
我运行模拟器点开始按钮没有反应
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询