Android:在线视频播放器

77 篇文章 0 订阅


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    >
<TextView  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/filename"
    />
    
    <EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/filename"
    />
    
    <LinearLayout
    android:orientation="horizontal"
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
        <ImageButton
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/play"
         android:id="@+id/playbutton"
         android:onClick="mediaplay"
        />
        <ImageButton
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/pause"
         android:id="@+id/pausebutton"
         android:onClick="mediaplay"
        />
        <ImageButton
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/reset"
         android:id="@+id/resetbutton"
         android:onClick="mediaplay"
        />
        <ImageButton
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/stop"
         android:id="@+id/stopbutton"
         android:onClick="mediaplay"
        />                    
    </LinearLayout>
    
    <SurfaceView
         android:layout_width="fill_parent"
         android:layout_height="240dp"
        android:id="@+id/surfaceView"
        />

</LinearLayout>





public class MainActivity extends Activity {
    private EditText nameText;
    private String path;
    private MediaPlayer mediaPlayer;
    private SurfaceView surfaceView;
    private boolean pause;
    private int position;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mediaPlayer = new MediaPlayer();
        nameText = (EditText) this.findViewById(R.id.filename);
        surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
        //把输送给surfaceView的视频画面,直接显示到屏幕上,不要维持它自身的缓冲区
        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        surfaceView.getHolder().setFixedSize(176, 144);
        surfaceView.getHolder().setKeepScreenOn(true);
        surfaceView.getHolder().addCallback(new SurfaceCallback());

    }
    
    private final class SurfaceCallback implements Callback{
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        }
        public void surfaceCreated(SurfaceHolder holder) {
            if(position>0 && path!=null){
                play(position);
                position = 0;
            }
        }
        public void surfaceDestroyed(SurfaceHolder holder) {
            if(mediaPlayer.isPlaying()){
                position = mediaPlayer.getCurrentPosition();
                mediaPlayer.stop();
            }
        }
    }

    @Override
    protected void onDestroy() {
        mediaPlayer.release();
        mediaPlayer = null;
        super.onDestroy();
    }

    public void mediaplay(View v){
        switch (v.getId()) {
        case R.id.playbutton:
            String filename = nameText.getText().toString();
            if(filename.startsWith("http")){
                path = filename;

                play(0);
            }else{
                File file = new File(Environment.getExternalStorageDirectory(), filename);
                if(file.exists()){
                    path = file.getAbsolutePath();

                    play(0);
                }else{
                    path = null;
                    Toast.makeText(this, R.string.filenoexsit, 1).show();
                }
            }
        
            break;

        case R.id.pausebutton:
            if(mediaPlayer.isPlaying()){
                mediaPlayer.pause();
                pause = true;
            }else{
                if(pause){
                    mediaPlayer.start();
                    pause = false;
                }
            }
            break;
            
        case R.id.resetbutton:
            if(mediaPlayer.isPlaying()){
                mediaPlayer.seekTo(0);
            }else{
                if(path!=null){
                    play(0);
                }
            }
            break;
        case R.id.stopbutton:
            if(mediaPlayer.isPlaying()){
                mediaPlayer.stop();
            }
            break;
        }
    }

    private void play(int position) {
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(path);
            mediaPlayer.setDisplay(surfaceView.getHolder());
            mediaPlayer.prepare();//缓冲
            mediaPlayer.setOnPreparedListener(new PrepareListener(position));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private final class PrepareListener implements OnPreparedListener{
        private int position;
        
        public PrepareListener(int position) {
             this.position = position;
        }

        public void onPrepared(MediaPlayer mp) {
            mediaPlayer.start();//播放视频
            if(position>0) mediaPlayer.seekTo(position);
        }
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Android Studio 列表视频播放器代码示例: 1. 在布局文件中,添加一个 RecyclerView 控件: ``` <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 2. 创建一个 VideoAdapter 类来绑定数据和视图: ``` public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VideoViewHolder> { private List<String> videoUrls; public VideoAdapter(List<String> videoUrls) { this.videoUrls = videoUrls; } @NonNull @Override public VideoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video, parent, false); return new VideoViewHolder(view); } @Override public void onBindViewHolder(@NonNull VideoViewHolder holder, int position) { String videoUrl = videoUrls.get(position); holder.videoView.setVideoPath(videoUrl); } @Override public int getItemCount() { return videoUrls.size(); } public class VideoViewHolder extends RecyclerView.ViewHolder { VideoView videoView; Button playButton; public VideoViewHolder(@NonNull View itemView) { super(itemView); videoView = itemView.findViewById(R.id.videoView); playButton = itemView.findViewById(R.id.playButton); playButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (videoView.isPlaying()) { videoView.pause(); playButton.setText("Play"); } else { videoView.start(); playButton.setText("Pause"); } } }); videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { playButton.setText("Play"); } }); } } } ``` 3. 在布局文件中,创建一个 item_video.xml 布局文件,用于显示每个视频项: ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="200dp"> <VideoView android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="match_parent" /> <Button android:id="@+id/playButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Play" android:layout_centerInParent="true" /> </RelativeLayout> ``` 4. 在代码中,初始化 RecyclerView 和 VideoAdapter,并设置布局管理器和适配器: ``` List<String> videoUrls = new ArrayList<>(); videoUrls.add("your_video_url_1_here"); videoUrls.add("your_video_url_2_here"); // add more video urls as needed RecyclerView recyclerView = findViewById(R.id.recyclerView); recyclerView.setLayoutManager(new LinearLayoutManager(this)); VideoAdapter videoAdapter = new VideoAdapter(videoUrls); recyclerView.setAdapter(videoAdapter); ``` 注意:在使用 RecyclerView 播放在线视频时,需要在 Manifest 文件中添加网络权限: ``` <uses-permission android:name="android.permission.INTERNET" /> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值