为了账号安全,请及时绑定邮箱和手机立即绑定

自己制作一个视频播放器,遇到问题,用的是内置surfaceview类,具体看代码!

自己制作一个视频播放器,遇到问题,用的是内置surfaceview类,具体看代码!

千巷猫影 2019-02-21 13:45:06
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 回答

?
慕哥9229398

TA贡献1877条经验 获得超6个赞

会不会是surfaceHolder还没准备好的时候,就调用了MediaPlayer的start()方法。

查看完整回答
反对 回复 2019-03-01
?
皈依舞

TA贡献1851条经验 获得超3个赞

自己顶一下,怎么没有人。。。

查看完整回答
反对 回复 2019-03-01
  • 2 回答
  • 0 关注
  • 615 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信