播放本地视屏

本文介绍了一个简单的Android应用案例,通过使用Intent和Uri组件调用Android系统自带的播放器来播放本地存储的视频文件。该应用展示了如何从SD卡中获取视频路径,并设置正确的MIME类型以确保视频可以被正确播放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >

    <Button
        android:id="@+id/but"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击调取系统的播放器进行播放视频"
        android:onClick="setdata"
        />

    </LinearLayout>

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.VideoView;

import com.bwie.zhoujianhang.quarter.R;



/**
 * Created by 杨运泽.
 */

public class BenDiFragment extends Fragment {


    private View view;
    private Button btn;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.bendifragment, null);
           initViews();
        return view;
    }

    //点击调取播放器进行播放视频
    public void initViews(){
        btn = view.findViewById(R.id.but);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开系统的播放器   Action  Intent.ACTION_VIEW 打开系统播放器的指令
                Intent intent = new Intent(Intent.ACTION_VIEW);
                //得到sdcard下的视频
                //Environment.getExternalStorageDirectory() 得到sdcard的根目录
                Uri data = Uri.parse(Environment.getExternalStorageDirectory()+"/minion_09.mp4");
                //交给intent进行加载播放  设置类型 1. 视频  2.类型
                intent.setDataAndType(data,"video/*");
                //播放
                startActivity(intent);
            }
        });

    }

    public static BenDiFragment create(String title) {
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        BenDiFragment benDiFragment = new BenDiFragment();
        benDiFragment.setArguments(bundle);
        return benDiFragment;
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值