exoplayer2 ts文件未在android框中播放

时间:2018-10-08 22:34:24

标签: java android live-streaming exoplayer m3u

使用exoplayer 2制作iptv频道应用程序。一切正常,视频可以在手机上播放,但是当我在android上运行应用程序时,播放器处于缓冲模式,无法播放,并且应用程序崩溃。 我检查了exoplayer的常见问题,他们说
当要求播放ExoPlayer缺少AUD或IDR关键帧的MPEG-TS文件时,ExoPlayer似乎停留在缓冲状态。如果需要播放此类文件,则可以分别使用FLAG_DETECT_ACCESS_UNITS和FLAG_ALLOW_NON_IDR_KEYFRAMES进行播放。可以使用setTsExtractorFlags在DefaultExtractorsFactory上设置这些标志。
您能帮我如何在Android框中完美添加标记或运行应用吗?
这是代码:

public class drawer extends AppCompatActivity implements PlaylistAdapter.clickME {
private static final String TAG = drawer.class.getSimpleName();
private final M3UParser parser = new M3UParser();
public M3UPlaylist playlist;
private ArrayList<LiveStreams> liveStreams;
private String user;
private String passwd;
private RecyclerView mPlaylistList;
private InputStream is;
DBHelper db;
private PlaylistAdapter mAdapter;

private ProgressBar progressBar;
private SimpleExoPlayer player;
private PlayerView simpleExoPlayerView;
ProgressBar progress_bar;

int vis;
 Toolbar toolbar;
Timer longClickTimer;
private static final DefaultBandwidthMeter BANDWIDTH_METER =
        new DefaultBandwidthMeter();
TrackSelection.Factory factory;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drawer);

    toolbar= findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Live");
    getSupportActionBar().setIcon(R.drawable.iptvsmall);

    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    liveStreams = new ArrayList<>();
    progressBar = findViewById(R.id._progress);
    db=new DBHelper(this);

    //////////


    simpleExoPlayerView = findViewById(R.id.video_view);
    user = Others.getStringOption(getApplicationContext(), "userid", "");
    passwd = Others.getStringOption(getApplicationContext(), "userPassword", "");

    simpleExoPlayerView.hideController();

    progress_bar = findViewById(R.id._progress_play);

    factory = new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
    player = ExoPlayerFactory.newSimpleInstance(

            new DefaultRenderersFactory(drawer.this),
            new DefaultTrackSelector(factory), new DefaultLoadControl());




    player.addListener(new Player.EventListener() {
        @Override
        public void onTimelineChanged(Timeline timeline, Object manifest, int reason) {

        }

        @Override
        public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {

        }

        @Override
        public void onLoadingChanged(boolean isLoading) {

        }

        @Override
        public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
            if (playbackState == Player.STATE_BUFFERING) {
                progress_bar.setVisibility(View.VISIBLE);
            } else {
                progress_bar.setVisibility(View.INVISIBLE);
            }
        }

        @Override
        public void onRepeatModeChanged(int repeatMode) {

        }

        @Override
        public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {

        }

        @Override
        public void onPlayerError(ExoPlaybackException error) {

            Toast.makeText(drawer.this, ""+error, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onPositionDiscontinuity(int reason) {

        }

        @Override
        public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {

        }

        @Override
        public void onSeekProcessed() {

        }
    });

    mPlaylistList = findViewById(R.id.playlist_recycler);



}

public void play(String url) {
    try {
        MediaSource mediaSource = buildMediaSource(Uri.parse(url));


        simpleExoPlayerView.setPlayer(player);

        Toast.makeText(this, "Player", Toast.LENGTH_SHORT).show();
        player.prepare(mediaSource, true, false);
        player.setPlayWhenReady(true);
    } catch (Exception e) {

        Log.e("googo1", e.getMessage());
    }
}

private MediaSource buildMediaSource(Uri uri) {


    return new ExtractorMediaSource.Factory(
            new DefaultHttpDataSourceFactory(mHandler.userAgent)).
            createMediaSource(uri);
}

  private class _readingDataFromUrl2 extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... strings) {

        StringBuilder builder;
        String json;
        BufferedReader reader;
        try {
            URL url = new URL(strings[0]);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setDoOutput(true);
            con.setDoInput(true);
            builder = new StringBuilder();
            reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
            while ((json = reader.readLine()) != null) {
                builder.append(json).append("\n");
            }
            return builder.toString();
        } catch (Exception error) {
            Log.e(TAG, error.toString());
            return null;
        }
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        if (s != null) {
            is = new ByteArrayInputStream(s.getBytes()); // this will read file online
            M3UPlaylist playlist = parser.parseFile(is);
            mHandler.m3UPlaylist = playlist;
          /*  ArrayList<fvtritme> arrayList=new ArrayList<>();
            ArrayList<String> array=new ArrayList<>();
            arrayList=db.getAllSubject();

            if(arrayList.size()>0)
            {
                for(int i=0;i<arrayList.size();i++)
                {
                    for(int j=0;j<playlist.getPlaylistItems().size();j++)
                    {

                        if(arrayList.get(i).getName().equals(playlist.getPlaylistItems().get(j).getItemName()))

                        {

                            Log.e("data",arrayList.get(i).getName()+" "+playlist.getPlaylistItems().get(j).getItemName()+" "+j);
                            array.add("yes");


                        }
                        else
                        {
                            array.add("no");

                        }
                    }
                }
                //Log.e("data",array.size()+" "+playlist.getPlaylistItems().size());

            }*/


           // mAdapter.stars(array);
            mAdapter.update(playlist.getPlaylistItems());
            final M3UItem item = playlist.getPlaylistItems().get(0);

            play(item.getItemUrl());
            url=item.getItemUrl();
        } else {
            Toast.makeText(drawer.this, "Unable to get channels, check connection", Toast.LENGTH_SHORT).show();
        }
        progressBar.setVisibility(View.GONE);
    }
}

@Override
protected void onResume() {
    super.onResume();

    if(player!=null)
    {
        player.setPlayWhenReady(true);
        player.getPlaybackState();
    }
    GetList();



}
  public void GetList() {

    final String link = "http://albboxapp.xyz/albx/geturl.json";
    progressBar.setVisibility(View.VISIBLE);
    RequestQueue queue = Volley.newRequestQueue(getApplicationContext());

    JsonObjectRequest stringRequest = new JsonObjectRequest(Request.Method.GET, link, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    String num = null;

                    try {
                        num = response.getString("urlget");
                        mHandler.userAgent = response.getString("useragent");
                        new _readingDataFromUrl2().execute(num);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                                       }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Others.getInstance().toast(getApplicationContext(), findViewById(R.id.activity_main), "Unable to get channels, check connection");
            progressBar.setVisibility(View.GONE);
        }
    });

    // Add the request to the RequestQueue.
    queue.add(stringRequest);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    mPlaylistList.setLayoutManager(layoutManager);

    mAdapter = new PlaylistAdapter(getApplicationContext(),drawer.this);
    mAdapter.setClick(this);

    mPlaylistList.setAdapter(mAdapter);



}


@Override
public void itemClick(String url) {

    play(String.valueOf(Uri.parse(url)));
    this.url = url;

}

0 个答案:

没有答案