【问题标题】:ExpoPlayer couldn't fetch urlExpoPlayer 无法获取网址
【发布时间】:2023-09-03 08:56:01
【问题描述】:

我的view 正在显示,但链接没有被提取和播放。 Internet 的权限也已添加到 Manifest 文件中。 activity_main 也已正确放置。

package com.example.exoplayer;
        
        import androidx.appcompat.app.AppCompatActivity;
        
        import android.net.Uri;
        import android.os.Bundle;
        
        import com.google.android.exoplayer2.ExoPlaybackException;
        import com.google.android.exoplayer2.ExoPlayerFactory;
        import com.google.android.exoplayer2.SimpleExoPlayer;
        import com.google.android.exoplayer2.source.ExtractorMediaSource;
        import com.google.android.exoplayer2.source.MediaSource;
        import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
        import com.google.android.exoplayer2.ui.PlayerView;
        import com.google.android.exoplayer2.upstream.DataSource;
        import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
        import com.google.android.exoplayer2.util.Util;
        
        public class MainActivity extends AppCompatActivity {
            private PlayerView playerView;
            private SimpleExoPlayer player;
        
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                playerView=findViewById(R.id.playerView);
        
            }
        
            @Override
            protected void onStart() {
                super.onStart();
                player= ExoPlayerFactory.newSimpleInstance(this,new DefaultTrackSelector());
                playerView.setPlayer(player);
                DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
                        Util.getUserAgent(this,getString(R.string.app_name)));
                MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory)
                      .createMediaSource(Uri.parse("http://buildappswithpaulo.com/videos/outro_music.mp4"));
                    player.prepare(videoSource);
            }
        }


  

【问题讨论】:

    标签: java android exoplayer android-video-player android-developer-api


    【解决方案1】:

    请按照以下步骤进行核对或复制粘贴

    1. 然后在您的清单文件中添加互联网权限以获取视频 URL

    manifest.xml

    <manifest ...>
      <uses-permission android:name="android.permission.INTERNET"/>
      <application
        android:usesCleartextTraffic="true"
        ...>
    
        ...
    
      </application>
    </manifest>
    
    //app/build.gradle
    apply plugin: 'com.android.application'
    
    android {
    ...
    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
    }
    
    dependencies {
    ...
    implementation 'com.google.android.exoplayer:exoplayer:2.10.4'
    }
    
      protected void onCreate(Bundle savedInstanceState) {
        ...
    
        Context ctx =this;
        String CONTENT_URL = "http://buildappswithpaulo.com/videos/outro_music.mp4";
        int playerID=R.id.pv_main;
        int appNameStringRes = R.string.app_name;
        startPlayingVideo(this,CONTENT_URL,playerID,appNameStringRes);
    
    
    }
    
    //
    private void startPlayingVideo(Context ctx , String CONTENT_URL, int playerID, String appNameRes) {
    
        PlayerView pvMain = ctx.findViewById(playerID);
    
        //BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        //TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);        
        //TrackSelector trackSelectorDef = new DefaultTrackSelector(videoTrackSelectionFactory);
        TrackSelector trackSelectorDef = new DefaultTrackSelector();
    
        SimpleExoPlayer absPlayerInternal = ExoPlayerFactory.newSimpleInstance(ctx, trackSelectorDef);
    
        String userAgent = Util.getUserAgent(ctx, ctx.getString(appNameRes));
    
        DefaultDataSourceFactory defdataSourceFactory = new DefaultDataSourceFactory(ctx,userAgent);
        Uri uriOfContentUrl = Uri.parse(CONTENT_URL);
        MediaSource mediaSource = new ProgressiveMediaSource.Factory(defdataSourceFactory).createMediaSource(uriOfContentUrl);
    
        absPlayerInternal.prepare(mediaSource);
        absPlayerInternal.setPlayWhenReady(true);
    
        pvMain.setPlayer(absPlayerInternal);
    
    }
    
    private void stopPlayer(PlayerView pv,SimpleExoPlayer absPlayer){
        pv.setPlayer(null);
        absPlayer.release();
        absPlayer = null;
    }
    

    【讨论】:

    • schemas.android.com/apk/res/android" xmlns:app="schemas.android.com/apk/res-auto" xmlns:tools=" schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
    • 我在MainActivity.class 出现错误 -: 在下面这一行 ->>>>>> `PlayerView pvMain = ctx.findViewById(playerID);`
    最近更新 更多