赞
踩
本文的目的是记录简单创建一个基于Exoplayer的视频播放软件的最小代码单元,并生成可以使用的APK应用软件。通过本文的代码,能够使用exoplayer播放一个SD卡的本地文件。
本文使用的代码:
Github地址
代码基于Android Studio创建的工程,Android 5.0版本 API21(项目需要所以版本较老)。
编译:直接使用菜单栏Run
运行MainActivity,然后使用Build APKs
生成开发者调试的debug版本的APK。随后,将apk推到需要测试的安卓系统板子上,测试APK是否好用。
MainActivity。
Gradle下面的文件。
plugins { id 'com.android.application' } android { namespace 'com.example.exoplayer2example' compileSdk 32 defaultConfig { applicationId "com.example.exoplayer2example" minSdk 21 targetSdk 32 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } buildFeatures { viewBinding true } } dependencies { implementation 'com.google.android.exoplayer:exoplayer:r2.0.0' implementation 'androidx.appcompat:appcompat:1.5.1' implementation 'com.google.android.material:material:1.6.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.navigation:navigation-fragment:2.5.2' implementation 'androidx.navigation:navigation-ui:2.5.2' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' }
pluginManagement { repositories { gradlePluginPortal() google() mavenCentral() } } dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() jcenter() } } rootProject.name = "Exoplayer2Example" include ':app'
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" tools:targetApi="31"> <activity android:name=".MainActivity" android:exported="true" android:label="@string/app_name" android:theme="@style/AppTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="android.app.lib_name" android:value="" /> </activity> </application> </manifest>
package com.example.exoplayer2example; import android.annotation.SuppressLint; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.util.Log; import androidx.appcompat.app.AppCompatActivity; import com.google.android.exoplayer2.DefaultLoadControl; import com.google.android.exoplayer2.ExoPlaybackException; import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.ExoPlayerFactory; import com.google.android.exoplayer2.LoadControl; import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory; import com.google.android.exoplayer2.extractor.ExtractorsFactory; import com.google.android.exoplayer2.source.ExtractorMediaSource; import com.google.android.exoplayer2.source.LoopingMediaSource; import com.google.android.exoplayer2.source.MediaSource; import com.google.android.exoplayer2.source.hls.HlsMediaSource; import com.google.android.exoplayer2.trackselection.AdaptiveVideoTrackSelection; import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; import com.google.android.exoplayer2.trackselection.TrackSelection; import com.google.android.exoplayer2.trackselection.TrackSelector; import com.google.android.exoplayer2.ui.SimpleExoPlayerView; import com.google.android.exoplayer2.upstream.BandwidthMeter; import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import com.google.android.exoplayer2.util.Util; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private SimpleExoPlayerView simpleExoPlayerView; private SimpleExoPlayer player; private ExoPlayer.EventListener exoPlayerEventListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v(TAG,"starting..."); setContentView(R.layout.activity_main); // 1. Create a default TrackSelector Handler mainHandler = new Handler(); BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveVideoTrackSelection.Factory(bandwidthMeter); TrackSelector trackSelector = new DefaultTrackSelector(mainHandler, videoTrackSelectionFactory); // 2. Create a default LoadControl LoadControl loadControl = new DefaultLoadControl(); // 3. Create the player Log.v(TAG,"create the player"); player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl); //4. Create view simpleExoPlayerView = new SimpleExoPlayerView(this); simpleExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.player_view); // Set media controller simpleExoPlayerView.setUseController(true); simpleExoPlayerView.requestFocus(); // Bind the player to the view. simpleExoPlayerView.setPlayer(player); //6. building cam live stream link: Log.v(TAG,"parse url"); @SuppressLint("SdCardPath") String myUrl = "/data/sample_Trim.mp4"; Uri mp4VideoUri = Uri.parse(myUrl); Log.v(TAG, String.valueOf(mp4VideoUri)); Log.v(TAG,"parse url end"); //Measures bandwidth during playback. Can be null if not required. DefaultBandwidthMeter bandwidthMeterA = new DefaultBandwidthMeter(); //Produces DataSource instances through which media data is loaded. //DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "exoplayer2example"), bandwidthMeterA); DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "exoplayer2example"), bandwidthMeterA); //Produces Extractor instances for parsing the media data. ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory(); //This is the MediaSource representing the media to be played: //FOR SD CARD SOURCE: MediaSource videoSource = new ExtractorMediaSource(mp4VideoUri, dataSourceFactory, extractorsFactory, null, null); //FOR LIVESTREAM LINK: //MediaSource videoSource = new HlsMediaSource(mp4VideoUri, dataSourceFactory, 1, null, null); final LoopingMediaSource loopingSource = new LoopingMediaSource(videoSource); // Prepare the player with the source. player.setPlayWhenReady(true); player.prepare(loopingSource); player.addListener(new ExoPlayer.EventListener() { @Override public void onLoadingChanged(boolean isLoading) { Log.v(TAG,"Listener-onLoadingChanged..."); } @Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { Log.v(TAG,"Listener-onPlayerStateChanged..."); } @Override public void onTimelineChanged(Timeline timeline, Object manifest) { Log.v(TAG,"Listener-onTimelineChanged..."); } @Override public void onPlayerError(ExoPlaybackException error) { Log.v(TAG,"Listener-onPlayerError..."); player.stop(); player.prepare(loopingSource); player.setPlayWhenReady(true); } @Override public void onPositionDiscontinuity() { Log.v(TAG,"Listener-onPositionDiscontinuity..."); } }); }//End of onCreate @Override protected void onStop() { super.onStop(); Log.v(TAG,"onStop()..."); } @Override protected void onStart() { super.onStart(); Log.v(TAG,"onStart()..."); } @Override protected void onResume() { super.onResume(); Log.v(TAG,"onResume()..."); } @Override protected void onPause() { super.onPause(); Log.v(TAG,"onPause()..."); } @Override protected void onDestroy() { super.onDestroy(); Log.v(TAG,"onDestroy()..."); player.release(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。