如何安装MediaPlayer的使用SurfaceView在Android的? [英] How to attach MediaPlayer with SurfaceView in android?

查看:185
本文介绍了如何安装MediaPlayer的使用SurfaceView在Android的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建的视频播放器与Android的媒体播放器对象。我能够听到声音但视频没有出现在surfaceView。这里是我的code

I'm building video player with android media player object. i'm able to hear the audio but the video does not appear on surfaceView. here is my code

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();
    }
}

目标的Andr​​oid 2.3及以上。如何解决此问题,请帮助我。

target is android 2.3 and above. how to fix it please help me..

推荐答案

最后我固定它自己。只是叫 mp.setDisplay(持有人); 的surfaceCreated()函数中。而最终code是

finally i fixed it myself. just called the mp.setDisplay(holder); inside the surfaceCreated() function. and the final code is

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();
    }
}

这篇关于如何安装MediaPlayer的使用SurfaceView在Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆