当我播放第二个视频时,只显示第一个视频的冻结帧并播放音频

时间:2018-05-15 15:38:06

标签: android android-mediaplayer exoplayer exoplayer2.x

我的应用程序有一个片段正在播放一秒钟的视频。播放视频后,用户必须按一个按钮,然后才能播放新的视频。在播放第二个视频之前,我的主要活动加载会创建一个新片段并创建新的Exoplayer实例。

我第一次播放视频时,一切都按预期工作。如果我想要播放第二个视频,它会显示第一个视频的最后一帧的冻结图像并播放第二个视频的声音。 此错误不会出现在Android 8(API级别26)上,而是出现在Android 8以下的版本上(我测试了Android 6和Android 7.1)。

鉴于我有一个带有新片段的新Surface和一个新的Exoplayer,对我来说,为什么还有前一个视频最后一帧的数据仍然是个谜。 是否有一些功能我可以调用删除该数据并获得我在Android 8中获得的行为,其中第二个视频在早期版本的Android中也没有问题?

在我使用ExoPlayer之前,普通的MediaPlayer有同样的问题。我创建了问题的minimized example

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button mButton = findViewById(R.id.button1);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createNewFragment();
            }
        });
    }

    private void createNewFragment() {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(R.id.contentFragment, new VideoFragment());
        ft.commit();
    }
}

public class VideoFragment extends Fragment implements SurfaceHolder.Callback{
    private Context mContext;
    private SimpleExoPlayer mPlayer;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.videolayout, container, false);
        SurfaceView mSurfaceView = (SurfaceView) view.findViewById(R.id.surface_view1);
        SurfaceHolder surfaceHolder = mSurfaceView.getHolder();
        surfaceHolder.setSizeFromLayout();
        surfaceHolder.addCallback(this);

        mContext = inflater.getContext();

        return view;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
        TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);

        mPlayer = ExoPlayerFactory.newSimpleInstance(mContext, trackSelector);
        mPlayer.setVideoSurfaceHolder(holder);
        mPlayer.setPlayWhenReady(true);
        playVideo(mContext);
    }

    private void playVideo(Context context){
        final RawResourceDataSource rawResourceDataSource = new RawResourceDataSource(context);

        int raw_res_id = context.getResources().getIdentifier(
                "collect",
                "raw",
                context.getPackageName());
        DataSpec dataSpec = new DataSpec(RawResourceDataSource.buildRawResourceUri(raw_res_id));
        try {
            rawResourceDataSource.open(dataSpec);

            DataSource.Factory factory = new DataSource.Factory() {
                @Override
                public DataSource createDataSource() {
                    return rawResourceDataSource;
                }
            };
            MediaSource media_source = new ExtractorMediaSource.Factory(factory).createMediaSource(rawResourceDataSource.getUri());
            mPlayer.prepare(media_source);

        } catch (RawResourceDataSource.RawResourceDataSourceException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
    }
}

1 个答案:

答案 0 :(得分:1)

尝试为fragmentchange

执行以下代码
 final FragmentTransaction transaction = fragmentManager.beginTransaction();
    Fragment f = new VideoFragment();
    transaction.replace(R.id.contentFragment, f).commit();

只需尝试此代码并检查是否发生相同的