使用SurfaceView的媒体播放器在Android api等级22及更低版本上获得音频和黑屏

时间:2016-10-10 15:11:27

标签: android media-player surfaceview

我尝试使用媒体播放器和SurfaceView播放视频,在android api level 23上它工作正常,但在22级以及更少我只获得音频和黑屏。

这是我的代码:(它全部在片段内)

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup   container, @Nullable Bundle savedInstanceState) {
    pageInflater = inflater.inflate(R.layout.page2, container, false);

    playButton = (ImageView) pageInflater.findViewById(R.id.playImage);
    videoSurface = (SurfaceView)pageInflater.findViewById(R.id.videoSurface);

    player = new MediaPlayer();
    controller = new VideoControllerView(getActivity());
    controller.setAnchorView((FrameLayout) pageInflater.findViewById(R.id.videoSurfaceContainer));

    SurfaceHolder videoHolder = videoSurface.getHolder();
    videoHolder.addCallback(this);


    playButton.setOnClickListener(new View.OnClickListener() {
                                      public void onClick(View v) {                                    
                     playButton.setVisibility(View.GONE);                                                     
                     playButton.setClickable(false);
                     player.stop();
                     player.reset();
                     setVideo();
                     player.start();
                                      }
                                  }
    );

    return pageInflater;
}

设置视频并开始播放:

private void setVideo() {
    String vidAddress = tutorialsVideo["SOME URL"];
    player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        player.setDataSource(getActivity(), Uri.parse(vidAddress));
    } catch (IOException e) {
        Toast.makeText(getActivity().getApplicationContext(), "Error while playing video. Please, check your network connection.", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
    controller.setMediaPlayer(this);
    player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mp) {

            player.seekTo(currentVideoPosition);
            player.start();

        }
    });

    player.prepareAsync();

    playButton.setVisibility(View.GONE);
    playButton.setClickable(false);

}

SurfaceHolder实现:

// Implement SurfaceHolder.Callback
@Override
public void surfaceCreated(SurfaceHolder holder) {
    player.setDisplay(holder);
}

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

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    player.release();
}
// End SurfaceHolder.Callback

代码在LG G4上使用Android API级别23以及在Android Studio Emulator Nexus 5 API 23上正常运行,但是当我试图在我的Samsung S3 API 19和任何带有API的仿真器上运行它时22或更少我用黑屏获得音频。

有什么建议吗?

0 个答案:

没有答案