Open a new scene after video playback - android

I am learning android and I am trying to figure out how to open a new scene adfter a video view has finished.
This is my code:
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.VideoView;
public class SplashScene extends Activity implements OnCompletionListener {
BaseActivity activity;
VideoView video;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
video = (VideoView) findViewById(R.id.vvSplashVideo);
String path = "android.resource://" + getPackageName()
+ "/raw/splashscreen";
video.setVideoURI(Uri.parse(path));
video.setOnCompletionListener(this);
video.start();
loadResources();
}
public void loadResources() {
}
#Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
Log.v("Fin", "done");
activity.setCurrentScene(new MainMenuScene());
}
When I run the app, the video works and the log message works but then I get this error:
http://puu.sh/1YuiA before the app crashes.
Any ideas as to what I'm doing wrong.

you have to initialize activity;inside SplashScene class.
code:
public class SplashScene extends Activity implements OnCompletionListener {
BaseActivity activity;
....
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
activity=new BaseActivity (); <===== initiate this
// rest of code }
}
}

Related

play Video in VideoView

In my activity I am trying to play a video which is stored in raw folder. below is my activity.
In the first button I am not getting error also like can't play this video, but only black screen appears. while clicking the second button I am getting a message that can't play this video. below is my activity
package com.example.college;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
public class FirstYear extends Activity {
Button cse,it,ece,eee;
VideoView vv;
MediaController mc;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.first_year);
cse=(Button)findViewById(R.id.button1);
it=(Button)findViewById(R.id.button2);
ece=(Button)findViewById(R.id.button3);
eee=(Button)findViewById(R.id.button4);
vv=(VideoView)findViewById(R.id.videoView1);
mc=new MediaController(this);
mc.setAnchorView(vv);
cse.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
vv.setVideoURI(Uri.parse("android.resource://com.example.college/"+R.raw.c));
vv.start();
}
});
it.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
vv.setVideoURI(Uri.parse("android.resource://com.example.college/"+R.raw.k));
vv.start();
}
});
ece.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
vv.setVideoURI(Uri.parse("android.resource://com.example.college/"+R.raw.a));
vv.start();
}
});
eee.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
vv.setVideoURI(Uri.parse("android.resource://com.example.college/"+R.raw.s));
vv.start();
}
});
}
}
The code is correct. All the errors are related to video resolution.
I have replaced video with different resolution , and it is working

imageview not loading the image.app is shutting down

I have an imageview and button in the xml file . loading the image1 and play song1.mp3 file.when the song is over it should load next image and play the song2.mp3 song it should go on until the last image. button for closing and exit the application.
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.widget.ImageView;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
public class MainActivity extends Activity {
public MediaPlayer mpp;
final int image[] = {R.drawable.apple,R.drawable.ball,R.drawable.cat};
int i=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView img = (ImageView) findViewById(R.id.img);
String audio[]={"song1.mp3","song2.mp3","song3.mp3"};
AssetFileDescriptor descriptor;
try {
descriptor = getAssets().openFd(audio[i]);
mpp.setDataSource( descriptor.getFileDescriptor(), descriptor.getStartOffset(),descriptor.getLength());
descriptor.close();
mpp.prepare();
mpp.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mpp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
img.setImageResource(image[i]);
i++;
mpp.start();
}
});
} }
Fatal Exception in main.RuntimeException not able to start the activity

Main Activity not opening

My app seems to start up properly, with the splash screen and stuff. But when it sleeps for 6 secs and when it supposed to get into the main activity the app crashes any help please?
Here is me code (android.intent.action1.MAINACTIVIVTY, the "action" was purposely changed to "action1")
package com.hellhogone.multitools;
import com.hellhogone.multitools.R;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Window;
import android.view.WindowManager;
public class Splash extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
MediaPlayer yo = MediaPlayer.create(Splash.this, R.raw.smusic);
yo.start();
Thread timer = new Thread(){
public void run(){
try{
sleep(6000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent h1 = new Intent("android.intent.action1.MAINACTIVITY");
startActivity(h1);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
You cannot start an activity from another thread than the UI thread. To avoid this problem you can use runOnUiThread() :
}finally{
runOnUiThread(new Runnable() {
public void run() {
Intent h1 = new Intent("android.intent.action1.MAINACTIVITY");
startActivity(h1);
}
});
}

Playback error on using two VideoViews

When starting playback of two HTTP streams in two VideoViews simultaneously, it works. When I try to stop one and start the other, it throws an error. Code used:
mVideoView1.setVideoPath(videoPath);
mVideoView2.setVideoPath(videoPath);
mVideoView1.start();
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mVideoView1.stopPlayback();
mVideoView2.start();
}
});
Any ideas what's causing this error? I get the same behaviour when I use MediaPlayer and SurfaceView (preparing second MediaPlayer in background, release() the first, then start() the second)
you can use the following code it work for me,
package com.materialexample;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.VideoView;
import in.co.bhadreshtech.materialexample.R;
public class VideoViews extends AppCompatActivity {
ProgressBar progressBar = null;
VideoView videoView1 = null;
VideoView video_views2 = null;
String videoUrl = "http://www.androidbegin.com/tutorial/AndroidCommercial.3gp";
Context context = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_views);
context = null;
videoView1 = (VideoView) findViewById(R.id.video_view1);
video_views2 = (VideoView) findViewById(R.id.video_views2);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
Uri videoUri = Uri.parse(videoUrl);
videoView1.setVideoURI(videoUri);
video_views2.setVideoURI(videoUri);
videoView1.start();
video_views2.start();
progressBar.setVisibility(View.VISIBLE);
videoView1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
#Override
public void onVideoSizeChanged(MediaPlayer mp, int arg1,
int arg2) {
// TODO Auto-generated method stub
progressBar.setVisibility(View.GONE);
mp.start();
}
});
}
});
video_views2.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
#Override
public void onVideoSizeChanged(MediaPlayer mp, int arg1,
int arg2) {
// TODO Auto-generated method stub
progressBar.setVisibility(View.GONE);
mp.start();
}
});
}
});
}
}
also don miss to add tow VideoView and one ProgressBar in layout

VideoView doesn't show the video when switching tabs

I have 2 tabs each containing videoView. But when I switch to the second tab the video plays the audio is working, but the video doesn't plays and the view is only black. This works fine in Android 2.1 + , but it does't in 1.6 Here is my code:
TabActivity:
package com.example.tab;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class main extends TabActivity {
/** Called when the activity is first created. */
private TabHost tabHost;
public void onCreate( Bundle savedInstanceState){
super.onCreate(savedInstanceState);
tabHost = getTabHost();
Intent intent1 = new Intent(this, Video1.class);
Intent intent2 = new Intent(this, Video2.class);
tabHost.addTab(tabHost.newTabSpec("Tab1")
.setIndicator("Video1")
.setContent(intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)));
tabHost.addTab(tabHost.newTabSpec("Tab2")
.setIndicator("Video2")
.setContent(intent2.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)));
}
}
Video1 activity:
/**
*
*/
package com.example.tab;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;
/**
* #author lyubomir.todorov
*
*/
public class Video1 extends Activity implements SurfaceHolder.Callback {
private String path = "http://xxx.xxx";
private VideoView mVideoView;
private SurfaceHolder holder;
private static final int INSERT_ID = Menu.FIRST;
private static final String TAG = "Tab";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoview1);
mVideoView = (VideoView) findViewById(R.id.surface_view1);
// holder = mVideoView.getHolder();
// holder.addCallback(this);
// holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, INSERT_ID, 0, "FullScreen");
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
Log.d(TAG, "Player re-started after device configuration change");
return true;
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
PlayVideo();
Log.d(TAG, "onResume Video1");
}
protected void onPause() {
super.onDestroy();
Log.d(TAG, "onPause Video1");
if (mVideoView != null) {
mVideoView.stopPlayback();
mVideoView.setVisibility(View.GONE);
mVideoView=null;
Log.d(TAG, "onPause1 Video1");
}
}
public void surfaceDestroyed(SurfaceHolder surfaceholder) {
holder.isCreating();
Log.d(TAG, "video1 surfaceDestroyed called");
}
public void surfaceCreated(SurfaceHolder holder) {
Log.d(TAG, "Current 1 surfaceCreated called");
Log.d(TAG, Boolean.valueOf(holder.isCreating()).toString());
// playVideo(extras.getInt(MEDIA));
}
public void PlayVideo() {
mVideoView.setVideoPath(path);
mVideoView.start();
mVideoView.setMediaController(new MediaController(this));
mVideoView.setVisibility(View.VISIBLE);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Log.d(TAG, Boolean.valueOf(holder.isCreating()).toString());
// TODO Auto-generated method stub
Log.d(TAG, "Current video surfaceChanged called");
}
}
videoview1.xml
<?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">
<VideoView android:id="#+id/surface_view1"
android:layout_width="320dip" android:layout_height="240dip"
/>
</LinearLayout>
Video2 activity:
/**
*
*/
package com.example.tab;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;
/**
* #author lyubomir.todorov
*
*/
public class Video2 extends Activity implements SurfaceHolder.Callback {
private String path = "http://xxx.xxx";
private VideoView mVideoView;
private SurfaceHolder holder;
private static final String TAG = "Tab";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoview2);
mVideoView = (VideoView) findViewById(R.id.surface_view2);
// holder= mVideoView.getHolder();
// holder.addCallback(this);
// holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
PlayVideo();
Log.d(TAG, "onResume Video1");
}
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause Video2");
if (mVideoView!=null){
mVideoView.stopPlayback();
mVideoView.setVisibility(View.GONE);
Log.d(TAG, "onPause1 Video2");
}
}
public void surfaceDestroyed(SurfaceHolder surfaceholder) {
holder.isCreating();
Log.d(TAG, "video1 surfaceDestroyed called");
}
public void surfaceCreated(SurfaceHolder holder) {
Log.d(TAG, "Current 1 surfaceCreated called");
Log.d(TAG, Boolean.valueOf(holder.isCreating()).toString());
}
public void PlayVideo() {
mVideoView.setVisibility(View.VISIBLE);
mVideoView.bringToFront();
mVideoView.setVideoPath(path);
mVideoView.start();
mVideoView.setMediaController(new MediaController(this));
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Log.d(TAG, Boolean.valueOf(holder.isCreating()).toString());
// TODO Auto-generated method stub
Log.d(TAG, "Current video surfaceChanged called");
}
}
videoview2.xml:
<?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">
<VideoView android:id="#+id/surface_view2"
android:layout_width="320dip" android:layout_height="240dip"
/>
</LinearLayout>

Categories

Resources