I did not understand this problem, when i record a video through back camera then video view showing actually view which should showing in the video View but when i use front camera then its rotate to opposite. i am using one class for both then why i am getting this type behavior of video view?
video view code
public class VideoPreviewActivity extends Activity {
VideoView VideoPreview;
String videoURI ="VIDEO";
Button back;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_preview_screen);
VideoPreview = (VideoView) findViewById(R.id.video_preview);
back = (Button) findViewById(R.id.back);
Bundle bundle = getIntent().getExtras();
videoURI = bundle.getString("VIDEO");
PlayVideo();
back.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
Intent i = new Intent(VideoPreviewActivity.this, PicRecordShareActivity.class);
startActivity(i);
}
});
}
private void PlayVideo()
{
try
{
getWindow().setFormat(PixelFormat.TRANSLUCENT);
MediaController mediaController = new MediaController(VideoPreviewActivity.this);
mediaController.setAnchorView(VideoPreview);
// Toast.makeText(getApplicationContext(), "Video:\t"+videoIndex, Toast.LENGTH_LONG).show();
Uri video = Uri.parse(videoURI);
VideoPreview.setMediaController(mediaController);
VideoPreview.setVideoURI(video);
VideoPreview.requestFocus();
VideoPreview.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
public void onPrepared(MediaPlayer mp)
{
VideoPreview.start();
}
});
}
catch(Exception e)
{
System.out.println("Video Play Error :"+e.toString());
finish();
}
}
}
Thank You
Related
New to Android coding here. I'm trying to have the user pick an audio file for the MediaPlayer to play using the "upload" button, and then the MediaPlayer should start playing the file once the user hits "play." Here is my code so far:
public class MainActivity extends Activity {
final int ACTIVITY_CHOOSE_FILE = 1;
File original;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent chooseFile;
Intent intent;
chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("audio/mpeg");
intent = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult(intent, ACTIVITY_CHOOSE_FILE);
}
});
Button playButton = (Button)findViewById(R.id.playbutton);
playButton.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(original.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ACTIVITY_CHOOSE_FILE: {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
original = new File(uri.getPath());
String tag = "TAG";
Log.d(tag, original.getAbsolutePath());
}
}
}
}
}
I can choose the audio file fine, but once I hit play nothing happens. What am I doing wrong?
After setting your mediaPlayer data source you have to call mediaPlayer.prepareAsync()
Media Playback in android
I have a simple app where a video loads from a url and when you click on the video, the MediaController pops up.
This works fine, but I'm wondering if there is a way I can just skip that step and have it so that clicking the video automatically triggers the 'play' function of the MediaController without having to ever see it.
These videos are short and I don't need to be able to control them other than just pressing play.
Here is my code:
private static final String MOVIE_URL ="http://mywebsite.com/videos/testvideo.mkv";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView vid = (VideoView) findViewById(R.id.videoView);
Uri video = Uri.parse(MOVIE_URL);
vid.setMediaController(new MediaController(this));
vid.setVideoURI(video);
vid.seekTo(1);
vid.setOnClickListener(
new View.OnClickListener()
{
#Override
public void onClick(View view)
{
vid.start();
vid.requestFocus();
}
}
);
}
EDIT
I needed to use onTouchListener instead of onClickListener because it's a videoview. This works perfectly!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView vid = (VideoView) findViewById(R.id.videoView);
Uri video = Uri.parse(MOVIE_URL);
// vid.setMediaController(new MediaController(this));
vid.setVideoURI(video);
vid.seekTo(1);
vid.setOnTouchListener(
new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event) {
vid.start();
vid.requestFocus();
return false;
}
}
);
}
Add a PreparedListener to listen for video if prepared . When the VideoView is prepared to start the play
vid.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
vid.start();
vid.requestFocus();
}
});
vid.setVideoURI(video);
When setVideoURI, the instance of MediaPlayerwill be created and register for MediaPlayer setOnPreparedListener . So that when MediaPlayer is prepared the VideoView onPrepared listener will get called.
i want play many videos in same videoview
when click on button , The online video play in videoview in other layout
example :
there are three button in "layout 1"
after click on any button go to "layout 2"
there is one VideoView in "Layout 2 "
everyone action Click on Button is Change String Value "VideoURl" in VideoView
The question is : How can I change the value of the String variable in the Layout 2 When you click on any button in the Layout 1?!!]1
http://i.stack.imgur.com/ytQiE.jpg
public class MainActivity extends Activity{
Button button1;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(context, Main2Activity.class);
i.putExtra("VidURL1", "http://img580.imageshack.us/img580/416/4dojlrwiknaexgxaiyqfbz.mp4");
startActivity(i);
}
});
}
}
The Code activity 2 :
public class Main2Activity extends Activity {
private ProgressDialog pDialog;
VideoView videoview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoview = (VideoView) findViewById(R.id.VideoView);
new StreamVideo().execute();
}
private class StreamVideo extends AsyncTask {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Main2Activity.this);
pDialog.setTitle("Android Video Streaming Tutorial");
pDialog.setMessage("Buffering...");
pDialog.setIndeterminate(false);
pDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
#Override
protected void onPostExecute(Void args) {
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(
Main2Activity.this);
mediacontroller.setAnchorView(videoview);
// Get the URL from String VideoURL
Uri video = Uri.parse(VideoURL);
videoview.setMediaController(mediacontroller);
videoview.setVideoURI(video);
videoview.requestFocus();
videoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
pDialog.dismiss();
videoview.start();
}
});
} catch (Exception e) {
pDialog.dismiss();
Log.e("Error", e.getMessage());
e.printStackTrace();
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
you can pass data between two activity using intent
Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent)
and to get data from the activity1
String Url = getIntent().getExtras().getString("yourKey");
I am trying to design simple code in android for play video after a button click but i am getting unknown problem. My present project files below. I am clueless right now. Please point out a thing or solve this problem. Thanks again!
public class VideoActivity extends Activity {
VideoView video;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play = (Button)findViewById(R.id.button1);
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
myVideoView.setVideoPath("/mnt/usb_storage/vivek.3gp");
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();
}
});
}
}
btnPlay.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
try
{
videoView.setVideoPath(videoPath);
videoView.setMediaController(new MediaController(Applicaion Context or YourActivity.this));
videoView.start();
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Error in btnInfo Click -->"+e.toString());
}
}
});
//BtnPlay
I Use MediaController to play my audio. But, I want to remove Backward & Forward Button from my MediaController. Please, help me. (PS. A Code is be useful) How can I edit it.
public class Main extends Activity implements MediaPlayerControl {
private static final String PLAY_AUDIO = "PLAY_AUDIO";
private MediaController mMediaController
private MediaPlayer mMediaPlayer;
private Handler mHandler = new Handler();
static Context context;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
mMediaPlayer = new MediaPlayer();
mMediaController = new MediaController(this);
mMediaController.setMediaPlayer(Main.this);
mMediaController.setAnchorView(findViewById(R.id.audioView));
mMediaController.setPrevNextListeners(new View.OnClickListener() {
public void onClick(View v) {
//next button clicked
}
}, new View.OnClickListener() {
public void onClick(View v) {
//previous button clicked
}
});
try {
mMediaPlayer.setDataSource(
this,
Uri.parse("android.resource://com.app.audioplayer/raw/allforyou")
);
mMediaPlayer.prepare();
} catch (IOException e) {
Log.e(PLAY_AUDIO, "Could not open file " + " " + " for playback.", e);
}
mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mHandler.post(new Runnable() {
public void run() {
mMediaController.show(0);
mMediaPlayer.start();
}
});
}
});
}
}
Simplest solution ...
MediaController mediaController = new MediaController(this, false);
Courtesy : #Meow meo (in comments of original question)