13

我正在使用 android 媒体播放器对象构建视频播放器。我能听到音频,但视频没有出现在 SurfaceView 上。这是我的代码

public class PlayerActivity extends Activity implements SurfaceHolder.Callback {
    String path;
    private MediaPlayer mp;
    private SurfaceView mPreview;
    private SurfaceHolder holder;
    boolean pausing = false;
    public static String filepath;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);

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

        mp.setDisplay(holder);
        try {
            Intent intent = getIntent();
            Uri fileuri = intent.getData();
            filepath=fileuri.getPath();
        } catch(Exception e) {}

        try {
            mp.setDataSource(filepath);
            mp.prepare();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mp.start();
    }
}

目标是android 2.3及以上。如何解决它请帮助我..

4

2 回答 2

27

最后我自己修好了。刚刚调用了mp.setDisplay(holder);surfaceCreated()函数内部。最后的代码是

public class PlayerActivity extends Activity implements SurfaceHolder.Callback {
    String path;
    private MediaPlayer mp;
    private SurfaceView mPreview;
    private SurfaceHolder holder;
    boolean pausing = false;
    public static String filepath;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);

        getWindow().setFormat(PixelFormat.UNKNOWN);
        mPreview = (SurfaceView)findViewById(R.id.surfaceView);
        holder = mPreview.getHolder();
        holder.setFixedSize(800, 480);
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mp = new MediaPlayer();


        try{
            Intent intent = getIntent();

            Uri fileuri = intent.getData();
            filepath=fileuri.getPath();
        }catch(Exception e){}


    }
    protected void onPause(){
        super.onPause();
        mp.release();
    }

    @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
        mp.setDisplay(holder);
        play();
    }

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

    }
    void play(){
        try {
            mp.setDataSource(filepath);

            mp.prepare(); 

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mp.start();
    }
}
于 2013-05-23T07:02:09.223 回答
0

它对我不起作用,在surfaceCreated()方法中抛出错误非法状态异常mp.setDisplay(holder);

声明mp.setDisplay(holder)如下:

@Override
public void surfaceCreated(SurfaceHolder holder) 
{  
    mediaPlayer.setDataSource(this,uri);
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.prepare(); 
    mp.setDisplay(holder);
    mp.start;
}
于 2017-04-26T11:53:47.530 回答