转自:http://blog.csdn.net/jiahui524/article/details/7108358


步骤:

mediaPlayer+ surfaceView 例子的步骤:
1,创建一个MediaPlayer,并创建三个按钮
2, 创建surfaceView,并设置surfaceView的getHolder.setType和getHolder.addCallback()
3,在addCallback的三个函数中,这里是如果在播放中建立的,则把positon=0 ,然后重新开始。
4,在按钮中设置mediaplayer的播放,大概是:
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//设置需要播放的视频
mediaPlayer.setDataSource("/mnt/sdcard/vid320X240.3gp");
//把视频画面输出到SurfaceView
mediaPlayer.setDisplay(surfaceView.getHolder()); ///关键 的地方!!!!!!
mediaPlayer.prepare();
//播放
mediaPlayer.start();

xml源代码:(注意需要三个png格式的图片,更改名字为play.png pause.png stop.png)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="fill_parent"
android:layout_height="360px" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >

<ImageButton
android:id="@+id/btnplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/play" />

<ImageButton
android:id="@+id/btnpause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pause" />

<ImageButton
android:id="@+id/btnstop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/stop" />
</LinearLayout>

</LinearLayout>

java 代码:

package com.example.tstmediaplaycontroller2;
/*
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}


}*/
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

ImageButton btnplay, btnstop, btnpause;
SurfaceView surfaceView;
MediaPlayer mediaPlayer;
int position;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnplay=(ImageButton)this.findViewById(R.id.btnplay);
btnstop=(ImageButton)this.findViewById(R.id.btnstop);
btnpause=(ImageButton)this.findViewById(R.id.btnpause);

btnstop.setOnClickListener(this);
btnplay.setOnClickListener(this);
btnpause.setOnClickListener(this);

mediaPlayer=new MediaPlayer();
surfaceView=(SurfaceView) this.findViewById(R.id.surfaceView);

//设置SurfaceView自己不管理的缓冲区
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.getHolder().addCallback(new Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
if (position>0) {
try {
//开始播放

play();

//并直接从指定位置开始播放
mediaPlayer.seekTo(position);
position=0;
} catch (Exception e) {
// TODO: handle exception
}
}
System.out.println("surface create .............xxxxxxxxxxxxxxxx");

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

}
});
}
@Override
public void onClick(View v)
{
switch (v.getId()) {
case R.id.btnplay:
play();
break;

case R.id.btnpause:
if (mediaPlayer.isPlaying())
{
mediaPlayer.pause();
}
else
{
mediaPlayer.start();
}
break;

case R.id.btnstop:
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}

break;
default:
break;
}

}
@Override
protected void onPause() {
//先判断是否正在播放
if (mediaPlayer.isPlaying()) {
//如果正在播放我们就先保存这个播放位置
position=mediaPlayer.getCurrentPosition()
;
mediaPlayer.stop();
}
super.onPause();
}

private void play() {
try {
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//设置需要播放的视频
mediaPlayer.setDataSource("/mnt/sdcard/vid320X240.3gp");
//把视频画面输出到SurfaceView
mediaPlayer.setDisplay(surfaceView.getHolder());
mediaPlayer.prepare();
//播放
mediaPlayer.start();
Toast.makeText(this, "开始播放!", Toast.LENGTH_LONG).show();
} catch (Exception e) {
// TODO: handle exception
}
}
}



注:

MediaPlayer主要用于播放音频,它是没有提供输出图像的输出界面,这时我们就用到了SurfaceView控件,将它与MediaPlayer结合起来,就能达到了视频的输出了。首先来了SurfaceView这个控件类

SurfaceView类


构造方法

方法名称

描述

public SurfaceView(Context context)

通过Context创建SurfaceView对象

public SurfaceView(Context context, AttributeSet attrs)

通过Context对象和AttributeSet创建SurfaceView对象

public SurfaceView(Context context, AttributeSet attrs, int defStyle)

通过Context对象和AttributeSet创建并可以指定样式,SurfaceView对象

常用方法

方法名称

描述

public SurfaceHolder getHolder ()

得到SurfaceHolder对象用于管理SurfaceView

public void setVisibility (int visibility)

设置是否可见,其值可以是VISIBLE, INVISIBLE, GONE.

SurfaceHolder


它是一个接口,用于管理SurfaceView。里面有两个常用的内部接口SurfaceHolder.Callback,SurfaceHolder.Callback2而Callback2是实现于Callback的

常用方法

方法名称

描述

public abstract void addCallback (SurfaceHolder.Callback callback)

添加一个Callback对象监听SurfaceView的变化

public abstract void removeCallback (SurfaceHolder.Callback callback)

移除Callback

public abstract void setType (int type)

设置SurfaceView的控制方式

public abstract Canvas lockCanvas ()

锁定整个SurfaceView对象,获取该Surface上的Canvas

public abstract Canvas lockCanvas (Rect dirty)

锁定SurfaceView上Rect划分的区域,获取该Surface上的Canvas

public abstract void unlockCanvasAndPost (Canvas canvas)

调用该方法,之前所绘制的图形还处于缓冲之中,下一次的lockCanvas()方法锁定的区域可能会“遮挡”它

SurfaceHolder.CallBack


在Callback里有三个抽象方法

方法名称

描述

public abstract void surfaceChanged (SurfaceHolder holder, int format, int width, int height)

SurfaceView改变时触发

public abstract void surfaceCreated (SurfaceHolder holder)

SurfaceView创建时触发

public abstract void surfaceDestroyed (SurfaceHolder holder)

SurfaceView销毁时触发

如何理解这几个类或者接口之间的关系?

这样理解:

SurfaceView它用于显示,SurfaceHolder就是用于用来管理这个显示的SurfaceView对象的,但在SurfaceHolder是怎么样去管理这个对象的呢?这就用到了SurfceHolder.addCallback()方法添加一个SurfaceHolder接口的内部接口的三个抽象方法用于管理或者说是用于监听SurfaceView。这样就达到了管理SurfaceView的目的。

mediaPlayer 的方法是:


Valid and invalid states

Method Name

Valid Sates

Invalid States

Comments

attachAuxEffect

{Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted}

{Idle, Error}

This method must be called after setDataSource. Calling it does not change the object state.

getAudioSessionId

any

{}

This method can be called in any state and calling it does not change the object state.

getCurrentPosition

{Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted}

{Error}

Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to theErrorstate.

getDuration

{Prepared, Started, Paused, Stopped, PlaybackCompleted}

{Idle, Initialized, Error}

Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to theErrorstate.

getVideoHeight

{Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted}

{Error}

Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to theErrorstate.

getVideoWidth

{Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted}

{Error}

Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to theErrorstate.

isPlaying

{Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted}

{Error}

Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to theErrorstate.

pause

{Started, Paused}

{Idle, Initialized, Prepared, Stopped, PlaybackCompleted, Error}

Successful invoke of this method in a valid state transfers the object to thePausedstate. Calling this method in an invalid state transfers the object to theErrorstate.

prepare

{Initialized, Stopped}

{Idle, Prepared, Started, Paused, PlaybackCompleted, Error}

Successful invoke of this method in a valid state transfers the object to thePreparedstate. Calling this method in an invalid state throws an IllegalStateException.

prepareAsync

{Initialized, Stopped}

{Idle, Prepared, Started, Paused, PlaybackCompleted, Error}

Successful invoke of this method in a valid state transfers the object to thePreparingstate. Calling this method in an invalid state throws an IllegalStateException.

release

any

{}

Afterrelease(), the object is no longer available.

reset

{Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, Error}

{}

Afterreset(), the object is like being just created.

seekTo

{Prepared, Started, Paused, PlaybackCompleted}

{Idle, Initialized, Stopped, Error}

Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to theErrorstate.

setAudioSessionId

{Idle}

{Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, Error}

This method must be called in idle state as the audio session ID must be known before calling setDataSource. Calling it does not change the object state.

setAudioStreamType

{Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted}

{Error}

Successful invoke of this method does not change the state. In order for the target audio stream type to become effective, this method must be called before prepare() or prepareAsync().

setAuxEffectSendLevel

any

{}

Calling this method does not change the object state.

setDataSource

{Idle}

{Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, Error}

Successful invoke of this method in a valid state transfers the object to theInitializedstate. Calling this method in an invalid state throws an IllegalStateException.

setDisplay

any

{}

This method can be called in any state and calling it does not change the object state.

setSurface

any

{}

This method can be called in any state and calling it does not change the object state.

setVideoScalingMode

{Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted}

{Idle, Error}

Successful invoke of this method does not change the state.

setLooping

{Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted}

{Error}

Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to theErrorstate.

isLooping

any

{}

This method can be called in any state and calling it does not change the object state.

setOnBufferingUpdateListener

any

{}

This method can be called in any state and calling it does not change the object state.

setOnCompletionListener

any

{}

This method can be called in any state and calling it does not change the object state.

setOnErrorListener

any

{}

This method can be called in any state and calling it does not change the object state.

setOnPreparedListener

any

{}

This method can be called in any state and calling it does not change the object state.

setOnSeekCompleteListener

any

{}

This method can be called in any state and calling it does not change the object state.

setScreenOnWhilePlaying any

{}

This method can be called in any state and calling it does not change the object state.

setVolume

{Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted}

{Error}

Successful invoke of this method does not change the state.
setWakeMode

any

{}

This method can be called in any state and calling it does not change the object state.

start

{Prepared, Started, Paused, PlaybackCompleted}

{Idle, Initialized, Stopped, Error}

Successful invoke of this method in a valid state transfers the object to theStartedstate. Calling this method in an invalid state transfers the object to theErrorstate.

stop

{Prepared, Started, Stopped, Paused, PlaybackCompleted}

{Idle, Initialized, Error}

Successful invoke of this method in a valid state transfers the object to theStoppedstate. Calling this method in an invalid state transfers the object to theErrorstate.

getTrackInfo

{Prepared, Started, Stopped, Paused, PlaybackCompleted}

{Idle, Initialized, Error}

Successful invoke of this method does not change the state.

addTimedTextSource

{Prepared, Started, Stopped, Paused, PlaybackCompleted}

{Idle, Initialized, Error}

Successful invoke of this method does not change the state.

selectTrack

{Prepared, Started, Stopped, Paused, PlaybackCompleted}

{Idle, Initialized, Error}

Successful invoke of this method does not change the state.

deselectTrack

{Prepared, Started, Stopped, Paused, PlaybackCompleted}

{Idle, Initialized, Error}

Successful invoke of this method does not change the state.



更多相关文章

  1. Java对象引用处理机制
  2. Object 开发中常用的重写方法总结toString,equals,hashCode,compare
  3. java 构造器内部的多态方法和行为
  4. Javassist生成class(生成类,方法,字段,注解)
  5. Eclipse创建的包变成文件夹的解决方法
  6. java中InputStream中的抽象方法read()为什么可以直接调用?
  7. 牛客网Java刷题知识点之同步方法和同步代码块的区别(用synchroniz
  8. 如何从Java中的类名获取类对象
  9. 域对象/服务和业务逻辑层

随机推荐

  1. android socket io 前篇
  2. Android客户端上传文件,C#服务端接收文件
  3. android 录音 Android 使用AudioRecord录
  4. Android Studio 2.0 Beta 5发布,修复几个
  5. android permission
  6. 几个activity跳转特效的实现
  7. Android下如何获取CPU序列号
  8. Android 密度转换实例
  9. 一个基于ffmpeg的简易视频播放器
  10. Android 获取AndroidManifest.xml文件ver