Android how to set Media Player (Videoview) on Button click?
I write a app that plays 3gp videos from url, everything works fine but just can open a video from an activity, that would mean I have to set an activity for each movie I will show in my app, and that would be to many.
I will open my video whit a button click and so that I can write 20 links in a single activity!
Here is my code witch works as a single activity:
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class videoact extends Activity implements OnPreparedListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.videoview);
MediaController mc = new MediaController(this);
VideoView vv = (VideoView)findViewById(R.id.view1);
try
{
Uri ur = Uri.parse("rtsp://v4.cache4.c.youtube.com/CjgLENy73wIaLwkHCCLABs2fehMYJCAkFEIJbXYtZ29vZ2xlSARSB3JlbGF0ZWRg7e7u0PeUj8hMDA==/0/0/0/video.3gp");
vv.setVideoURI(ur);
vv.setMediaController(mc);
vv.requestFocus();
vv.start();
mc.show();
}
catch(Exception e)
{
System.out.print(e.getMessage() + "error");
}
}
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
}
}
and if I try to make a button in front of it:
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
public class videoact extends Activity implements OnPreparedListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next1 = (Button) findViewById(R.id.button1);
next1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//here Eclipse say"The constructor MediaController(new View.OnClickListener(){}) is undefined"
//and gives an error
MediaController mc = new MediaController(this);
VideoView vv = (VideoView)findViewById(R.id.view1);
try
{
Uri ur = Uri.parse("rtsp://v4.cache4.c.youtube.com/CjgLENy73wIaLwkHCCLABs2fehMYJCAkFEIJbXYtZ29vZ2xlSARSB3JlbGF0ZWRg7e7u0PeUj8hMDA==/0/0/0/video.3gp");
vv.setVideoURI(ur);
vv.setMediaController(mc);
vv.requestFocus();
vv.start();
mc.show();
}
catch(Exception e)
{
System.out.print(e.getMessage() + "error");
}
}
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
}
}
In the second version I change the set content view to "main" there are a button from where the button1 should start to play the video!
Update:
Maybe I cant explain my self! What I need to do is I have a activity( MainActivity) were I load a layout. In this layout I have the covers of different movies the covers are buttons. If I pres the cover the media player should start play the code I write before, my problem is the code I write is in a another activity lets call him (Movie1), if i do it this way I have to create for each movie one activity and this is to much I think, lets say it would be more than 4开发者_JAVA百科0 activities, if you say 40 activities is not much than I have no problem, or just tell me hove to say my activity (MainAcitivity) for example on button click open video from URL file without open Movie1 activity!
I think I have to create a blank MediaPlayer activity where I can say load by button click x this URL with my MediaPlayer activity but I don't know how.
精彩评论