1

I'm trying to play a video through a url, it works on android <9

but on android 9 it's not working

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
                MediaPlayer mediaPlayer = new MediaPlayer();
                mediaPlayer.setDataSource(videoPath);
                mediaPlayer.prepareAsync();
                mediaPlayer.setLooping(false);
                mediaPlayer.setSurface(new Surface(surfaceTexture));
                mediaPlayer.setOnPreparedListener(MediaPlayer::start);
}  

UPDATE:

I'm trying to play a video through a direct url, it works on android <9

but on android 9 it's not working this code was working before android 9 came it was working well but with android 9 not working anymore

this is the full class :

public class FeatureFragment extends Fragment implements TextureView.SurfaceTextureListener {


    TextureView surfaceView;
    private String videoPath = "direct url.mp4";


    public static Fragment newInstance() {
        return new FeatureFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.feature_fragment, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        surfaceView = view.findViewById(R.id.videoView1);
        surfaceView.setSurfaceTextureListener(this);
    }


    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
        try {
            MediaPlayer mediaPlayer = new MediaPlayer();
            mediaPlayer.setDataSource(videoPath);
            mediaPlayer.prepareAsync();
            mediaPlayer.setLooping(false);
            mediaPlayer.setSurface(new Surface(surfaceTexture));
            mediaPlayer.setOnPreparedListener(MediaPlayer::start);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
        surfaceTexture = null;
        videoPath = null;
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {

    }
}

and here is the layout which contains only the TextureView

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


        <TextureView
            android:id="@+id/videoView1"
            android:layout_width="300dp"
            android:layout_gravity="center"
            android:layout_height="300dp"
            android:visibility="visible" />

</FrameLayout>
MusabAlothman
  • 68
  • 1
  • 8

0 Answers0