从未调用过TextureView onSurfaceTextureAvailable

时间:2012-12-11 11:46:59

标签: android video

当我的Android应用程序启动时,我尝试播放启动活动的视频,我遇到的问题是onSurfaceTextureAvailable接口方法永远不会被调用。

以下是代码:

public class HomeActivity extends Activity implements TextureView.SurfaceTextureListener,
        MediaPlayer.OnBufferingUpdateListener, OnCompletionListener,
        MediaPlayer.OnPreparedListener, MediaPlayer.OnVideoSizeChangedListener {

    private MediaPlayer videoMediaPlayer;
    private TextureView videoPreview;
    private Bundle extras;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        videoPreview = new TextureView(this);
        videoPreview.setSurfaceTextureListener(this);
        extras = getIntent().getExtras();
        setContentView(videoPreview);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        Surface s = new Surface(surface);
        Log.d("HOME", "onSurfaceTextureAvailable");
        try {
            videoMediaPlayer= new MediaPlayer();
            Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
                    + R.raw.intro_video);
            videoMediaPlayer.setDataSource(getApplicationContext(), video);
            videoMediaPlayer.setSurface(s);
            videoMediaPlayer.prepare();
            videoMediaPlayer.setOnBufferingUpdateListener(this);
            videoMediaPlayer.setOnCompletionListener(this);
            videoMediaPlayer.setOnPreparedListener(this);
            videoMediaPlayer.setOnVideoSizeChangedListener(this);
            videoMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            videoMediaPlayer.start();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

// Other stuff

代码来自stackoverflow类似question,似乎他让它像这样工作。

有什么建议吗?

1 个答案:

答案 0 :(得分:5)

要调用onSurfaceTextureAvailable(),必须启用硬件加速。你可以在AndroidManifast.xml文件中执行此操作

<activity
    android:name="com.example.HomeActivity"
    android:hardwareAccelerated="true" >
</activity>