sap
sap

Reputation: 319

Unable to use Exoplayer lib in android

I am trying to implement Exoplayer in android to stream songs. This is what I have tried until now by following this link

I have 2 buttons in my layout file, 1 to start player and other to stop.

Here is the class file

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

    Button btPlay = (Button) findViewById(R.id.bt_play);
    Button btStop = (Button) findViewById(R.id.bt_stop);
    Context mContext = getBaseContext();
    String URL = "http://sound15.mp3slash.net/indian/tummile/tummile01(www.songs.pk).mp3";

    btPlay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Uri uri = Uri.parse(URL);
            final int numRenderers = 2;
            SampleSource sampleSource = new FrameworkSampleSource(mContext, uri, null, numRenderers);
//above line gives me error

            TrackRenderer audioTrack = new MediaCodecAudioTrackRenderer(sampleSource);

            ExoPlayer exoPlayer = ExoPlayer.Factory.newInstance(numRenderers);
            exoPlayer.prepare(audioTrack);
            exoPlayer.setPlayWhenReady(true);
        }
    });

}

The error thrown is on SampleSource line which says

cannot resolve constructor FrameworkSampleSource

How do I deal with this error and carry on to stream song. Any help will be appreciated!

Upvotes: 1

Views: 983

Answers (1)

Kunal Bhatia
Kunal Bhatia

Reputation: 721

for using library in appropriate way: youtube video

FrameworkSampleSource is depreciated use ExtractorSampleSource

see below code:

        String url = parentArray.getJSONObject(songIndex).getString("file");
        Uri uri = Uri.parse(url);
        player = ExoPlayer.Factory.newInstance(1);
        playerControl = new PlayerControl(player);
        DataSource dataSource = new DefaultUriDataSource(this, TAG);
        ExtractorSampleSource extractorSampleSource = new ExtractorSampleSource(uri, dataSource, new DefaultAllocator(64 * 1024), 64 * 1024 * 256);
        audioRenderer = new MediaCodecAudioTrackRenderer(extractorSampleSource, MediaCodecSelector.DEFAULT);
        player.prepare(audioRenderer);
        player.setPlayWhenReady(true);

Upvotes: 1

Related Questions