Android exoplayer视频在模拟器上播放但在设备上播放

时间:2017-05-28 12:39:54

标签: android video video-streaming

我正在尝试使用简单的exoplayer在我的服务器上播放视频。视频在模拟器上播放正常,但无法在设备上播放。我在日志中遇到以下错误

E/ExoPlayerImplInternal: Source error
com.google.android.exoplayer2.upstream.HttpDataSource$InvalidResponseCodeException: Response code: 404
    at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.open(DefaultHttpDataSource.java:211)
    at com.google.android.exoplayer2.source.ExtractorMediaPeriod$ExtractingLoadable.load(ExtractorMediaPeriod.java:640)
    at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:295)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    at java.lang.Thread.run(Thread.java:818)

以前我收到有关网络超时的错误 有趣的是,视频在模拟器上播放效果很好。这是我的视频播放器活动代码 -

public class Videoplayer extends AppCompatActivity {

private SQLiteHandler db;
public Integer ivid;
public String title;
public String topic;
public String subject;
public String description;
public String url;
public String thumb;
public SimpleExoPlayerView playerView;
public SimpleExoPlayer player;
private long playbackPosition;
private int currentWindow;
private boolean playWhenReady = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_videoplayer);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    db = new SQLiteHandler(this);

    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    Integer vid = extras.getInt("vid");
    Log.d("vid", vid.toString());
    ArrayList<HashMap<String, String>> vid_details = db.getVideoDetails();

    for(int i = 0; i < vid_details.size(); i++){
        Log.d("loop ", vid_details.get(i).get("subject")+" "+vid_details.get(i).get("topic"));
        ivid = Integer.parseInt(vid_details.get(i).get("vid"));
        if(ivid == vid){
            title = vid_details.get(i).get("title");
            topic = vid_details.get(i).get("topic");
            subject = vid_details.get(i).get("subject");
            description = vid_details.get(i).get("description");
            url = "http://admin.ganeshanclasses.in/"+vid_details.get(i).get("url");
            thumb = vid_details.get(i).get("thumb");

            Log.d("url", url);
        }
    }

    TextView tvh = (TextView)findViewById(R.id.vidtitle);
    tvh.setText(title);

    TextView tvsh = (TextView)findViewById(R.id.vidsubtop);
    tvsh.setText(topic);

    TextView tvdsc = (TextView)findViewById(R.id.viddesc);
    tvdsc.setText(description);

    playerView = (SimpleExoPlayerView)findViewById(R.id.videoView);
}

@Override
public void onStart() {
    super.onStart();
    if (Util.SDK_INT > 23) {
        initializePlayer();
    }
}

@Override
public void onResume() {
    super.onResume();
    //hideSystemUi();
    if ((Util.SDK_INT <= 23 || player == null)) {
        initializePlayer();
    }
}

@Override
public void onPause() {
    super.onPause();
    if (Util.SDK_INT <= 23) {
        releasePlayer();
    }
}

@Override
public void onStop() {
    super.onStop();
    if (Util.SDK_INT > 23) {
        releasePlayer();
    }
}

private void initializePlayer() {
    if (player == null) {
        player = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(this),
                new DefaultTrackSelector(), new DefaultLoadControl());
        playerView.setPlayer(player);
        player.setPlayWhenReady(playWhenReady);
        player.seekTo(currentWindow, playbackPosition);
    }
    MediaSource mediaSource = buildMediaSource(Uri.parse(url));
    player.prepare(mediaSource, true, false);
}

private void releasePlayer() {
    if (player != null) {
        playbackPosition = player.getCurrentPosition();
        currentWindow = player.getCurrentWindowIndex();
        playWhenReady = player.getPlayWhenReady();
        player.release();
        player = null;
    }
}

private MediaSource buildMediaSource(Uri uri) {
    return new ExtractorMediaSource(uri, new DefaultHttpDataSourceFactory("ua"),
            new DefaultExtractorsFactory(), null, null);
}

@SuppressLint("InlinedApi")
private void hideSystemUi() {
    playerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}

}

  

编辑:我想这不是一个exoplayer问题,因为默认的android videoview正在发生同样的事情。

0 个答案:

没有答案