How to play multiple video in different video view in one activity - android

Sample Code:
How to play the two video in one activity
public class Two_videos extends Activity
{
VideoView video1, video2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.two_video);
VideoView video1= (VideoView) findViewById(R.id.video1);
video1.setVideoPath("/mnt/sdcard/Movies/com.bnb.giggle/IMG_20130415184609.mp4");
video1.start();
VideoView video2= (VideoView) findViewById(R.id.video2);
video2.setVideoPath("/mnt/sdcard/Movies/com.bnb.giggle/IMG_20130415184608.mp4");
video2.start();
}
}
cannot play two video same time.

Play multiple videos is depends on hardware but if your device supports only one instance of media Player then you must call yourVideoView.stopPlayBack() for playing video in another videoview. you can resume video in first videoview by using its resume() method but after stop playing from second.

Your device should support playing multiple videos also quality of videos (HD videos) which needs high end devices else it will throw error.
I tried this code it works with all nexus devices.

According to this answer, it is very much possible to play multiple videos simultaneously, but it depends on the device and its hardware. The Android version doesn't seem to matter. I'd suggest you read his comments and code for a better understanding.

See below code , here play 4 video's in different video view in one activity
main_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="ycrathi.com.multiplevideoplay.MainActivity">
<VideoView
android:id="#+id/v1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25" />
<VideoView
android:id="#+id/v2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25" />
<VideoView
android:id="#+id/v3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25" />
<VideoView
android:id="#+id/v4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25" />
</LinearLayout>
MainActivity.java
package ycrathi.com.multiplevideoplay;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView v1 = (VideoView) findViewById(R.id.v1);
VideoView v2 = (VideoView) findViewById(R.id.v2);
VideoView v3 = (VideoView) findViewById(R.id.v3);
VideoView v4 = (VideoView) findViewById(R.id.v4);
v1.setVideoURI(Uri.parse("http://192.168.1.1/Video/vid5.mp4"));
v1.start();
v1.requestFocus();
v1.setKeepScreenOn(true);
v2.setVideoURI(Uri.parse("http://192.168.1.1/Video/vid5.mp4"));
v2.start();
v2.requestFocus();
v2.setKeepScreenOn(true);
v3.setVideoURI(Uri.parse("http://192.168.1.1/Video/vid5.mp4"));
v3.start();
v3.requestFocus();
v3.setKeepScreenOn(true);
v4.setVideoURI(Uri.parse("http://192.168.1.1/Video/vid5.mp4"));
v4.start();
v4.requestFocus();
v4.setKeepScreenOn(true);
}
}
AndroidMenifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ycrathi.com.multiplevideoplay">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Here is the full answer for showing multiple video in one activity.
Working in all device.
I have used Surfaceview and Textureview for Playing Videos.
Activity:
public class ReactionViewActivity extends AppCompatActivity {
private static final String TAG = ReactionViewActivity.class.getSimpleName();
private String mainVideo = "";
private ProgressBar progress_bar;
private SurfaceView mSurfaceView;
private TextureView mTextureView;
private MediaPlayer mMediaPlayer, mMediaPlayer1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reaction_view);
initView();
}
private void initView() {
progress_bar = findViewById(R.id.progress_bar);
mSurfaceView = findViewById(R.id.surface_view);
mTextureView = findViewById(R.id.textureView);
textureParams = (FrameLayout.LayoutParams) mTextureView.getLayoutParams();
surfaceParams = (FrameLayout.LayoutParams) mSurfaceView.getLayoutParams();
final String video = getIntent().getStringExtra("video");
mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
Surface surface = new Surface(surfaceTexture);
try {
mMediaPlayer1 = new MediaPlayer();
mMediaPlayer1.setDataSource(video);
mMediaPlayer1.setSurface(surface);
mMediaPlayer1.setLooping(true);
mMediaPlayer1.prepareAsync();
// Play video when the media source is ready for playback.
mMediaPlayer1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
progress_bar.setVisibility(View.GONE);
mediaPlayer.start();
}
});
mMediaPlayer1.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IllegalArgumentException e) {
Log.d(TAG, e.getMessage());
} catch (SecurityException e) {
Log.d(TAG, e.getMessage());
} catch (IllegalStateException e) {
Log.d(TAG, e.getMessage());
} catch (IOException e) {
Log.d(TAG, e.getMessage());
}
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return false;
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
});
if (Objects.requireNonNull(getIntent().getExtras()).containsKey("mainVideo")) {
mainVideo = getIntent().getStringExtra("mainVideo");
mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDisplay(surfaceHolder);
try {
mMediaPlayer.setDataSource(mainVideo);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mMediaPlayer.start();
}
});
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
#Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}
});
}
}
#Override
public void onBackPressed() {
releaseMediaPlayer();
finish();
}
#Override
protected void onDestroy() {
super.onDestroy();
releaseMediaPlayer();
}
private void releaseMediaPlayer() {
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
if (mMediaPlayer1 != null) {
mMediaPlayer1.release();
mMediaPlayer1 = null;
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ReactionViewActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
android:layout_below="#+id/rlTopbar">
<TextureView
android:id="#+id/textureView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
<SurfaceView
android:id="#+id/surface_view"
android:layout_width="110dp"
android:layout_height="140dp"
android:layout_gravity="right"
android:layout_marginRight="#dimen/margin_20"
android:layout_marginTop="#dimen/margin_20" />
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="#dimen/margin_50"
android:layout_height="#dimen/margin_50"
android:layout_gravity="center"
android:indeterminateDrawable="#drawable/custom_progress" />
</FrameLayout>
</RelativeLayout>

Related

ImageVIew Thumbnail setVisibility GONE not working inside onPrepared

I want to hide ImageView Thumbnail after Remote Video ready to play, means when onPrepared execute but imageView.setVisibility(View.GONE) doesn't works at all.
I have seen this answers one, two and I think cause is SurfaceView or VideoView. as per answers
I have tried using both MediaPlayer and VideoView
code using MediaPlayer and SurfaceView
mMediaPlayer = new MediaPlayer();
holder.surfaceView.setDrawingCacheEnabled(true);
try {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(mContext, Uri.parse(video_url));
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
((Activity)mContext).runOnUiThread(new Runnable() {
#Override
public void run() {
holder.imgThumbnail.getParent().requestTransparentRegion(holder.imgThumbnail);
holder.imgThumbnail.setVisibility(View.GONE);
holder.imgThumbnail.getParent().requestTransparentRegion(holder.imgThumbnail);
}
});
Toast.makeText(mContext,"onPrepared",Toast.LENGTH_LONG).show();
}
});
} catch (IllegalArgumentException | SecurityException | IllegalStateException | IOException e) {
e.printStackTrace();
}
mMediaPlayer.prepareAsync();
XML of SurfaceView
<RelativeLayout
android:layout_width="410dp"
android:id="#+id/v_view"
android:visibility="visible"
android:layout_height="307.50dp">
<SurfaceView
android:id="#+id/video_view"
android:layout_width="410dp"
android:layout_height="307.50dp"
/>
<ImageView
android:id="#+id/imgThumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:scaleType="centerCrop" />
</RelativeLayout>
code using VideoView
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
Log.i(TAG,"onPrepared");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
videoView.start();
Toast.makeText(mContext,"start video",Toast.LENGTH_LONG).show();
((Activity)mContext).runOnUiThread(new Runnable() {
#Override
public void run() {
holder.imgThumbnail.getParent().
requestTransparentRegion(holder.imgThumbnail);
holder.imgThumbnail.setVisibility(View.GONE);
holder.imgThumbnail.getParent().
requestTransparentRegion(holder.imgThumbnail);
}
});
}
},100);
}
});
XML of VideoView
<RelativeLayout
android:layout_width="410dp"
android:id="#+id/v_view"
android:visibility="visible"
android:layout_height="307.50dp">
<VideoView
android:id="#+id/video_view"
android:layout_width="410dp"
android:layout_height="307.50dp"
/>
<ImageView
android:id="#+id/imgThumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:background="#color/back_orange"
android:scaleType="centerCrop" />
</RelativeLayout>
Please help! I'm stuck

Scrolling in my app is really not smooth

I made an android app that plays sounds when clicking the button. These sounds can be also shared via messenger. All of these buttons (their look and position) are specified in activity_main.xml. The problem is, when I scroll up or down in my app, it looks terrible, because it's not smooth and my app seems to be laggy.
Here's my code with some buttons and imageviews for example
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/r1">
<Button
android:text="#string/sound1"
android:layout_marginTop="33dp"
android:id="#+id/sound1"
android:layout_width="210dp"
android:background="#drawable/backgroundbuttons"
android:layout_height="50dp"
android:layout_alignRight="#+id/sound3"
android:layout_alignEnd="#+id/sound3"
android:textSize="13sp" />
<Button
android:text="#string/sound2"
android:layout_width="210dp"
android:layout_height="50dp"
android:layout_marginTop="8dp"
android:id="#+id/sound2"
android:background="#drawable/backgroundbuttons"
android:layout_below="#+id/sound1"
android:layout_alignLeft="#+id/sound1"
android:layout_alignStart="#+id/sound1"
android:textSize="13sp" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
app:srcCompat="#drawable/sharebutton"
android:id="#+id/share_button1"
android:layout_alignBottom="#+id/sound1"
android:layout_toRightOf="#+id/sound1"
android:layout_toEndOf="#+id/sound1" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
app:srcCompat="#drawable/sharebutton"
android:id="#+id/share_button2"
android:layout_alignBottom="#+id/sound2"
android:layout_toRightOf="#+id/sound2"
android:layout_toEndOf="#+id/sound2" />
There are in total 25 buttons and 25 imageviews in my activity_main that look like this. All of these imageviews are based on one file (sharebutton.jpg).
EDIT:
Main Activity class:
public class MainActivity extends AppCompatActivity {
private MediaPlayer mp;
private MessengerThreadParams mThreadParams;
private boolean mPicking;
private static final int REQUEST_CODE_SHARE_TO_MESSENGER = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button s1 = (Button) findViewById(R.id.sound1);
Button s2 = (Button) findViewById(R.id.sound2);
findViewById(R.id.share_button1).setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
onMessengerButtonClicked1();
}
});
findViewById(R.id.share_button2).setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
onMessengerButtonClicked2();
}
});
Intent intent = getIntent();
if (Intent.ACTION_PICK.equals(intent.getAction())) {
mThreadParams = MessengerUtils.getMessengerThreadParamsForIntent(intent);
mPicking = true;
}
s1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onPause();
mp = MediaPlayer.create(MainActivity.this, R.raw.a1);
mp.start();
}
});
s2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onPause();
mp = MediaPlayer.create(MainActivity.this, R.raw.a2);
mp.start();
}
private void onMessengerButtonClicked1() {
// The URI can reference a file://, content://, or android.resource.
Uri uri =
Uri.parse("android.resource://" + getPackageName()+ "/raw/" +R.raw.a1);
ShareToMessengerParams shareToMessengerParams =
ShareToMessengerParams.newBuilder(uri, "audio/mpeg")
.setMetaData("{ \"audio\" : \"a1\" }")
.build();
if (mPicking) {
MessengerUtils.finishShareToMessenger(this, shareToMessengerParams);
} else {
MessengerUtils.shareToMessenger(
this,
REQUEST_CODE_SHARE_TO_MESSENGER,
shareToMessengerParams);
}
}
private void onMessengerButtonClicked2() {
Uri uri =
Uri.parse("android.resource://" + getPackageName()+ "/raw/" +R.raw.a2);
ShareToMessengerParams shareToMessengerParams =
ShareToMessengerParams.newBuilder(uri, "audio/mpeg")
.setMetaData("{ \"audio\" : \"a2\" }")
.build();
if (mPicking) {
MessengerUtils.finishShareToMessenger(this, shareToMessengerParams);
} else {
MessengerUtils.shareToMessenger(
this,
REQUEST_CODE_SHARE_TO_MESSENGER,
shareToMessengerParams);
}
}
protected void onPause() {
super.onPause();
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
https://developer.android.com/guide/components/activities/activity-lifecycle.html
Above is a general outline that can be used in broad areas including your situation.
As for your code you can implement the following:
#Override
public void onResume() {
mp.onResume();
// Setup the player
mp.resume();
}
#Override
public void onDestroy() {
if(mp != null) {
mp.release();
}
super.onDestroy();
}
s1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp != null)
{
mp.stop();
mp.release();
}
finish();
}
});
}
If it helps, use logs to help you see the lifecycle
try{
if(mp !=null && mp.isPlaying()){
Log.d("TAG------->", "player is running");
mp.stop();
Log.d("Tag------->", "player is stopped");
mp.release();
Log.d("TAG------->", "player is released");
}
}catch(Exception e){
//Throw the error
}

Restart the audio from where it had stopped when the audio played after clicking a button finishes

I am playing an audio (mySound) as background music. On clicking a button, the background audio should stop and a different audio (mySound11) should start playing. After this audio gets done, the background music should automatically resume from where it had left. I have tried this code, but the background audio doesn't resume after mySound11 is over. Please help. Thanks in advance.
MainActivity.java
public class MainActivity extends ActionBarActivity {
MediaPlayer mySound;
MediaPlayer mySound11;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySound11 = MediaPlayer.create(this, R.raw.tiktik);
mySound = MediaPlayer.create(this, R.raw.jhakmaarke);
mySound.setLooping(true);
mySound.start();
}
public void playMusic (View view){
mySound.pause();
mySound11.start();
}
#Override
public void onResume() {
super.onResume();
mySound.start();
}
#Override
protected void onPause() {
super.onPause();
mySound.release();
mySound11.release();
finish();
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button Dabaa be"
android:onClick="playMusic"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp" />
I Solved your problem with the help of Thread.
I tested it working as you wished.
int mySound11Duration;
MediaPlayer mySound;
MediaPlayer mySound11;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySound11 = MediaPlayer.create(this, R.raw.tiktik);
mySound = MediaPlayer.create(this, R.raw.jhakmaarke);
mySound.setLooping(true);
mySound.start();
}
public void playMusic(View view) {
if (mySound.isPlaying()) { //check mysound is playing or not
mySound11Duration = mySound11.getDuration();
Thread thread=new Thread() {
#Override
public void run() {
try {
mySound.pause();
int mySoundPausePosition = mySound.getCurrentPosition();//get the time where the song is paused.
mySound11.start();
sleep(mySound11Duration);//sleep method causes the currently executing thread to sleep for specified number of time(miliseconds).
mySound.seekTo(mySoundPausePosition);
mySound.start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
}
}
}

no video media player

I have a problem with playing video in mediaplayer. I have a sound but no video. I tried everything and can't find solve. Or maybe you know how to play sdp file in videoview? Here's the code:
public class TestStream1 extends Activity implements Callback{
MediaPlayer mMediaPlayer;
String SrcPath = "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_175k.mov";
private SurfaceView mPreview;
private SurfaceHolder holder;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPreview = (SurfaceView) findViewById(R.id.surface);
holder = mPreview.getHolder();
holder.addCallback(TestStream1.this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.setFixedSize(400,300);
mMediaPlayer = new MediaPlayer();
try{
mMediaPlayer.setDataSource(SrcPath);
mMediaPlayer.setDisplay(holder);
mMediaPlayer.prepare();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.start();
}catch(Exception e){
Toast .makeText( TestStream1.this, "Fail", Toast.LENGTH_LONG).show();
}
}
#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
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
EDIT: layout file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<SurfaceView
android:id="#+id/surface"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</LinearLayout>
Some videos cannot be played in mediaplayer, if it doesn't have the codec for its a/v or it is too complex or badly interleaved (Look at Logcat for signs about some). Could be one or more reasons. To make sure the mediaplayer can play the type of video you are asking it to play, save a video file of the same a/v codec and extension to your sd card and then try to play it using the default media player.
Meanwile, try using a videoView to check the video can be played.Your code seems all right.
Try this code :
package a.b;
public class TestStream1 extends Activity implements Callback{
MediaPlayer mMediaPlayer;
String SrcPath = "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_175k.mov";
private SurfaceView mPreview;
private SurfaceHolder holder;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPreview = (SurfaceView) findViewById(R.id.surface);
holder = mPreview.getHolder();
holder.addCallback(TestStream1.this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.setFixedSize(400,300);
mMediaPlayer = new MediaPlayer();
}
#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
try{
mMediaPlayer.setDisplay(holder);
mMediaPlayer.setDataSource(SrcPath);
mMediaPlayer.prepare();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.start();
}catch(Exception e){
Toast .makeText( TestStream1.this, "Fail", Toast.LENGTH_LONG).show();
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
mMediaPlayer.release();
}
}
SurfaceHolders are created ansynchronously, so we have to wait until the surface has been created and then assign the returned surface holder object .
And I am using the following layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SurfaceView
android:id="#+id/surface"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
The above solution should work.
Note sure, but try setting the datasource like:
mMediaPlayer.setDataSource(this, SrcPath);

How to add video file at the startup of App in Android Programming?

I need to add video file whenever I start App showing details. So I need help for that. I need to know how to write a code for that.
save your video file in raw folder
give your video file name in
path="android.resource://yourpackagename/"+R.raw.yourvideofilename;
see the below class
public class HomeVideo extends Activity{
private VideoView videoView;
String extStorageDirectory;
protected static final int PLAY = 0x101;
protected static final int STOP = 0x102;
protected static final int PAUSE = 0x103;
int State;
private String current;
private String path ;
private VideoView mVideoView;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.homevideo);
path="android.resource://yourpackagename/"+R.raw.yourvideofilename;
mVideoView = (VideoView) findViewById(R.id.video);
if (path == null || path.length() == 0) {
Toast.makeText(HomeVideo.this, "File URL/path is empty",
Toast.LENGTH_LONG).show();
} else {
// If the path has not changed, just start the media player
if (path.equals(current) && mVideoView != null) {
mVideoView.start();
mVideoView.requestFocus();
return;
}
current = path;
mVideoView.setVideoPath(path);
mVideoView.start();
mVideoView.requestFocus();
}
mVideoView.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer arg0) {
Intent in =new Intent(HomeVideo.this,NextActivity.class);
startActivity(in);
finish();
}
});
}
}
xml file like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView
android:id="#+id/video"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:keepScreenOn="true">
</VideoView>
</LinearLayout>
In manifest file
<activity android:name=".HomeVideo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
VideoViewExample *.java*
package com.sample.VideoViewExample;
public class VideoViewExample extends Activity {
private VideoView mVideoView;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() +"/"+R.raw.documentariesandyou));
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
}
}
and add thid in the XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView<br/>
android:id="#+id/surface_view"
android:layout_width="320px"
android:layout_height="240px"/>
</LinearLayout>

Categories

Resources