要在Android模拟器中播放远程URL的mp4视频,您可以使用以下步骤:
build.gradle
文件中,添加以下依赖项以使用ExoPlayer库播放视频:dependencies {
implementation 'com.google.android.exoplayer:exoplayer:2.X.X'
}
activity_main.xml
文件中,添加一个SimpleExoPlayerView
控件来承载视频播放器:<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="@+id/player_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
MainActivity.java
文件中,初始化ExoPlayer并指定远程URL的mp4视频:import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.ui.SimpleExoPlayerView;
public class MainActivity extends AppCompatActivity {
private SimpleExoPlayer player;
private SimpleExoPlayerView playerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playerView = findViewById(R.id.player_view);
player = ExoPlayerFactory.newSimpleInstance(this);
playerView.setPlayer(player);
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "AppName"));
String videoUrl = "https://example.com/video.mp4";
MediaSource videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(videoUrl));
player.prepare(videoSource);
player.setPlayWhenReady(true);
}
@Override
protected void onDestroy() {
super.onDestroy();
player.release();
}
}
注意:请确保将https://example.com/video.mp4
替换为您自己的远程URL。
推荐的腾讯云相关产品:
这些产品可以帮助您更好地处理和播放音视频内容,以提供更好的用户体验。
领取专属 10元无门槛券
手把手带您无忧上云