I am doing the Android Programming Tutorial on Splash Screens where you show a picture or text for 5 Seconds than it goes to the Main Application. My Question is..Instead of Text or Pictures I want to display a Video File for 5 Seconds before it goes to the Next page of the Application.
I am not talking about when the Application Loads I am talking about when it is Loaded and you program it to display something on a Separate Java & XML page to display something then move to something else..here is my current code.
#Override
protected void onCreate(Bundle SplashScreen1) {
// TODO Auto-generated method stub
super.onCreate(SplashScreen1);
setContentView(R.layout.splash);
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
ourSong.start();
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch (InterruptedException e){
e.printStackTrace();
}finally{
Intent openStartingPoint = new Intent("com.Player.Splash.STARTINGPOINT");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
So What Do I do to make it display a Video Media file without the Start/Stop etc..
1) Create SplashScreen.java class.
2) Create a raw folder inside res directory(res/raw).
3) Paste your mp4 video file in this raw folder(if you don't have any sample mp4, you can download from the below link). http://www.mediafire.com/download/p05ki89i2dt5x2x/splash.mp4
4) Then add the following code in your SplashScreen.java class.
public class SplashScreenActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
VideoView videoHolder = new VideoView(this);
setContentView(videoHolder);
Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.splash);
videoHolder.setVideoURI(video);
videoHolder.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
jump();
}
});
videoHolder.start();
} catch (Exception ex) {
jump();
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
jump();
return true;
}
private void jump() {
if (isFinishing())
return;
startActivity(new Intent(this, MainActivity.class));
finish();
}
}
Note: splash_activity.xml is not required.
I hope this will help you. You just create a simple VideoView for create a splash screen for the video.
Checkout the source code hear and simple steps what is the best practice to create a splash screen
Use a MediaPlayer along with a VideoView. You can then "listen" for when the video playback is done, by setting an OnCompletionListener on your MediaPlayer.
See here: http://developer.android.com/reference/android/media/MediaPlayer.html
And here: http://developer.android.com/reference/android/widget/VideoView.html
Also, pay special attention to the state diagram on the MediaPlayer reference page. It can be a bit tricky and has been known to trip a few people up.
imgAnim=(VideoView)findViewById(R.id.animimage);
String uriPath = "android.resource://com.petnvet/" + R.drawable.vidio;
Uri uri = Uri.parse(uriPath);
imgAnim.setVideoURI(uri);
imgAnim.requestFocus();
imgAnim.start();
// imgAnim.setVideoPath("android.resource://com.myapplication/" + R.drawable.vidio);
int SPLASH_DISPLAY_LENGTH = 3000;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent mainIntent = new Intent(SplashScreen.this, Login.class);
startActivity(mainIntent);
finish();
}
}, SPLASH_DISPLAY_LENGTH);
Here is the code for adding video. In case you need to add controls on video like pause or seek etc. you can add them with:
vv.setMediaController(new MediaController(this));
Rest of the code:
VideoView vv;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
vv=(VideoView)findViewById(R.id.videoView);
Uri path=Uri.parse("android:resource://"+getPackageName()+"/"+R.raw.hello);
vv.setVideoURI(path);
vv.setMediaController(new MediaController(this));
vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp) {
Intent in=new Intent(splash.this,MainActivity.class);
startActivity(in);
finish();
}
});
vv.start();
Related
friends, I am developing an android application. I want to play a thanks video when I exit my application. can anyone suggest any method, please..?
Just Use onBackPressed(); or When U Close The App
Hear Simple Example..... You Can Modify as Your Use
#Override
public void onBackPressed() {
//VideoView vv = (VideoView) findViewById(R.id.videoView1); your VideoView Default Visiblity is GONE
vv.setVisiblity(View.VISIBLE);
uri = "your video uri";
vv.setVideoURI(Uri.parse(uri));
vv.start();
vv.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
//Video Finish
super.onBackPressed();
}
});
}
OR
public void closinApplicationShowVideo()
{
videoView.setVisiblity(View.VISIBLE);
uri = "your video uri";
videoView.setVideoURI(Uri.parse(uri));
videoView.start();
videoView.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
//Video Finish
//Apply Your Logic To Close Application
}
});
}
Note : If You Have One Or More Activity Then U need To create One New
Activity for this Video in This Activity Write Code To Playvideo on
Complition close The Application....
Call VideoView Activity onBackPressed in Other Activity
Like
in Your Other Activity when User Try To Close Application like backpress
Call Activity on BackPressed
#Override
public void onBackPressed() {
//Note Dont Call super.onBackPressed();
startActivity(new Intent(currentActivity.this,videoViewActivity.class));
finish();
}
public class MergeVideo extends Activity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myfirstpage);
VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
myVideoView.setVideoPath("/storage/emulated/0/Android/data/com.example.android.camera2video/files/a.mp4");
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.seekTo(6000);
myVideoView.start();
}
}
this is my code for play video . i have one main video file 19 second length i want to play video from 6 second to 12 second i am able to start video from 6 second . i don't know how to stop video play on 12 sec please suggest me how implement this .
Not tested this, but it should work in your case.
private VideoView mVideoView;
private boolean mShouldStop = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mVideoView = (VideoView) findViewById(R.id.myvideoview);
mVideoView.setVideoPath("/storage/emulated/0/Android/data/com.example.android.camera2video/files/a.mp4");
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.seekTo(6000);
mVideoView.start();
trackProgress();
}
private void trackProgress() {
new Thread(new Runnable() {
#Override
public void run() {
while (!mShouldStop) {
if (mVideoView != null && mVideoView.isPlaying()) {
if (mVideoView.getCurrentPosition() >= 12000) {
mVideoView.stopPlayback();
mShouldStop = true;
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
Since the VideoView documentation doesn't have hooks for this, let's take a step back: what do we want to do?
Play the video (already done)
After x amount of time, stop the video (to do)
Implement a timer
We want to play a video for an amount of time, then stop the video. Why not implement a Timer (see documentation)?
On this timer, you can schedule a task to be performed.
One caveat: Don't forget to cancel() the timer. Otherwise it'll keep calling the method to pause/stop the video.
I've created this simple soundboard app which plays a sound on click. I've also setup so that the text changes when the button is hit but is there anyway for the text to change back to its original when the sound file is finished playing
Thanks in advance!
MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button aussi = (Button) findViewById(R.id.aussi);
aussi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(aussi.getText().toString().equals("Australian")){
try{
}catch (Exception e){
e.printStackTrace();
}
aussi.setText("Jay");
mediaPlayer = MediaPlayer.create(MainActivity.this,
R.raw.aussi);
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer play) {
// TODO Auto-generated method stub
play.release();
}
});
}
}
});
You got the solution by yourself, the completion listener tells you when the sound finishes playing. so :
#Override
public void onCompletion(MediaPlayer play) {
play.release();
// Now you can set you text
aussi.setText("your original text");
}
});
what i have is an activity which i play some sound each the user open it , but when he got to the child activity of it .. it stops as i want .. but i want the user when he press back button and return to this activity (which is parent activity) to play this sound again ... how can i do this .. here is some of my code:
public class GeneralScreen extends Activity {
String mytext="";
MediaPlayer mMediaPlayer;
protected static final int RESULT_SPEECH = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.general_screen);
GridView g = (GridView) findViewById(R.id.myGrid);
mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(this, R.raw.selecteng);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(false);
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
Log.i("Completion Listener","Song Complete");
Toast.makeText(GeneralScreen.this, "Media Completed", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); //for open google API
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US"); //for transfer voice from google API to text in EditText
try {
startActivityForResult(intent, RESULT_SPEECH); // for get result
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(), //if make error get this message
"Ops! Your device doesn't support Speech to Text",
Toast.LENGTH_SHORT);
t.show();
}
}
});
Just create a method playYourMusic() and put all the code to start mediaplayer inside. Then put the playYourMusic into onRestart and in onCreate():
#Override
protected void onRestart() {
playYourMusic();
super.onRestart();
}
Think on it, to finish the activity that You are leaving when You will go back to main. So, why not in onResume()? Because onResume() is also called at the start of Activity. Some devices handle the LifeCycle of android in a different way.
You just use following methods #Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//play music code here
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
//stop music code here
}
You need to execute your playSound() method in the appropriate lifecycle method (in this case I think it would likely be onResume() ).
Understanding the Activity lifecycle is very fundamental, and important to understand, please check the following resource for more information:
http://developer.android.com/training/basics/activity-lifecycle/index.html
I am making a new android sound application. I made a clickable button to play sound when I click on it. But I also want it to stop playing sound when I click for the second time. That part works fine now here is the problem, when I click again on button to play sound again, it doesn't play it, Media player is completely stopped. I was looking on forums but I can't seem to find an answer that could help me.
Here is my Activity:
MediaPlayer mpButtonClick1;
MediaPlayer mpButtonClick2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.prvi);
final MediaPlayer mpButtonClick1 = MediaPlayer.create(this, R.raw.spalshm);
final MediaPlayer mpButtonClick2 = MediaPlayer.create(this, R.raw.splashs);
Button dugme = (Button) findViewById(R.id.dugme);
dugme.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mpButtonClick1.isPlaying()) {
mpButtonClick1.stop();
mpButtonClick1.reset();
}
else {
mpButtonClick1.start();
}
}
});
When I try to write mpButtonClick1.prepare(); I get error Unhandled Exception Type IOE exception
Try to use pause instead of stop.
Reason: if you pause the MediaPlayer, then you can resume it later. However, if you use stop, almost any other method won't work and you will have to prepare the MediaPlayer again (or create a new one).
More info: here and here
PS: don't forget to release the memory when you finish using the resources.
Try this:
You should use only one mediaplayer object
public class PlayaudioActivity extends Activity {
private MediaPlayer mp;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
final TextView t = (TextView) findViewById(R.id.textView1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.far);
mp.start();
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.beet);
mp.start();
}
});
}
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}
Change your class with below code:
remove reset();.
init well all components:
MediaPlayer mpButtonClick1;
MediaPlayer mpButtonClick2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.prvi);
mpButtonClick1 = MediaPlayer.create(this, R.raw.spalshm);
mpButtonClick2 = MediaPlayer.create(this, R.raw.splashs);
Button dugme = (Button) findViewById(R.id.dugme);
dugme.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mpButtonClick1.isPlaying()) {
mpButtonClick1.stop();
}
else {
mpButtonClick1.start();
}
}
});
You're calling mpButtonClick1.reset() after mpButtonClick1.stop() - don't do that:
if (mpButtonClick1.isPlaying()) {
mpButtonClick1.stop();
mpButtonClick1.reset(); //<--------- calling reset(), remove this line
}
The docs for reset() say:
Resets the MediaPlayer to its uninitialized state. After calling this method, you will have to initialize it again by setting the data source and calling prepare().
Remove mpButtonClick1.reset() and it should work.
Keep in mind that MediaPlayer works as a state machine, which means that if you call methods in the wrong order, you'll get problems. Please read about MediaPlayer here and here.
Hey please use following
for stop -> media player
mp.seekTo(0);
mp.pause();
again for start just call
mp.start();
In my experience when I need to play multiple times and I may need to stop one play to start another play, (like in the case of multiple buttons), I just create another player, making sure that I release the resources for the previous one. To stop just use
mediaPlayer.stop();
But for play use something like this (adapt the logging to your specific needs) to create/recreate your player:
private boolean createMediaPlayer()
{
if (mediaPlayer!=null)
{
if(mediaPlayer.isPlaying())
{
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer=null;
}
}
mediaPlayer = new MediaPlayer();
mediaPlayer.setVolume(1f, 1f);
try
{
mediaPlayer.setAudioStreamType(Interop.PRIMARY_STREAM);
mediaPlayer.setDataSource(m_soundFile);
mediaPlayer.prepare();
return true;
// Interop.logDebug(TAG + "-loadAudio: SUCCESS" + m_soundFile);
} catch (Exception e)
{
Interop.logError(TAG + "-LoadAudio for Clic Sound: audioPlayer prepare failed for current file: " + m_soundFile);
Interop.logError(TAG + "-Exception: " , e);
return false;
}
}
and than use
if (createMediaPlayer())
mediaPlayer.start();
this will ensure proper release of the resources used by the media player.
A simple solution is to Use pause instead of stop and the seek to the beginning of the song.
I know that this question is quite old but recently while learning Android, I also got stuck at this point and found a very simple solution which I'd like to share with everyone.
Instead of trying to stop or reset the media, you can just seek back to the starting position.
mediaPlayer.seekTo(0);
For reference, I am also posting my code below:
public class MainActivity extends AppCompatActivity {
MediaPlayer mp;
public void play(View view) {
mp.start();
}
public void pause(View view) {
mp.pause();
}
public void stop(View view) {
// this seeks to the beginning of the file
mp.seekTo(0);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp = MediaPlayer.create(this, R.raw.sample_audio);
}
}