Android学习之MediaPlayer视频播放
来源:互联网 发布:mac如何关闭dashboard 编辑:程序博客网 时间:2024/05/17 06:23
ActivityMain.java
package com.example.playvdieo;import android.content.pm.PackageManager;import android.os.Environment;import android.support.annotation.NonNull;import android.support.v4.app.ActivityCompat;import android.support.v4.content.ContextCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;import android.widget.VideoView;import android.Manifest;import java.io.File;public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button play; private Button pause; private Button stop; private VideoView videoView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); play = (Button)findViewById(R.id.play); play.setOnClickListener(this); pause = (Button)findViewById(R.id.pause); pause.setOnClickListener(this); stop = (Button)findViewById(R.id.stop); stop.setOnClickListener(this); videoView = (VideoView)findViewById(R.id.video_view); //判断有无授权,有就直接进行资源初始化,没有就向用户请求授权 if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); }else { initVideoSource();//初始化MediaPlayer } } @Override public void onClick(View view) { switch (view.getId()){ case R.id.play: if (!videoView.isPlaying()){ videoView.start(); } break; case R.id.pause: if (videoView.isPlaying()){// videoView.pause(); videoView.suspend(); } break; case R.id.stop: if (videoView.isPlaying()){ videoView.resume();//重新播放 } } } public void initVideoSource(){ File file = new File(Environment.getExternalStorageDirectory(),"video.mp4"); videoView.setVideoPath(file.getPath()); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode){ case 1: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ initVideoSource(); }else { Toast.makeText(this,"拒绝授权将无法使用程序!",Toast.LENGTH_SHORT).show(); finish(); } break; default: break; } } @Override protected void onDestroy() { super.onDestroy(); if (videoView != null){ videoView.suspend(); } }}
布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/play" android:text="播放"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/pause" android:text="暂停"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/stop" android:text="停止"/> </LinearLayout> <VideoView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/video_view"/></LinearLayout>
注册AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.playvdieo"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application></manifest>
阅读全文
0 0
- Android视频播放器之学习MediaPlayer
- Android学习之MediaPlayer视频播放
- Android视频播放之MediaPlayer
- Android学习二、MediaPlayer播放视频
- Android学习二、MediaPlayer播放视频
- Android学习二、MediaPlayer播放视频
- Android 视频播放之MediaPlayer+SurfaceView
- Android播放视频之MediaPlayer和SurfaceView
- Android MediaPlayer之音/视频播放
- MediaPlayer之视频播放
- Android 视频播放 MediaPlayer
- Android mediaPlayer 播放视频
- Android MediaPlayer播放视频
- Android音视频之MediaPlayer音视频播放
- Android学习之MediaPlayer 音频播放器
- android mediaplayer 播放 视频 【转】
- android Mediaplayer 播放音视频
- Android视频播放器mediaplayer
- 神经网络激活函数和损失函数
- 高效算法设计_算法分析初步(最大连续和)
- HDU 5542 The Battle of Chibi(树状数组+dp)
- 能力提升路径和层次
- Max Factor(技巧题)
- Android学习之MediaPlayer视频播放
- 机载点云单木分割
- LinkedList和ArrayList区别
- 知识汇总
- 基于ASP.NET WebAPI OWIN实现Self-Host项目实战
- HDU 6098 Inversion
- QT之细节集锦
- Docker安装及加速配置及基本操作
- Unity之LeapMotion开发