为什么SurfaceView不能在android中显示3gp视频

时间:2012-05-18 22:50:32

标签: android video media surfaceview

我使用以下示例代码播放3gp视频。在Android 2.3 ..但只播放音频,但视频没有出现。有什么问题?

package pit.opensource.video;

import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;

public class L2VideoExampleActivity extends Activity implements SurfaceHolder.Callback{

    MediaPlayer mediaPlayer;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    boolean pausing = false;

    AudioManager audioManager;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        SeekBar volControl = (SeekBar)findViewById(R.id.volbar);
        volControl.setMax(maxVolume);
        volControl.setProgress(curVolume);
        volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStartTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
                // TODO Auto-generated method stub
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
            }
        });


        Button buttonPlayVideo = (Button)findViewById(R.id.playvideoplayer);
        Button buttonPauseVideo = (Button)findViewById(R.id.pausevideoplayer);

        getWindow().setFormat(PixelFormat.UNKNOWN);
        surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setFixedSize(176, 144);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mediaPlayer = new MediaPlayer();

        buttonPlayVideo.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                pausing = false;

                if(mediaPlayer.isPlaying()){
                    mediaPlayer.reset();
                }

                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mediaPlayer.setDisplay(surfaceHolder);

                try {
                //  mediaPlayer.setDataSource(stringPath);
                //  mediaPlayer.prepare();
                    mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.samplevideo);

                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                mediaPlayer.start();


            }});

        buttonPauseVideo.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(pausing){
                    pausing = false;
                    mediaPlayer.start();
                }
                else{
                    pausing = true;
                    mediaPlayer.pause();
                }
            }});

    }



    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub

    }
}

1 个答案:

答案 0 :(得分:0)

我认为你必须在surfaceview中播放视频。你现在在oncreate中播放视频,可能还没有创建surfaceview。 在surfaceCreated

中使用类似的内容
    @Override
    public void surfaceCreated(SurfaceHolder holder) 
    {

             if(ArrayManager.array_for_playing != null)
        if(SDcard.icurrentIndex != -1)
          playVideo(SDcard.icurrentIndex);
    }

这是playVideo方法

    private void playVideo(Integer Media) 
    {

        try {

            mPlayer.reset();
            mPlayer.setDataSource(ArrayManager.array_for_playing.get(SDcard.icurrentIndex).getPath());

          mPlayer.setDisplay(holder);                
            songProgressBar.setProgress(0);
            songProgressBar.setMax(100);
            mPlayer.prepare();
            mPlayer.start();
            btnPlay.setBackgroundResource(R.drawable.img_btn_pause);

            // Updating progress bar
            updateProgressBar();

        } 
        catch (Exception e) 
        {
            Log.e("video player", "error: " + e.getMessage(), e);
        }
    }

在IniatializeUi方法

     private void InitializeUI()
     {
            mPreview = (SurfaceView) findViewById(R.id.surface_view);
            holder = mPreview.getHolder();
            holder.addCallback(this);
            holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
      }

并在活动之上:

    private SurfaceView mPreview;
    private SurfaceHolder holder;

我希望这能帮助用户。