您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
72_如何播放在线的视频&渐进式视频的制作
发布时间:2021-03-13 10:02:09编辑:雪饮阅读()
昨天实现了音频mp3播放器的异步方式。今天我们来实现在线视频的播放,其实前面已经实现过视频播放,但是仅仅是加载sdcard中的视频进行播放,是本地视频播放。
那么实现网络视频播放,就不需要sdcard权限了,但是又多出了两个关于网络的配置,同样是要在清单文件中的。则清单文件AndroidManifest.xml如:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.playonline">
<uses-permission android:name="android.permission.INTERNET"/>
<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/Theme.Playonline"
android:usesCleartextTraffic="true"
>
<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>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.playonline">
<uses-permission android:name="android.permission.INTERNET"/>
<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/Theme.Playonline"
android:usesCleartextTraffic="true"
>
<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>
然后布局就一个输入框和一个播放按钮即可,输入框用于输入网络视频地址,对于网络视频地址中的视频必须是流视频,渐进式的。这里我直接用之前javaweb项目中放的那个output.mp4了。所幸这个是可以支持在线播放的。如果某些视频不支持流视频,渐进式。则可以用quicktime软件处理为流视频(一般为h.264)渐进式视频。
那么这里就不多啰嗦了,且看布局如:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:text="http://192.168.43.71:8080/javaweb/output.mp4"
android:layout_height="wrap_content" >
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="play"
android:text="播放" />
<SurfaceView
android:id="@+id/sv"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
那么最后就是MainActivity.java主程序咯:
package com.example.playonline;
import androidx.appcompat.app.AppCompatActivity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText et_path;
SurfaceView sv;
SurfaceHolder holder;
MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_path = (EditText) this.findViewById(R.id.editText1);
sv = (SurfaceView) this.findViewById(R.id.sv);
holder = sv.getHolder();
/* 下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前 */
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void play(View view){
try {
String path = et_path.getText().toString().trim();
player = new MediaPlayer();
player.setDataSource(path);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDisplay(holder);
player.prepareAsync();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
} catch (Exception e) {
System.out.println("error ");
e.printStackTrace();
}
}
}
然后部署到设备如(这里就不用继续勾选权限了,因为清单文件中没有用到要勾选的权限):
关键字词:android,在线视频,渐进式视频