在MediaPlayer上无法通过Android 9通过URL播放视频

时间:2018-10-17 10:56:18

标签: android media-player

我正在尝试通过url播放视频,它可以在android <9

上运行

但是在android 9上无法正常工作

@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);
}  

更新:

我正在尝试通过直接网址播放视频,该视频可在android <9

上运行

但是在android 9上它不起作用,此代码在android 9出现之前就可以正常工作,但是在android 9上不再起作用

这是完整的课程:

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) {

    }
}

这是仅包含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>

0 个答案:

没有答案