Android Studio上手,基于VideoView的本地文件及流媒体播放器
既然是第一个Android程序。少不了要Hello World。
1. 新建安卓project
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZm0wNTE3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
2. 输入project名称
3. 选择平台版本号
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZm0wNTE3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
4. 选择一个空的Activity
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZm0wNTE3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
5. 定制自己的Activity
点击Finish后,便生成了可以直接执行的Hello World程序。
以下開始讨论如何使这个仅仅能打印Hello World的程序可以播放本地和网络视频。
此处附上功能文件夹结构:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZm0wNTE3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
6. 布局文件
首先须要又一次布局。
设计器的设计结果是保存在“activity_video_view_demo.xml”这个XML文件里的。所以,略微花一点时间看下这个XML文件。就非常easy看懂。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".VideoViewDemo"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="= Stream Player ="
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textIsSelectable="false"
android:layout_alignParentBottom="false"
android:gravity="center_horizontal" /> <EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/url"
android:layout_below="@+id/textView"
android:layout_alignParentTop="false"
android:layout_alignParentLeft="true"
android:text="rtsp://ipaddr:port/domain"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView" /> <RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/url"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="false"> <RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stream"
android:id="@+id/radioButtonStream"
android:layout_below="@+id/url"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false"
android:layout_alignBottom="@+id/start_play" /> <RadioButton
android:layout_width="wrap_content"
android:layout_height="51dp"
android:text="File"
android:id="@+id/radioButtonFile"
android:checked="false"
android:layout_alignBottom="@+id/radioButtonStream"
android:layout_toRightOf="@+id/radioButtonStream"
android:layout_below="@+id/url" />
</RadioGroup> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PLAY"
android:id="@+id/start_play"
android:layout_below="@+id/url"
android:layout_alignRight="@+id/url"
android:layout_alignEnd="@+id/url"
android:layout_toRightOf="@+id/radioGroup1"
android:layout_toEndOf="@+id/radioGroup1" /> <VideoView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rtsp_player"
android:layout_below="@+id/start_play"
android:layout_alignRight="@+id/url"
android:layout_alignEnd="@+id/url" /> </RelativeLayout>
布局设计器效果:
对比XML文件,最外层是一个关系布局。里面依次是TextView(用于显示标题);EditText(用于输入文件名称或流媒体的URL)。一个RadioGroup。当中包括两个RadioButton(用于区分是区网络流还是读本地文件);一个Button(单击開始播放)。一个VideoView(视频播放区)。
当中的layout_width能够是wrap_content(依据内容自适应)。fill_parent(填充整个外部空间),match_parent(依据外部空间自适应),这个当视频播放区加载视频后会看出区别。android:id这个属性很重要。是Java程序中调用布局控件的关键。
7. 源代码
package com.example.chenth.videoviewdemo; import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.VideoView; public class VideoViewDemo extends Activity {
/** Called when the activity is first created. */ Button playButton ;
VideoView videoView ;
EditText rtspUrl ;
RadioButton radioStream;
RadioButton radioFile; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_view_demo); rtspUrl = (EditText)this.findViewById(R.id.url);
playButton = (Button)this.findViewById(R.id.start_play);
radioStream = (RadioButton)this.findViewById(R.id.radioButtonStream);
radioFile = (RadioButton)this.findViewById(R.id.radioButtonFile); playButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
if (radioStream.isChecked()) {
PlayRtspStream(rtspUrl.getEditableText().toString());
}
else if (radioFile.isChecked()){
PlayLocalFile(rtspUrl.getEditableText().toString());
}
}
}); videoView = (VideoView)this.findViewById(R.id.rtsp_player); } //play rtsp stream
private void PlayRtspStream(String rtspUrl){
videoView.setVideoURI(Uri.parse(rtspUrl));
videoView.requestFocus();
videoView.start();
} //play rtsp stream
private void PlayLocalFile(String filePath){
videoView.setVideoPath(Environment.getExternalStorageDirectory() + "/" + filePath);
videoView.requestFocus();
videoView.start();
}
}
在on鞥的Create函数中依据ID从布局中获取控件,当playButton点击时依据RadioButton的选择(取网络流还是播放本地文件)调用不同的函数。(主要是用到了Android原生的VideoView。而这个控件播放网络流使用 videoView.setVideoURI()函数,播放本地文件使用setVideoPath()函数)。
8. 添加权限
在调试这个程序的时候,总是出现“Can't Play This Video”或者“无法播放该视频”的错误。我也为此花费了非常多时间。最后发现是由于没有给这个APP赋予訪问网络和本地文件的权限。
权限配置在AndroidManifest.xml这个文件里。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chenth.videoviewdemo" > <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".VideoViewDemo"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
注意"uses-permission"的两行。分别表示该应用须要訪问网络,以及读写本地文件,这两个权限相应了播放网络流和本地文件。
加了这两行以后。打包安装时,就能够看到APP想Android系统申请权限的提示了。
9. 在模拟器上调试
点击Run app,会出现选择设备的窗体,没有现成设备的话就新增一个。
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZm0wNTE3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZm0wNTE3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
由于我不知道模拟器相应的根文件夹在PC的什么位置,所以无法在模拟器中直接測试播放本地文件。
另外。在模拟器中測试播放网络流也不成功,推測应该是模拟器权限未设置好。所以,直接进入最后一步吧。
10. 打包公布
点击菜单条的Build -> Generate Signed APK。第一次使用时会要求创建一个Key,就创建吧。然后一路Next 就能够了。最后把生成的apk文件复制到手机,安装。搞定。
最后上手机截屏效果:一张是播放RTSP实时流,还有一张是我家小盆友的录像。
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZm0wNTE3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
11. 附:Android原生VideoView的播放缓冲大小好像是不能调整的。导致网络RTSP流的延时达到了9-10秒。各位看官假设知道什么好的办法。欢迎留言告知,谢谢!
Android Studio上手,基于VideoView的本地文件及流媒体播放器的更多相关文章
- Android studio中找不到so文件的问题:java.lang.UnsatisfiedLinkError
解决Android studio中找不到so文件的问题:java.lang.UnsatisfiedLinkError 表示我们不编译jni代码,直接从libs里面复制so库 文件路径:app\buil ...
- [osx] android studio下修改avd的hosts文件
1. 启动avd 安装/启动avd就不说啦,可以直接在android studio里面操作的 2. 进入adb目录 当然是打开终端来敲命令啦. cd /Users/birdylee/Library/A ...
- Android studio 使用SVN需要忽略的文件
Android Studio创建的Android项目一般需要忽略 1..idea文件夹 2..gradle文件夹 3.所有的build文件夹 4.所有的.iml文件 5.local.propertie ...
- 【Android Studio安装部署系列】二十六、Android studio录制屏幕并生成gif文件
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 Android Studio自带录制屏幕功能,那么就可以很方便地将手机上的屏幕操作录制成视频.然后借助一些软件或者网站转换成gif文 ...
- Android View转为图片保存为本地文件,异步监听回调操作结果;
把手机上的一个View或ViewGroup转为Bitmap,再把Bitmap保存为.png格式的图片: 由于View转Bitmap.和Bitmap转图片都是耗时操作,(生成一个1M的图片大约500ms ...
- [Android Studio] Android Studio快速定位当前打开的文件在哪个目录(package)下
转载自:http://blog.csdn.net/hyr83960944/article/details/38067499 在Eclipse中有一个很好的功能,就是比如我打开一个AActivity,左 ...
- [Android Studio] Android Studio快速定位当前打开的文件在哪个目录(package)下
转载自:http://blog.csdn.net/hyr83960944/article/details/38067499 在Eclipse中有一个很好的功能,就是比如我打开一个AActivity,左 ...
- Android Studio 2.3.1导出jar文件不能生成release解决办法
升级了AS之后,在项目中的时候,有个需求需要把通过AS导出一个模块,需要以jar的形式导出来,研究了一下,按照网上的描述操作了一遍,不知道是AS版本问题还是自己操作问题,发现使用 ./gradlew ...
- android studio 出现找不到R文件的错误
百度知道: 检查是否编译了项目.Android studio有时候没有编译就会报出没有R文件的错误. 检查带代码中包名是否正确.有时候从其他地方复制代码过来时连带了包名,也会报出R文件找不到. 检查布 ...
随机推荐
- 【2017 Multi-University Training Contest - Team 7】Hard challenge
[Link]:http://acm.hdu.edu.cn/showproblem.php?pid=6127 [Description] 平面上有n个点,每个点有一个价值,每两个点之间都有一条线段,定义 ...
- 大数据- Hive
构建在Hadoop之上的数据仓库,数据计算使用MR,数据存储使用HDFS 由于数据计算使用mapreduce.因此通经常使用于进行离线数据处理 Hive 定义了一种类 SQL 查询语言 ...
- Android 小米盒子游戏手柄按键捕获 - 能获取到的 home 键依旧是个痛
Android 小米盒子游戏手柄按键捕获 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 ...
- 开源课程管理系统(CMS):Moodle
开源课程管理系统(CMS):Moodle 一.总结 1.php开发的cms,可借鉴参考用 二.Moodle(百度) Moodle(Modular Object-Oriented Dynamic Lea ...
- MyBatis学习总结(15)——定制Mybatis自动代码生成的maven插件
==================================================================================================== ...
- 一个开源.net混淆器——ConfuserEx (收藏)
一个开源.net混淆器——ConfuserEx http://yck1509.github.io/ConfuserEx/ 由于项目中要用到.net 混淆器,网上搜寻了很多款,比如Dotfuscator ...
- Altium Designer导入pcb原件之后都是绿的
- [React] Close the menu component when click outside the menu
Most of the time, your components respond to events that occur within the component tree by defining ...
- C语言深度剖析-----函数
认清函数的真面目 函数的意义 面向过程的程序设计 函数声明和定义 函数参数 编写代码的时候,不要编写类似先后调用的代码 f(k,k++) C语言中的顺序点 a--&&a ,& ...
- 每日技术总结:setInterval,setTimeout,文本溢出,小程序,wepy
前言: 项目背景:vue,电商,商品详情页 1.倒计时,倒计到0秒时停止 data () { return { n: 10 } }, created () { let int = setInterva ...