简体   繁体   English

Android中的“ Surface”类到底是什么?

[英]What exactly is a “Surface” class in Android?

I have been trying for a long time to render a video on a "Surface" class using MediaPlayer class. 我一直在尝试使用MediaPlayer类在“ Surface”类上呈现视频。 It was playing audio, but not the video. 它正在播放音频,但不播放视频。 Everywhere I search, people talk about SurfaceView and SurfaceHolder but I have only a Surface object. 我到处搜索的人都谈论SurfaceView和SurfaceHolder,但我只有一个Surface对象。 How to crack this blocker? 如何破解这个障碍?

This is how I tried, 这就是我尝试过的方式

public class SampleVideoPlayer{

private Uri mUrl;
private Surface mSurface;
private MediaPlayer mMediaPlayer;
private Context mContext;

public SampleVideoPlayer(Context context, String url, Surface surface){
    mUrl = Uri.parse(url);
    mSurface = surface;
    mMediaPlayer = new MediaPlayer();
    mContext = context;
}

public void playVideo() throws IOException {
        mMediaPlayer.setDataSource(mContext, mUrl);
        mMediaPlayer.setSurface(mSurface);
        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.start();
            }
        });
        mMediaPlayer.prepareAsync();
}

} }

Adding the Session Object I am passing, 添加我正在传递的会话对象,

public class MyTvSession extends TvInputService.Session implements Handler.Callback {

Context mContext;
String vidUrl;
Surface mSurface;
SampleVideoPlayer player = null;
SampleMediaPlayer mediaPlayer;

public MyTvSession(Context context){
    super(context);
    ChannelXmlReader reader = new ChannelXmlReader(context);
    ArrayList<Channel> channels = reader.ReadXml();
    mContext = context;
    vidUrl = channels.get(0).url;
}
@Override
public boolean handleMessage(Message message) {
    Log.d("HANDLE MESSAGE", message.toString());
    return true;
}

@Override
public void onRelease() {

}

@Override
public boolean onSetSurface(Surface surface) {
    if(surface != null)
        Log.d("NOT NULL from SESSION", "NOTNULL");
    mSurface = surface;
    return true;
}

@Override
public void onSurfaceChanged(int format, int width, int height) {
    super.onSurfaceChanged(format, width, height);
    if(mediaPlayer != null)
        mediaPlayer.mMediaPlayer.setSurface(mSurface);

    Log.d("ONSURFACECHANGED", "Event");
}



@Override
public void onSetStreamVolume(float v) {

}

@Override
public boolean onTune(Uri uri) {
    Log.d("TUNING CHANNEL", uri.toString());
    try {
        mediaPlayer = new SampleMediaPlayer(mContext, vidUrl, mSurface);
        mediaPlayer.playVideo();
    }catch(Exception e){
        Log.d("MPEXCEPTION", Log.getStackTraceString(e));
    }
    return true;
}

@Override
public void onSetCaptionEnabled(boolean b) {

}

} }

The Surface class is a thin wrapper around a buffer list shared with the backing surfaceflinger process, which is responsible for rendering to the display. Surface类是与底层SurfaceFlinger进程共享的缓冲区列表周围的瘦包装,后者负责渲染到显示器。

You can get one of these using the SurfaceView and its SurfaceHolder , which are tied to the lifecycle of the view. 您可以使用SurfaceView及其SurfaceHolder获得其中之一,它们与视图的生命周期相关。 So be sure to get it after being called back when the surface has been created. 因此,请确保在创建曲面后调用它后才能获取它。

Alternatively, you can use a SurfaceTexture which is created using your own custom OpenGL context. 或者,您可以使用使用您自己的自定义OpenGL上下文创建的SurfaceTexture With this approach you can render using your own OpenGL code or even pass it off to the media engine for rendering. 使用这种方法,您可以使用自己的OpenGL代码进行渲染,甚至将其传递给媒体引擎进行渲染。 You can also get a SurfaceTexture tied to the view subsystem by using TextureView (but like SurfaceView you'll need to use it at the appropriate time in its lifecycle.) 您还可以通过使用TextureViewSurfaceTexture绑定到视图子系统(但是像SurfaceView一样,您需要在其生命周期的适当时间使用它)。

I have exactly the same problem. 我也有完全一样的问题。 But it only happens on Philips TV. 但这仅在飞利浦电视上发生。 The same code runs fine on every other Android TV devices. 相同的代码可以在其他所有Android TV设备上正常运行。 The surface I get in onSetSurface is valid, sound is playing, but picture is black. 我在onSetSurface中获得的表面有效,正在播放声音,但是图像为黑色。 When I close the app, the video is visible for a second. 当我关闭应用程序时,该视频将显示一秒钟。 It seems to be in the background. 似乎是在后台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM