From 7920cb20c79be99b639267ddd3c9ddc49b78d1ae Mon Sep 17 00:00:00 2001 From: Crossle Song Date: Tue, 13 Aug 2013 13:14:47 +0800 Subject: [PATCH] Add TimedText demo & Video buffer demo --- vitamio-sample/AndroidManifest.xml | 27 ++- vitamio-sample/project.properties | 2 +- vitamio-sample/res/layout/subtitle1.xml | 21 +++ vitamio-sample/res/layout/subtitle2.xml | 29 ++++ vitamio-sample/res/layout/videobuffer.xml | 47 ++++++ vitamio-sample/res/values/strings.xml | 8 + .../demo/MediaMetadataRetrieverDemo.java | 15 ++ .../vov/vitamio/demo/MediaPlayerSubtitle.java | 156 ++++++++++++++++++ .../src/io/vov/vitamio/demo/VideoBuffer.java | 117 +++++++++++++ .../vov/vitamio/demo/VideoSubtitleList.java | 64 +++++++ .../vov/vitamio/demo/VideoViewSubtitle.java | 86 ++++++++++ .../vov/vitamio/demo/VitamioListActivity.java | 2 + 12 files changed, 570 insertions(+), 4 deletions(-) create mode 100644 vitamio-sample/res/layout/subtitle1.xml create mode 100644 vitamio-sample/res/layout/subtitle2.xml create mode 100644 vitamio-sample/res/layout/videobuffer.xml create mode 100644 vitamio-sample/src/io/vov/vitamio/demo/MediaPlayerSubtitle.java create mode 100644 vitamio-sample/src/io/vov/vitamio/demo/VideoBuffer.java create mode 100644 vitamio-sample/src/io/vov/vitamio/demo/VideoSubtitleList.java create mode 100644 vitamio-sample/src/io/vov/vitamio/demo/VideoViewSubtitle.java diff --git a/vitamio-sample/AndroidManifest.xml b/vitamio-sample/AndroidManifest.xml index 0f852df..350df54 100644 --- a/vitamio-sample/AndroidManifest.xml +++ b/vitamio-sample/AndroidManifest.xml @@ -1,12 +1,12 @@ + android:versionCode="002" + android:versionName="0.0.2" > + android:targetSdkVersion="18" /> @@ -52,6 +52,27 @@ android:name=".MediaMetadataRetrieverDemo" android:label="Media/MediaMetadata" > + + + + + + + + + + + \ No newline at end of file diff --git a/vitamio-sample/project.properties b/vitamio-sample/project.properties index 0f02fee..1f93c1f 100644 --- a/vitamio-sample/project.properties +++ b/vitamio-sample/project.properties @@ -11,5 +11,5 @@ #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt # Project target. -target=android-17 +target=android-18 android.library.reference.1=../vitamio diff --git a/vitamio-sample/res/layout/subtitle1.xml b/vitamio-sample/res/layout/subtitle1.xml new file mode 100644 index 0000000..72bbd86 --- /dev/null +++ b/vitamio-sample/res/layout/subtitle1.xml @@ -0,0 +1,21 @@ + + + + + + + \ No newline at end of file diff --git a/vitamio-sample/res/layout/subtitle2.xml b/vitamio-sample/res/layout/subtitle2.xml new file mode 100644 index 0000000..f2e0be6 --- /dev/null +++ b/vitamio-sample/res/layout/subtitle2.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/vitamio-sample/res/layout/videobuffer.xml b/vitamio-sample/res/layout/videobuffer.xml new file mode 100644 index 0000000..63679fb --- /dev/null +++ b/vitamio-sample/res/layout/videobuffer.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/vitamio-sample/res/values/strings.xml b/vitamio-sample/res/values/strings.xml index 310c401..0965460 100644 --- a/vitamio-sample/res/values/strings.xml +++ b/vitamio-sample/res/values/strings.xml @@ -6,4 +6,12 @@ Play Streaming Video Play Audio from Local File Play Audio from Resources + VideoSubtitleDemo + Settings + MediaPlayer Subtitles + VideoView Subtitles + MediaPlayerSubtitle + VideoViewSubtitle + VideoSubtitleList + VideoBuffer \ No newline at end of file diff --git a/vitamio-sample/src/io/vov/vitamio/demo/MediaMetadataRetrieverDemo.java b/vitamio-sample/src/io/vov/vitamio/demo/MediaMetadataRetrieverDemo.java index d3e8a3a..f60c065 100644 --- a/vitamio-sample/src/io/vov/vitamio/demo/MediaMetadataRetrieverDemo.java +++ b/vitamio-sample/src/io/vov/vitamio/demo/MediaMetadataRetrieverDemo.java @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2013 yixia.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package io.vov.vitamio.demo; import android.app.Activity; diff --git a/vitamio-sample/src/io/vov/vitamio/demo/MediaPlayerSubtitle.java b/vitamio-sample/src/io/vov/vitamio/demo/MediaPlayerSubtitle.java new file mode 100644 index 0000000..a19a3d6 --- /dev/null +++ b/vitamio-sample/src/io/vov/vitamio/demo/MediaPlayerSubtitle.java @@ -0,0 +1,156 @@ +/* + * Copyright (C) 2013 yixia.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.vov.vitamio.demo; + +import java.io.IOException; + +import io.vov.vitamio.LibsChecker; +import io.vov.vitamio.MediaPlayer; +import io.vov.vitamio.MediaPlayer.OnPreparedListener; +import io.vov.vitamio.MediaPlayer.OnTimedTextListener; + +import android.os.Bundle; +import android.os.Environment; +import android.app.Activity; +import android.graphics.PixelFormat; +import android.view.SurfaceHolder; +import android.view.SurfaceHolder.Callback; +import android.view.SurfaceView; +import android.widget.TextView; +import android.widget.Toast; + +public class MediaPlayerSubtitle extends Activity implements Callback, OnPreparedListener, OnTimedTextListener { + + SurfaceView splayer; + SurfaceHolder sholder; + TextView tv; + private MediaPlayer mediaPlayer; + private String path = ""; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (!LibsChecker.checkVitamioLibs(this)) + return; + setContentView(R.layout.subtitle1); + tv = (TextView) findViewById(R.id.sub1); + splayer = (SurfaceView) findViewById(R.id.surface); + sholder = splayer.getHolder(); + sholder.setFormat(PixelFormat.RGBA_8888); + sholder.addCallback(this); + } + + private void playVideo() { + try { + if (path == "") { + // Tell the user to provide an audio file URL. + Toast.makeText(MediaPlayerSubtitle.this, "Please edit MediaPlayer Activity, " + "and set the path variable to your media file path." + " Your media file must be stored on sdcard.", Toast.LENGTH_LONG).show(); + return; + } + mediaPlayer = new MediaPlayer(this); + mediaPlayer.setDataSource(path); + mediaPlayer.setDisplay(sholder); + mediaPlayer.prepareAsync(); + mediaPlayer.setOnPreparedListener(this); + + mediaPlayer.setOnTimedTextListener(this); + + // TODO Auto-generated catch block + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalStateException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Override + public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { + // TODO Auto-generated method stub + + } + + @Override + public void surfaceCreated(SurfaceHolder holder) { + // TODO Auto-generated method stub + playVideo(); + } + + @Override + public void surfaceDestroyed(SurfaceHolder holder) { + // TODO Auto-generated method stub + + } + + private void startVPback() { + + mediaPlayer.start(); + } + + @Override + public void onPrepared(MediaPlayer arg0) { + + // TODO Auto-generated method stub + startVPback(); + mediaPlayer.addTimedTextSource(Environment.getExternalStorageDirectory() + "/12.srt"); + mediaPlayer.setTimedTextShown(true); + } + + @Override + protected void onPause() { + // TODO Auto-generated method stub + super.onPause(); + relaMediaPlay(); + } + + private void relaMediaPlay() { + // TODO Auto-generated method stub + if (mediaPlayer != null) { + mediaPlayer.release(); + mediaPlayer = null; + } + + } + + @Override + protected void onDestroy() { + // TODO Auto-generated method stub + super.onDestroy(); + relaMediaPlay(); + + } + + @Override + public void onTimedText(String text) { + // TODO Auto-generated method stub + tv.setText(text); + } + + @Override + public void onTimedTextUpdate(byte[] pixels, int width, int height) { + // TODO Auto-generated method stub + + } + +} diff --git a/vitamio-sample/src/io/vov/vitamio/demo/VideoBuffer.java b/vitamio-sample/src/io/vov/vitamio/demo/VideoBuffer.java new file mode 100644 index 0000000..775a671 --- /dev/null +++ b/vitamio-sample/src/io/vov/vitamio/demo/VideoBuffer.java @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2013 yixia.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.vov.vitamio.demo; + +import android.app.Activity; +import android.net.Uri; +import android.os.Bundle; +import android.view.View; +import android.widget.ProgressBar; +import android.widget.TextView; +import android.widget.Toast; + +import io.vov.vitamio.LibsChecker; +import io.vov.vitamio.MediaPlayer; +import io.vov.vitamio.MediaPlayer.OnBufferingUpdateListener; +import io.vov.vitamio.MediaPlayer.OnInfoListener; +import io.vov.vitamio.widget.MediaController; +import io.vov.vitamio.widget.VideoView; + +public class VideoBuffer extends Activity implements OnInfoListener, OnBufferingUpdateListener { + + /** + * TODO: Set the path variable to a streaming video URL or a local media file + * path. + */ + private String path = ""; + private Uri uri; + private VideoView mVideoView; + private boolean isStart; + ProgressBar pb; + TextView downloadRateView, loadRateView; + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + if (!LibsChecker.checkVitamioLibs(this)) + return; + setContentView(R.layout.videobuffer); + mVideoView = (VideoView) findViewById(R.id.buffer); + pb = (ProgressBar) findViewById(R.id.probar); + + downloadRateView = (TextView) findViewById(R.id.download_rate); + loadRateView = (TextView) findViewById(R.id.load_rate); + if (path == "") { + // Tell the user to provide a media file URL/path. + Toast.makeText(VideoBuffer.this, "Please edit VideoBuffer Activity, and set path" + " variable to your media file URL/path", Toast.LENGTH_LONG).show(); + return; + } else { + /* + * Alternatively,for streaming media you can use + * mVideoView.setVideoURI(Uri.parse(URLstring)); + */ + uri = Uri.parse(path); + mVideoView.setVideoURI(uri); + mVideoView.setMediaController(new MediaController(this)); + mVideoView.requestFocus(); + mVideoView.setOnInfoListener(this); + mVideoView.setOnBufferingUpdateListener(this); + mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { + @Override + public void onPrepared(MediaPlayer mediaPlayer) { + // optional need Vitamio 4.0 + mediaPlayer.setPlaybackSpeed(1.0f); + } + }); + } + + } + + @Override + public boolean onInfo(MediaPlayer mp, int what, int extra) { + switch (what) { + case MediaPlayer.MEDIA_INFO_BUFFERING_START: + if (mVideoView.isPlaying()) { + mVideoView.pause(); + isStart = true; + pb.setVisibility(View.VISIBLE); + downloadRateView.setVisibility(View.VISIBLE); + loadRateView.setVisibility(View.VISIBLE); + + } + break; + case MediaPlayer.MEDIA_INFO_BUFFERING_END: + if (isStart) { + mVideoView.start(); + pb.setVisibility(View.GONE); + downloadRateView.setVisibility(View.GONE); + loadRateView.setVisibility(View.GONE); + } + break; + case MediaPlayer.MEDIA_INFO_DOWNLOAD_RATE_CHANGED: + downloadRateView.setText("" + extra + "kb/s" + " "); + break; + } + return true; + } + + @Override + public void onBufferingUpdate(MediaPlayer mp, int percent) { + loadRateView.setText(percent + "%"); + } + +} diff --git a/vitamio-sample/src/io/vov/vitamio/demo/VideoSubtitleList.java b/vitamio-sample/src/io/vov/vitamio/demo/VideoSubtitleList.java new file mode 100644 index 0000000..4a2a373 --- /dev/null +++ b/vitamio-sample/src/io/vov/vitamio/demo/VideoSubtitleList.java @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2013 yixia.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.vov.vitamio.demo; + +import android.app.ListActivity; +import android.content.Intent; +import android.os.Bundle; +import android.view.View; +import android.widget.ListView; +import android.widget.SimpleAdapter; + +import io.vov.vitamio.LibsChecker; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class VideoSubtitleList extends ListActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (!LibsChecker.checkVitamioLibs(this)) + return; + setListAdapter(new SimpleAdapter(this, getData(), android.R.layout.simple_list_item_1, new String[] { "title" }, new int[] { android.R.id.text1 })); + } + + protected List> getData() { + List> myData = new ArrayList>(); + addItem(myData, "MediaPlayerSubtitle", new Intent(this, MediaPlayerSubtitle.class)); + addItem(myData, "VideoViewSubtitle", new Intent(this, VideoViewSubtitle.class)); + return myData; + } + + protected void addItem(List> data, String name, Intent intent) { + Map temp = new HashMap(); + temp.put("title", name); + temp.put("intent", intent); + data.add(temp); + } + + @Override + protected void onListItemClick(ListView l, View v, int position, long id) { + @SuppressWarnings("unchecked") + Map map = (Map) l.getItemAtPosition(position); + Intent intent = (Intent) map.get("intent"); + startActivity(intent); + } + +} diff --git a/vitamio-sample/src/io/vov/vitamio/demo/VideoViewSubtitle.java b/vitamio-sample/src/io/vov/vitamio/demo/VideoViewSubtitle.java new file mode 100644 index 0000000..e714653 --- /dev/null +++ b/vitamio-sample/src/io/vov/vitamio/demo/VideoViewSubtitle.java @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2013 yixia.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.vov.vitamio.demo; + +import android.app.Activity; +import android.os.Bundle; +import android.widget.TextView; +import android.widget.Toast; + +import io.vov.vitamio.LibsChecker; +import io.vov.vitamio.MediaPlayer; +import io.vov.vitamio.MediaPlayer.OnTimedTextListener; +import io.vov.vitamio.widget.MediaController; +import io.vov.vitamio.widget.VideoView; + +public class VideoViewSubtitle extends Activity { + + private String path = ""; + private String subtitle_path = ""; + private VideoView mVideoView; + private TextView mSubtitleView; + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + if (!LibsChecker.checkVitamioLibs(this)) + return; + setContentView(R.layout.subtitle2); + mVideoView = (VideoView) findViewById(R.id.surface_view); + + mSubtitleView = (TextView) findViewById(R.id.subtitle_view); + + if (path == "") { + // Tell the user to provide a media file URL/path. + Toast.makeText(VideoViewSubtitle.this, "Please edit VideoViewSubtitle Activity, and set path" + " variable to your media file URL/path", Toast.LENGTH_LONG).show(); + return; + } else { + /* + * Alternatively,for streaming media you can use + * mVideoView.setVideoURI(Uri.parse(URLstring)); + */ + mVideoView.setVideoPath(path); + + mVideoView.setMediaController(new MediaController(this)); + mVideoView.requestFocus(); + + mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { + @Override + public void onPrepared(MediaPlayer mediaPlayer) { + // optional need Vitamio 4.0 + mediaPlayer.setPlaybackSpeed(1.0f); + mVideoView.addTimedTextSource(subtitle_path); + mVideoView.setTimedTextShown(true); + + } + }); + mVideoView.setOnTimedTextListener(new OnTimedTextListener() { + + @Override + public void onTimedText(String text) { + mSubtitleView.setText(text); + } + + @Override + public void onTimedTextUpdate(byte[] pixels, int width, int height) { + + } + }); + } + + } +} diff --git a/vitamio-sample/src/io/vov/vitamio/demo/VitamioListActivity.java b/vitamio-sample/src/io/vov/vitamio/demo/VitamioListActivity.java index aa73957..dffa5f3 100644 --- a/vitamio-sample/src/io/vov/vitamio/demo/VitamioListActivity.java +++ b/vitamio-sample/src/io/vov/vitamio/demo/VitamioListActivity.java @@ -47,6 +47,8 @@ protected List> getData() { addItem(myData, "MediaPlayer", new Intent(this, MediaPlayerDemo.class)); addItem(myData, "VideoView", new Intent(this, VideoViewDemo.class)); addItem(myData, "MediaMetadata", new Intent(this, MediaMetadataRetrieverDemo.class)); + addItem(myData, "VideoSubtitle", new Intent(this, VideoSubtitleList.class)); + addItem(myData, "VideoBuffer", new Intent(this, VideoBuffer.class)); return myData; }