Using the following code I can play a video on android in a new Activity. I would however like to preload the video (during a loading screen) and then show it when it is fully loaded. Is it possible to perhaps somehow trigger the activity to show later? In theory It would also be ok to not use a different activity.
public class VideoPlayer extends Activity implements OnCompletionListener,OnPreparedListener
{
private VideoView mVV;
#Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.videoplayer);
String url = getIntent().getStringExtra("url");
if(url == null)
finish();
mVV = (VideoView)findViewById(R.id.myvideoview);
mVV.setOnCompletionListener(this);
mVV.setOnPreparedListener(this);
mVV.setVideoURI(Uri.parse(url));
mVV.start();
}
public void stopPlaying() {
mVV.stopPlayback();
this.finish();
}
#Override
public void onCompletion(MediaPlayer mp) {
finish();
}
#Override
public void onPrepared(MediaPlayer mp) {
}
}
from my main activity:
private void playVideo(String url) {
Intent videoPlaybackActivity = new Intent(this, VideoPlayer.class);
videoPlaybackActivity.putExtra("url", url);
startActivity(videoPlaybackActivity);
}
I presume I can use the onPrepared function, but I'm not sure how to do the activity triggering to show the activity later.
http://developer.android.com/reference/android/widget/VideoView.htmlHere are the methods for the VideoView. So there is indeed a:
setOnPreparedListener(MediaPlayer.OnPreparedListener l)
Probably the best way is to use fragments in the activity and show the fragment in the activity where the mVV = (VideoView)findViewById(R.id.myvideoview); is declared when the onprepared is called.
Or hide and show the view in the activity. (using fragments is probably nicer).
Think that will work and that that is what you wanted?
Related
Whenever i click on music, i want to launch an Activity that play the selected song, however i can't seems to get this right. I have tried many codes, but all to no avail.
Here is the button that will launch the activity
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> adapter, View v, int position, long id){
Intent i = new Intent(getActivity(), MusicPlaying.class);
i.putExtra("song", position);
startActivity(i);
}
});
And below is the Activity that receive intent from the above button.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music_playing);
value = getIntent().getIntExtra("song", 0);
...
if (mp.isPlaying()) {
mp.stop();
} else {
playMusic();
}
...
}
Edit: The app plays two songs at a time instead of playing only selected song.
You should be very careful with MediaPlayer state transactions, check diagram.
For example when you call stop(), you cannot call start() again until you prepare the MediaPlayer again.
In your case it looks like you didn't release MediaPlayer when the host Activity destroyed (ex. screen rotation), can be done like that
#Override
public void onDestroy() {
if (mp != null)
mp.release();
super.onDestroy();
}
I'm using a small 2 second sound effect attached to a button. When clicked, the sound effect plays and the user is taking to the next activity. The function I created is outside onCreate and uses Intent to send the user to the next activity. When I add the mp variables I get an error saying there's a problem with playGame(). What is causing the MediaPlayer to not play when it's placed in this function? Eclipse suggests changing mp.create() to MediaPlayer.create but that doesn't fix the issue.
public class SplashScreenActivity extends ActionBarActivity {
public MediaPlayer mp;
public void playGame(View view) {
mp.create(this, R.raw.bulletricochet);
mp.start();
// Do something in response to button
Intent intent = new Intent(this, QuizActivity.class);
startActivity(intent);
}
public void playRules(View view) {
Intent intentR = new Intent(this, RulesActivity.class);
startActivity(intentR);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
}}
Your mp object is null and yet you are calling create method on it.
To create a MediaPlayer object use MediaPlayer.create. You say you tried it but probably didn't assign the return value to your mp variable.
mp = MediaPlayer.create(this, R.raw.bulletricochet);
In my Android app, I am trying to simply go back to my main Activity once a video that I am playing ends. I have tried many workarounds, but I can't find a way to call StartActivity from the video onCompletionListener - I am getting the "cannot make a static reference to the non-static method startActivity(Intent) from the type Activity" error.
I tried getting a context from the Activity that preceded the videoView, and passing that to the intent/startActivity. That allowed the app to compile, but then I got a runtime exception.
Here is the code as it stands now, which gets the "cannot make a static reference" error - any help would be appreciated!
public class Videoscreen extends Activity{
public static VideoView myVideoView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoplay);
myVideoView = (VideoView) findViewById(R.id.main_videoview);
System.out.println("playing video oncreate");
playVideo();
}
public static void playVideo(){
// video finish listener
myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer vmp) {
Intent intent = new Intent();
intent.setClass(Videoscreen.this, Game.class);
Videoscreen.startActivity(intent);
}
});
String low_word = SpellingView.get_low_word();
Uri bubblesUri = Uri.parse("android.resource://org.lalloinc.ilovetrucks/raw/"+ low_word + "_vid");
myVideoView.setVideoURI(bubblesUri);
myVideoView.start();
}
}
If you know the time of the video then you try:
String uri1 = "android.resource://" + getPackageName() + "/" + R.raw.race3;
vd.setVideoURI(Uri.parse(uri1));
vd.start();
new Thread() {
public void run() {
try{
sleep(50000);
} catch (Exception e) {
}
Intent intent = new Intent(Video.this, Another.class);
startActivity(intent);
finish();
}
}.start();
if you don't know the time then you get the time as:
int vtime = vd.getDuration();
And then at thread sleep you just put this integer.
If you started the Video Activity from the Activity you would like to go back to later, just calling finish() at the end of the video will do the job.
Starting the main Activity again creates a not necessarily wanted stack of activities.
This below activity works fine but the mediaController display only if I click on the screen. And the second problem is the media controller display only for 3 sec. what should I do to remove this problem?
public class PlayingActivity extends Activity
{
private VideoView mVideoView;
private EditText mPath;
MediaController mediaController;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playingactivity);
mPath = (EditText) findViewById(R.id.path);
mPath.setText(GlobalVariable.getstrEmail());
mVideoView = (VideoView) findViewById(R.id.surface_view);
Uri uri = Uri.parse("/sdcard/download/test.mp3");
mediaController = new MediaController(this);
mediaController.findFocus();
mediaController.setEnabled(true);
mediaController.show(0);
mediaController.setAnchorView(mVideoView);
mVideoView.setMediaController(mediaController);
mVideoView.setVideoURI(uri);
mVideoView.start();
}
}
mediaController.requestFocus();
will make it display as soon as the video starts ( without requiring the click)
and
mVideoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaController.show(0);
}
});
Will keep it on the screen.
Hope it helps
Requesting focus or specifying 0 in show method never worked for me.
The problem is that MediaController class has default timeout of 3000ms or 3seconds. And its show() method replaces our given parameter to its default parameter. Its a stupid bug resulting from untested code at Google.
We need to implement a lousy workaround of replacing the default value by desired value.
Try the below code. It should work.
mediaControls = new MediaController(getActivity()){
#Override
public void show (int timeout){
if(timeout == 3000) timeout = 20000; //Set to desired number
super.show(timeout);
}
};
mVideoView.setMediaController(mediaControls);
Neo's suggestions are perfect. But I would like to add "mp.start()" to onPrepared(MediaPlayer mp) method, without which the media file won't start playing.
There are two main problem in MediaController:
auto hide is 3s by default
Tapping on the video shows/hide the control bar
For the first part it's easly fixed changing the default timeout value of start to zero (zero means indefinite,it is used internally as the video starts) like this:
mediaController = new MediaController(this){
#Override
public void show() {
super.show(0);//Default no auto hide timeout
}
};
The second problem is a little tricky because the click handler is declared as private and final so we do not have any control on that.
My solution is to use another function to set visibility and disable the hide function like this:
mediaController = new MediaController(this){
#Override
public void show() {
super.show(0);//Default no auto hide timeout
}
#Override
public void hide() {
//DOES NOTHING
}
void setVisible(boolean visible){//USE THIS FUNCTION INSTEAD
if(visible)
super.show();
else
super.hide();
}
};
You can also add a variable to re-enable standard functionality if visibility is set to false like so:
mediaController = new MediaController(this){
private boolean forceVisible=false;
#Override
public void show() {
super.show(0);//Default no auto hide timeout
}
#Override
public void hide() {
if(!forceVisible)super.hide();
}
void setVisible(boolean visible){
forceVisible=visible;
if(visible)
super.show();
else
super.hide();
}
};
I'm trying to make my app to play intro clip for only when I start activities.
But from my code it's always play the clip after wakeup before resume to app although I did not closed the app. What can I do to fix this prob?
From main:
startActivity(new Intent(this, MyIntro.class));
From MyIntro:
public class MyIntro extends Activity implements OnCompletionListener {
int a;
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.intro);
playIntro();
}
public void onConfigurationChanged(Configuration newConfig) {
setContentView(R.layout.intro);
}
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
this.finish();
}
private void playIntro(){
setContentView(R.layout.intro);
VideoView video = (VideoView) this.findViewById(R.id.VideoView01);
Uri uri = Uri.parse("android.resource://real.app/" + R.raw.intro);
video.setVideoURI(uri);
video.requestFocus();
video.setOnCompletionListener(this);
video.start();
}
}
What function have you overridden in your main Activity - the one where you call
startActivity(new Intent(this, MyIntro.class))
?
I would assume it's onResume() and the line above is executed too many times, because of that. Read again the explanation of Activity lifecycle here, it's the first thing I do, when I have problems like that.
Get back to us with a little more info about the main Activity.
Evil hack:
Add a static pointer to your own activity, fill or override it when "onCreate" gets called. If it's null, play your movie, otherwise, don't.
You could do the same with a static boolean really.
private static boolean isRunning = false;
protected void onCreate(Bundle bundle) {
super.onCreate(bundle)
if(!isRunning)
{
isRunning = true;
//Play your video here
}
}
There are much more elegant and correct ways of doing this, but if you're in a hurry this will probably work.