0

Hi i am using the code above for getting the subtitles from a url to play in a video which i stream in my android app. Unfortunatly nothing happens and the suds are not displayed in the video playing.

 public class VideoPlayerActivity extends Activity implements SurfaceHolder.Callback,     MediaPlayer.OnPreparedListener, VideoControllerView.MediaPlayerControl {

SurfaceView videoSurface;
MediaPlayer player;
VideoControllerView controller;
String subtitleURL = "http://************/mp4/Despicable.Me.2.2013.srt";
private boolean mediaplayer_error_state = false;
private boolean mediaplayer_released = false;
private TimedTextObject srt;
static final int SUBTITLE_DISPLAY_CHECK = 100;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video_player);
    new SubtitleAsyncTask().execute();
    videoSurface = (SurfaceView) findViewById(R.id.videoSurface);
    SurfaceHolder videoHolder = videoSurface.getHolder();
    videoHolder.addCallback(this);
    player = new MediaPlayer();
    controller = new VideoControllerView(this);

    try {
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        player.setDataSource(this,    Uri.parse("http://*************/mp4/Despicable.Me.2.2013.mp4"));
        player.setOnPreparedListener(this);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }



}


private Handler subtitleDisplayHandler = new Handler();
private Runnable subtitle = new Runnable() {
 public void run() {
   if (isMediaPlayerStateValid() && player.isPlaying()) {
      int currentPos = player.getCurrentPosition();
      Collection<Caption> subtitles =  srt.captions.values();
      for(Caption caption : subtitles) {
         if (currentPos >= caption.start.getMilliseconds() && currentPos <= caption.end.getMilliseconds()) {
            onTimedText(caption);
            break;
     } else if (currentPos > caption.end.getMilliseconds()) {
        onTimedText(null);
         }
      }
   }
   subtitleDisplayHandler.postDelayed(this, SUBTITLE_DISPLAY_CHECK);
 };
};

protected boolean isMediaPlayerStateValid() {
    if (player != null && mediaplayer_error_state == false
                    && mediaplayer_released == false) {
            return true;
    }
    return false;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    controller.show();
    return false;
}

// Implement SurfaceHolder.Callback
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    player.setDisplay(holder);
    player.prepareAsync();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}
// End SurfaceHolder.Callback

// Implement MediaPlayer.OnPreparedListener
@Override
public void onPrepared(MediaPlayer mp) {
    controller.setMediaPlayer(this);
    controller.setAnchorView((FrameLayout) findViewById(R.id.videoSurfaceContainer));
    player.start();
}
// End MediaPlayer.OnPreparedListener
// Implement VideoMediaController.MediaPlayerControl
@Override
public boolean canPause() {
    return true;
}
@Override
public boolean canSeekBackward() {
    return true;
}
@Override
public boolean canSeekForward() {
    return true;
}

@Override
public int getBufferPercentage() {
    return 0;
}

@Override
public int getCurrentPosition() {
    return player.getCurrentPosition();
}

@Override
public int getDuration() {
    return player.getDuration();
}

@Override
public boolean isPlaying() {
    return player.isPlaying();
}

@Override
public void pause() {
    player.pause();
}

@Override
public void seekTo(int i) {
    player.seekTo(i);
}
@Override
public void start() {
    player.start();
}
@Override
public boolean isFullScreen() {
    return false;
}
@Override
public void toggleFullScreen() {

}
// End VideoMediaController.MediaPlayerControl

public void onTimedText(Caption text) {
       TextView subtitles = (TextView) findViewById(R.id.txtSubtitles);
       if (text == null) {
          subtitles.setVisibility(View.INVISIBLE);
          return;
       }
       subtitles.setText(Html.fromHtml(text.content));
       subtitles.setVisibility(View.VISIBLE);
    }

public class SubtitleAsyncTask extends AsyncTask<Void, Void, Void> {

      @Override
      protected Void doInBackground(Void... params) {
        if (subtitleURL != null) {
           try {
              URL url = new URL(subtitleURL);
              InputStream stream = url.openStream();
              FormatSRT formatSRT = new FormatSRT();

            srt = formatSRT.parseFile(stream);
              subtitleDisplayHandler.post(subtitle);

           } catch (Exception e) {
              Log.e(getClass().getName(), e.getMessage(), e);
           }
        }
        // TODO Auto-generated method stub
        return null;
      }
    }

  }

i am also using the files from the serenity app from the link above: https://github.com/NineWorlds/serenity-android

Can anyone help me with this project!!??

4

0 回答 0