java - 自己制作一个视频播放器,遇到问题,用的是内置surfaceview类,具体看代码!
迷茫
迷茫 2017-04-18 10:48:40
0
2
565
public class Main_activity extends AppCompatActivity {
  private ImageButton button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(ImageButton) findViewById(R.id.imageButton);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Main_activity.this,MyListView.class);
                startActivity(intent);
            }
        });

    }
}
public class MyListView extends AppCompatActivity {
    private ListView listView;
    private List<String> data = new ArrayList<String>();
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        listView = new ListView(this);
        data=getData();
        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,data));
        setContentView(listView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(MyListView.this,Surfaceview.class);
                intent.putExtra("datap",data.get(position));
                startActivity(intent);
                finish();
            }
        });
    }
    private List<String> getData(){
        List<String> data = new ArrayList<String>();
        ContentResolver c=getContentResolver();
        Cursor cur = c.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null,
                null, null,null);
        int num=cur.getCount();
        cur.moveToFirst();
        for (int i=0;i<num;i++){
            String datapath=cur.getString(cur.getColumnIndex(MediaStore.Video.Media.DATA));
            data.add(datapath);
            cur.moveToNext();
        }
        return data;
    }
}
public class Surfaceview extends AppCompatActivity {
    MediaPlayer mp;
    SurfaceView sv;
    Handler mHandler;
    Runnable mRunnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.surfacexml);
        Intent intent=this.getIntent();
        final String datapath=intent.getStringExtra("datap");
        sv=(SurfaceView)findViewById(R.id.surfaceView2);
        final LinearLayout linearLayout=(LinearLayout)findViewById(R.id.test);
        mp=new MediaPlayer();
        final Button bplay=(Button)findViewById(R.id.play);
        Button bbefore=(Button)findViewById(R.id.before);
        Button bnext=(Button)findViewById(R.id.next);
        final SeekBar seekbar=(SeekBar)findViewById(R.id.seekbar);

       sv.setOnTouchListener(new View.OnTouchListener() {
           @Override
           public boolean onTouch(View v, MotionEvent event) {
               mHandler.removeCallbacks(mRunnable);
               linearLayout.setVisibility(View.VISIBLE);
               mHandler.postDelayed(mRunnable,4000);
               return false;
           }
       });

        mHandler= new Handler();

        mRunnable = new Runnable() {
            @Override
            public void run() {
                linearLayout.setVisibility(View.INVISIBLE);
            }
        };



                try {
                    mp.setDataSource(datapath);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                mp.setDisplay(sv.getHolder());
                try {
                    mp.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                mp.start();
                Timer timer=new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        float x=mp.getDuration();
                        float i=mp.getCurrentPosition();
                        float p=i/x*100;
                        int g=(int)p;
                     seekbar.setProgress(g);
                    }
                },0, 500);
                seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    }
                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {
                    }
                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                    float x=seekBar.getProgress();
                        float a=mp.getDuration();
                        float i=x/100*a;
                        int j=(int)i;
                        mp.pause();
                        mp.seekTo(j);
                        mp.start();
                    }
                });

        bplay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

             if (mp.isPlaying()) {
                  mp.pause();
                  bplay.setText("play");              }

           else {
                   mp.start();
                 bplay.setText("pause");
               }
            }
        });
  }
}

以上是3个主类,我的思路是主页面一个按钮,然后点击就进入MyListView,就是用list类现实本机里所有视频文件,然后点击一个item,在进入Surfaceview,这是主播放的界面,但是前面都还好,就是最后这个surface进不去,我在努力思考原因,各种调试,但是还是没找出原因,错误如下:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: yuyu.mymedia, PID: 2524
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{yuyu.mymedia/yuyu.mymedia.Surfaceview}: java.lang.IllegalArgumentException: The surface has been released
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
                      at android.app.ActivityThread.-wrap14(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6688)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
                   Caused by: java.lang.IllegalArgumentException: The surface has been released
                      at android.media.MediaPlayer._setVideoSurface(Native Method)
                      at android.media.MediaPlayer.setDisplay(MediaPlayer.java:795)
                      at yuyu.mymedia.Surfaceview.onCreate(Surfaceview.java:66)
                      at android.app.Activity.performCreate(Activity.java:6912)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008) 
                      at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6688) 

然后调试的时候,当运行到sv.setOnTouchListener(new View.OnTouchListener()这句以及后面的mp,handle等,会出现没有sv,mp,这些的实例,十分不理解,怎么会没有实例?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回覆(2)
迷茫

會不會是surfaceHolder還沒準備好的時候,就呼叫了MediaPlayer的start()方法。

Ty80

自己頂一下,怎麼沒有人。 。 。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板