码迷,mamicode.com
首页 > 其他好文 > 详细

网络获取视频

时间:2017-11-23 10:54:42      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:star   async   surface   top   tar   引擎   用户   null   gre   

布局:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:orientation="vertical"
 8     tools:context="com.example.administrator.zhoukao03.MainActivity">
 9 
10     <LinearLayout
11         android:orientation="horizontal"
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content">
14 
15         <Button
16             android:id="@+id/play_btn"
17             android:layout_width="wrap_content"
18             android:layout_height="wrap_content"
19             android:text="播放"/>
20 
21         <Button
22             android:id="@+id/stop_btn"
23             android:layout_width="wrap_content"
24             android:layout_height="wrap_content"
25             android:text="暂停/播放"/>
26 
27 
28     </LinearLayout>
29 
30     <com.example.administrator.zhoukao03.MySurfaceView
31         android:id="@+id/surfaceview"
32         android:layout_width="match_parent"
33         android:layout_height="200dp" />
34 
35     <Button
36         android:id="@+id/sd_btn1"
37         android:layout_width="wrap_content"
38         android:layout_height="wrap_content"
39         android:text="1倍"/>
40     <Button
41         android:id="@+id/sd_btn2"
42         android:layout_width="wrap_content"
43         android:layout_height="wrap_content"
44         android:text="2倍"/>
45     <Button
46         android:id="@+id/sd_btn3"
47         android:layout_width="wrap_content"
48         android:layout_height="wrap_content"
49         android:text="3倍"/>
50 
51 </LinearLayout>

Activity:

 1 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 2 
 3     private String url = "http://baobab.kaiyanapp.com/api/v1/playUrl?vid=61069&editionType=low&source=ucloud";
 4     private Button play_btn;
 5     private Button stop_btn;
 6     private MySurfaceView surfaceview;
 7     private Button sd_btn1;
 8     private Button sd_btn2;
 9     private Button sd_btn3;
10 
11     @Override
12     protected void onCreate(Bundle savedInstanceState) {
13         super.onCreate(savedInstanceState);
14         setContentView(R.layout.activity_main);
15         initView();
16     }
17 
18     private void initView() {
19         play_btn = (Button) findViewById(R.id.play_btn);
20         stop_btn = (Button) findViewById(R.id.stop_btn);
21         surfaceview = (MySurfaceView) findViewById(R.id.surfaceview);
22 
23         //设置分辨率
24         surfaceview.setPivotY(320);
25         surfaceview.setPivotX(320);
26 
27 
28         play_btn.setOnClickListener(this);
29         stop_btn.setOnClickListener(this);
30         sd_btn1 = (Button) findViewById(R.id.sd_btn1);
31         sd_btn1.setOnClickListener(this);
32         sd_btn2 = (Button) findViewById(R.id.sd_btn2);
33         sd_btn2.setOnClickListener(this);
34         sd_btn3 = (Button) findViewById(R.id.sd_btn3);
35         sd_btn3.setOnClickListener(this);
36     }
37 
38     @Override
39     public void onClick(View v) {
40         switch (v.getId()) {
41             case R.id.play_btn:
42                 //点击播放按钮
43                 surfaceview.playVideo(url);
44 
45                 break;
46             case R.id.stop_btn:
47                 surfaceview.stopVideo();
48 
49                 break;
50 
51             case R.id.sd_btn1:
52                 surfaceview.setprogress(100);
53 
54 
55                 break;
56             case R.id.sd_btn2:
57                 surfaceview.setprogress(200);
58 
59 
60 
61                 break;
62             case R.id.sd_btn3:
63                 surfaceview.setprogress(300);
64 
65 
66                 break;
67         }
68     }
69 }

MySurfaceView类:

 1 public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback, MediaPlayer.OnCompletionListener {
 2 
 3     private SurfaceHolder holder;
 4     private MediaPlayer mediaPlayer;
 5 
 6     public MySurfaceView(Context context, AttributeSet attrs) {
 7         super(context, attrs);
 8 
 9 
10         init();
11     }
12 
13     //快进
14     public void setprogress(int progress){
15         int currentPosition = mediaPlayer.getCurrentPosition();
16         mediaPlayer.seekTo(currentPosition+progress);
17         Toast.makeText(getContext(),"快进"+progress,Toast.LENGTH_SHORT).show();
18     }
19 
20     //暂停/播放方法
21     public void stopVideo(){
22         if (mediaPlayer.isPlaying()){
23             mediaPlayer.pause();
24         }else {
25             mediaPlayer.start();
26         }
27     }
28 
29 
30 
31     //播放视频方法
32     public void playVideo(String path){
33         if (mediaPlayer==null){
34             mediaPlayer=new MediaPlayer();
35             //设置surfaceview不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前
36             mediaPlayer.setOnCompletionListener(this);
37         }
38 
39         try {
40             mediaPlayer.reset();
41             mediaPlayer.setDataSource(path);
42             mediaPlayer.setDisplay(holder);
43             mediaPlayer.prepareAsync();
44         } catch (IOException e) {
45             e.printStackTrace();
46         }
47     }
48 
49     private void init() {
50         holder=this.getHolder();
51 
52         //重写SurfaceHolder.Callback方法
53         holder.addCallback(this);
54     }
55 
56     @Override
57     public void surfaceCreated(SurfaceHolder surfaceHolder) {
58 
59     }
60 
61     @Override
62     public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
63 
64     }
65 
66     @Override
67     public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
68         if (mediaPlayer!=null){
69             mediaPlayer.release();
70             mediaPlayer=null;
71         }
72 
73     }
74 
75 
76     //缓冲完成播放
77     @Override
78     public void onCompletion(MediaPlayer mediaPlayer) {
79         mediaPlayer.start();
80     }
81 }

权限:

  <uses-permission android:name="android.permission.INTERNET">

</uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

 

网络获取视频

标签:star   async   surface   top   tar   引擎   用户   null   gre   

原文地址:http://www.cnblogs.com/SongYongQian/p/7883359.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!