Neelabh
Neelabh

Reputation: 29

How to fix 'mp4 video not running in Exoplayer 2.9'

I am trying to run an mp4 video from a link using Exoplayer 2.9 but the video is not running. Here is the video link I am trying to run "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4".

I have looked at other answers for Exoplayer cases but apparently, a lot of methods have become deprecated for Exoplayer version 2.9.0. It's not a phone problem as I tried running a sample Exoplayer version 2.8.2 code which runs fine. Eventually, I would have to run the previous version, in case there is no answer for the above.

Here is the layout file:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="@dimen/card_margin">
        <com.google.android.exoplayer2.ui.PlayerView
            android:id="@+id/receipe_step_video"
            android:focusable="true"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            />
        <TextView
            android:id="@+id/recipe_step_detail"
            android:padding="@dimen/card_margin"
            android:layout_height="250dp"
            android:layout_width="match_parent"
            android:textAppearance="?android:textAppearanceMedium"
                />
    </LinearLayout>
</ScrollView>

Here is the Activity code:

public class RecipeStepActivity extends AppCompatActivity {
    @BindView(R.id.recipe_step_detail)
    TextView mRecipeDetailView;
    @BindView(R.id.receipe_step_video)
    PlayerView playerView;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recipe_step_layout);
        ButterKnife.bind(this);
        mRecipeDetailView.setText(MyData.description);

        SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(this);
        player.setPlayWhenReady(true);
        playerView.setPlayer(player);

        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
                Util.getUserAgent(this,this.getString(R.string.app_name)));
        MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(MyData.videoUrl));
        player.prepare(videoSource);
        player.release();

    }
}

Here is the output which I am getting. The video is not playing here. Link to image: https://github.com/sigma-s/100-Days-of-Code/blob/master/Images/bakingappday8.png

Upvotes: 0

Views: 1297

Answers (1)

Neelabh
Neelabh

Reputation: 29

Ah! Found the error after trying multiple permutations and combinations for the last 2 days.

Just need to place player.release() in onStop so that the player is not released as soon as it is set up. Another important thing is to provide internet permission in Android Manifest as the video is being accessed through an mp4 URL.

@Override
public void onStop(){
    super.onStop();
    player.release();
}

I have tried the above code and it works fine.

Upvotes: 0

Related Questions