您好我正在使用上面的代码从网址获取字幕,以便在我的Android应用中流式传输的视频中播放。不幸的是没有任何事情发生,并且视频播放中没有显示肥皂泡。
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;
}
}
}
我也使用上面链接中的宁静应用程序中的文件: https://github.com/NineWorlds/serenity-android
任何人都可以帮助我完成这个项目!! ??