1. The reason for the black screen: it may be because the surfaceView has not been created and played before it is definitely not possible, so it must be played in surfaceViewCreated
2. The second reason for the black screen: activity is The user is forced to close, or the interface, if you save the original state next time you come in, then the screen may be black, the starting point of playback must also be placed in surfaceViewCreate, you can only resume playback from here, and when the activity is forced to exit, you need to In surfaceDestroyed records the position, and if mediaPlayer is playing, it needs to be paused
public void surfaceDestroyed(SurfaceHolder holder){
if(mediaPlayer.isPlaying()){
position = MediaPlayer.getCurrentPosition();
mediaPlayer.stop();
3. Play After finishing, change the background image of surfaceView to sv.setBackgroundResource(R.drawable.bg_finish );
//Change the background image of SurfaceView
4. One of the stuttering problems, stuttering is a super serious problem: if it’s an error reported by prepare, please Put mp. The start(); method is placed in the listener event, that is, in the onPrepare method. This position is that you are not ready yet, how can you play it? Before you are ready, you can set it to continuously load the handle
5. One of the reasons why playback fails or the screen flashes directly: from one video activity or view to another video View playback, if the time is tight, it may immediately fail or crash. The reason for this is that the mediaPlayer has not had time to release, or the surfaceView has not been destroyed. In short, there are two objects, and it must have failed. Someone Speaking of the problem with the original Android device, if the operating system used by the device is modified by a third-party manufacturer, this element may appear. If it is the original ecology, it may not. The solution is to delay the playback of the second video, or A slight delay when jumping, or a pop-up is loading when the video is playing, let the progress bar delay the rotation time of one second, and then play, it should be able to solve this problem [http://doublekj.blog.163.com/blog /static/146818474201272453250354/]
- privateRunnable runner=newRunnable()
- {
- @Override
- public voidrun()
- span>
- handler.sendEmptyMessageDelayed(0,2000); li>
< li class="alt" style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color: inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> };
6. It should be noted that when resetting multiple times, it will consume a part of the time, that is, the mediaPlayer playback time.
7. If mediaPlayer is destroyed many times and created many times, it may also allocate a certain amount of space each time. The cost is quite high, but it’s safe and won’t appear If it is null
8. If the same video is being played at this time, it will also cause the lagging reason
< p>9. Regarding the sound problem, it is better to execute after setting the data source:
put
mPlayer.setAudioStreamType(AudioManager.STREAM_SYSTEM);
replace
mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
or
mediaPlayer.setAudioStreamType(AudioManager .STREAM_VOICE_CALL);//Handset mode
The method of mediaPlayer is:
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 the Error state.
|
getDuration
|
{Prepared, Started, Paused, Stopped, PlaybackCompleted}
|
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 the Error state.
|
|
getVideoHeight
|
{Idle, Initialized, Prepared, Star ted, 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 the Error state.
|
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 the Error state.
< /p> |
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 the Error state.
|
pause
|
{ Started, Paused}
|
{Idle, Initialized, Prepared, Stopped, PlaybackCompleted, Error}
|
Successful invoke of this method in a valid state transfers the object to the Paused state. Calling this method in an invalid state transfers the object to the Error em> state.
|
{Initialized, Stopped}
|
{Idle, Prepared, Started, Paused, PlaybackCompleted, Error}
|
Successful invoke of this method in a val id state transfers the object to the Prepared state. Calling this method in an invalid state throws an IllegalStateException.
|
|
prepareAsync
|
{Initialized, Stopped}
|
{Idle, Prepared, Started, Paused, PlaybackCompleted, Error}
td> | Successful invoke of this method in a valid state transfers the object to the Preparing state. 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}
|
{}
p> |
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 the Error state.
< /p> |
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 the Initialized state. 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 the Error state.
|
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.
p> |
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 the Started state. Calling this method in an invalid state transfers the object to the Error state.
|
stop
|
{Prepared, Started, Stopped, Paused, PlaybackCompleted}
|
{Idle, Initialized, Error}
|
Successful invoke of this method in a valid state transfers the object to the Stopped state. Calling this method in an invalid state transfers the object to the Error state.
|
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. |
surfaceView的相关概念:
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) |
添加一个Callba ck对象监听SurfaceView的变化 |
public abstract void removeCallback (SurfaceHolder.Callback callback) |
移除Callback |
public abstract void setType (int type) |
设置SurfaceView的控制方式 |
public abstract Canvas lockCanvas () |
锁定整个Surfac eView对象,获取该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的目的。
- 顶
- 0
- 踩
mediaPlayer 的方法是:
Valid and invalid states
Method Na me
|
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 the Error state.
|
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 the Error state.
|
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 the Error state.
|
getVideoWidth
|
{Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted}
|
{Error}
|
Successful invoke of this meth od in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state.
|
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 the Error state.
|
pause
|
{Started, Paused}
|
{Idle, Initialized, Prepared, Stopped, PlaybackCompleted, Error}
|
Successful invoke of this method in a valid state transfers the object to the Paused state. Calling this method in an invalid state transfers the object to the Error state.
|
prepare
|
{Initialized, Stopped}
|
{Idle, Prepared, Started, Paused, PlaybackCompleted, Error}
|
Successful invoke of this method in a valid state transfers the object to the Prepared state. 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 the Preparing state. Calling this method in an invalid state throws an IllegalStateException.
|
release
|
any
|
{}
|
After release() , the object is no longer available.
|
reset
|
{Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, Error}
|
{}
|
After reset() , 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 the Error state.
< /p> |
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 chan ge 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 the Initialized state. 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 the Error state.
|
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 ca lled 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 the Started state. Calling this method in an invalid state transfers the object to the Error state.
|
stop
|
{Prepared, Started, Stopped, Paused, PlaybackCompleted}
|
{Idle, Initialized, Error}
|
Successful invoke of this method in a valid state transfers the object to the Stopped state. Calling this method in an invalid state transfers the object to the Error state.
|
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. |
surfaceView的相关概念:
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) |
< p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 通过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对象监听Surf aceView的变化 |
public abstract void removeCallback (SurfaceHolder.Callback callback) |
移除Callback |
public abstract void setType (int type) |
设置SurfaceView的控制方式 |
public abstract Canvas lockCanvas () |
锁定整个SurfaceView对象,获取该Surface上的Ca nvas |
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的目的。
- 顶
- 0
- 踩