3

動画を再生している 、 、 のアクティビティがありますが、コントローラが表示されませTextureViewん。しばらくグーグルで検索しましたが、解決策が見つかりませんでした。SurfaceMediaPlayer

再生/一時停止とシーカーを利用可能にするにはどうすればよいですか?

コード:

public class VideoActivity extends Activity implements SurfaceTextureListener, OnClickListener {

    private MediaPlayer mMediaPlayer;
    private TextureView textureView;
    private Uri videoUri;
    private Button playPause;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.video_layout_new);

        playPause = (Button) findViewById(R.id.playPauseBtn);
        playPause.setOnClickListener(this);

        int videoIndex = getIntent().getExtras().getInt("video");
        videoUri = null;

        switch (videoIndex) {

        case 1:
            videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video1);
            break;
        case 2:
            videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video2);
            break;
        case 3:
            videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video3);
            break;
        case 4:
            videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video4);
            break;

        }

        textureView = (TextureView) findViewById(R.id.textureView1);
        textureView.setSurfaceTextureListener(this);

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;

        int widthGo = (int) (width * 0.95);
        int heightGo = (int) (widthGo * 0.8);

        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(widthGo, heightGo);
        lp.gravity = Gravity.CENTER;

        textureView.setLayoutParams(lp);

    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
        }
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {

        Surface s = new Surface(surface);

        try {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(VideoActivity.this, videoUri);
            mMediaPlayer.setSurface(s);
            mMediaPlayer.prepare();
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.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();
        }

    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {

    }

    @Override
    public void onClick(View v) {

        if (v.equals(playPause)) {

            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.pause();
                playPause.setBackgroundResource(R.drawable.play_btn);
            } else {
                mMediaPlayer.start();
                playPause.setBackgroundResource(R.drawable.pause_btn);
            }

        }

    }

}
4

0 に答える 0