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();
}
Related
I have an activity (A), it has a VideoView. I setup video view with onPrepareListener. When I start new activity (B) from A. After that, I come back to A from B, all element in onPrepared is called??? Why it happen??? So I'm sorry my english not good, thank you :D
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getBaseContext(), SecondActivity.class));
}
});
mVideoView = (VideoView) findViewById(R.id.video_view);
mVideoView.setVideoPath(
"/storage/emulated/0/Download/Charlize Theron's Variety Cover Shoot - Behind the Scenes.3gpp");
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer m) {
Log.i("test", m.toString());
}
});
I am trying to build a small app which plays a sound when we click on the button. But I am not able to play the sound. Don't know what the problem is. Please help me on this. Below is the code.
public class MainActivity extends AppCompatActivity {
private Button button;
private MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = new MediaPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(),R.raw.song);
button = (Button)findViewById(R.id.mediaButtonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer.start();
}
});
}
}
Note:-Sorry guys,I thought that the problem is with my code but the app is running perfectly fine on my phone,so its the problem with my genymotion emulator.Can anyone please suggest me the solution for this.By the way,I am using Mac OSX.
The MediaPlayer has its own lifecycle. You can't just create the instance and then start to play. First you have to prepare it and then play it.
You can prepare your mediaplayer either sync or asynchronously.
Something along the lines of:
MediaPlayer mediaPlayer= new MediaPlayer();
mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.song);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
mediaPlayer.prepareAsync();
Or, if you want to do it synchronously
MediaPlayer mediaPlayer= new MediaPlayer();
mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.song);
try {
mediaPlayer.prepare();
} catch (IOException e){
}
mediaPlayer.start();
Just make sure you prepare it before you play it.
Media Player lifecycle: https://developer.android.com/reference/android/media/MediaPlayer.html
You need to make sure the media player is ready before you can play it, so you set the onPreparedListener to handle this for you, like so:
MediaPlayer mp = new MediaPlayer();
mp = MediaPlayer.create(getApplicationContext(),R.raw.song);
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp.start();
}
});
button = (Button)findViewById(R.id.mediaButtonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.prepareAsync();
}
});
There will be a slight delay from when you press the button to the sound playing this way. Another way to do it could be to disable the button until the media player has prepared and then in the onclick of the button you could just call mp.start(); when the button has been enabled.
I am having a listview in my app. Each listview item has button which will play an audio from a url received from web service. But my problem is that if I click play button from the next item then both start playing together. I am having problem in this. I want only one to play at a time. Right now I am creating new Media player object everytime button is clicked, but I also tried creating a single global object but in this case it only plays first time and not after it. What is the possible solution of it.
finalHolder.iv_sound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(birdsUrlList.get(position).getUrl_audio());
mp.prepare();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
//startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(birdsUrlList.get(position).getUrl_video())));
} catch (Exception e) {
e.printStackTrace();
}
}
});
Make the mp variable global and remove this:
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
because if you take a look at this reference http://developer.android.com/reference/android/media/MediaPlayer.html#StateDiagram it says:
Once the MediaPlayer object is in the End state, it can no longer be
used and there is no way to bring it back to any other state.
And when you call mp.release(); the media player WILL go to that state.
Then make your onClickListener look something like this:
#Override
public void onClick(View v) {
try {
if (mp.isPlaying()) {
mp.stop();
mp.reset();
}
mp.setDataSource(birdsUrlList.get(position).getUrl_audio());
//... and so on
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.reset();
}
});
The trick there is to make it back to Idle state so you can set the new data source and start playing again. It's all about the states...
How do i use the OnCompletion listener for some music?
I would like to press a button to go to another activity that plays some music and then goes back when the music playback is finished. I allready coded the other stuff. I just cant figure out how to use the OnCompletion listener?
You should put the code that should be run when the music is completed in the OnCompletionListener, for example:
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
finish(); // finish current activity
}
});
mPlayer.setOnErrorListener(new OnErrorListener() {
public boolean onError(MediaPlayer paramMediaPlayer, int paramInt1,int paramInt2) {
// TODO Auto-generated method stub
//your code if any error occurs while playing even you can show an alert to user
return true;
}
});
mPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
//your code if the file was completely played either show an alert to user or start another activity or file.
//even you can finish you activity here
}
});
I find that above are correct however I was struggling on where to place the code.
See below, i place this code after my code to start the tune!
playButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer.start(); //Next line is the beginning of where to place the code.
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
Toast.makeText(MainActivity.this, "I'm Finished", Toast.LENGTH_SHORT).show();
}
});
}
});
here is the kotlin version of setOnCompletionListener :
mediaPlayer.setOnCompletionListener(MediaPlayer.OnCompletionListener { it // this is MediaPlayer type
Log.d(TAG,"setOnCompletionListener OnCompletionListener called")
// do other task
})
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();