i working videoview. i wrote code witch can play video from url with asynctask.at the moment everythink is ok but i have another problem.when i run my app videoview is not full screen.
[i have like this resulit 1
this is a my source
public class Layout1 extends Fragment {
VideoView video;
MediaController media;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#SuppressWarnings("deprecation")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout1, null);
video = (VideoView) view.findViewById(R.id.video_view);
new BackgroundAsyncTask()
.execute(Global.yutubeDownloadUrl);
return view;
}
public class BackgroundAsyncTask extends AsyncTask<String, Uri, Void> {
Integer track = 0;
ProgressDialog dialog;
protected void onPreExecute() {
dialog = new ProgressDialog(getActivity());
dialog.setMessage("Loading, Please Wait...");
dialog.setCancelable(true);
dialog.show();
}
protected void onProgressUpdate(final Uri... uri) {
try {
media=new MediaController(getActivity());
video.setMediaController(media);
media.setVisibility(View.GONE);
media.setPrevNextListeners(new View.OnClickListener() {
#Override
public void onClick(View v) {
// next button clicked
}
}, new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
media.show();
video.setVideoURI(uri[0]);
video.requestFocus();
video.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer arg0) {
video.start();
dialog.dismiss();
}
});
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
}
#Override
protected Void doInBackground(String... params) {
try {
Uri uri = Uri.parse(params[0]);
publishProgress(uri);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
and xml code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<VideoView
android:id="#+id/video_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
what am i doing wrong? if anyone knows solution please help me
The easy way is to change your activity theme to
#android:style/Theme.Holo.NoActionBar.Fullscreen
or
#android:style/Theme.Black.NoTitleBar.Fullscreen
(as long as it has fullscreen in the name it's fine).
if you really need the videoview to be in a fragment there might be some problems, but as i see your code you can use an activity instead of that fragment and set the theme for that activity.
By the way... you don't need to use an async.
Related
I am capturing a video using Cameraview, once the recording is finished, the app will playback the video in fullscreen. However after I record the first video, I am only seeing a black screen. If I ress back and record another video everything works and the playback is successful. The errors I am having are:
E/MediaPlayer: error (1, -2147483648)
E/MediaPlayer: isPlaying: called in state MEDIA_PLAYER_STATE_ERROR
here is the code sample:
public class VideoPreviewActivity extends AppCompatActivity {
FullscreenVideoLayout videoLayout;
CustomReusingDialog mCustomReusingDialog;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_preview);
videoLayout = (FullscreenVideoLayout) findViewById(R.id.videoview);
videoLayout.setActivity(this);
final Uri videoUri = getIntent().getParcelableExtra("video");
try {
videoLayout.setVideoURI(videoUri);
} catch (IOException e) {
e.printStackTrace();
}
videoLayout.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
playVideo();
}
});
videoLayout.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mCustomReusingDialog =CustomReusingDialog.newInstance(new CustomReusingDialog.Builder(VideoPreviewActivity.this, new CustomReusingDialog.OnDialogShowListener() {
#Override
public void onGetDialog(final Dialog mDialog, View mView) {
TextView uploadText =(TextView)mView.findViewById(R.id.uploadText);
TextView deleteText =(TextView)mView.findViewById(R.id.deleteText);
uploadText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mCustomReusingDialog.dismiss();
if (LoginManager.isNetworkAvailable(getApplicationContext()))
new UploadManager(VideoPreviewActivity.this,new File(videoUri.getPath())).execute();
else
LoginManager.showMessage(getApplicationContext(),"Please Check your Network Settings!!");
}
});
deleteText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mCustomReusingDialog.dismiss();
UploadManager.deleteFile(VideoPreviewActivity.this,new File(videoUri.getPath()));
}
});
}
}).setDialogLayout(R.layout.dialog_upload).setCancelAble(true));
mCustomReusingDialog.show(getSupportFragmentManager(),"");
}
});
}
void playVideo() {
if (videoLayout.isPlaying()) return;
videoLayout.start();
}
#Override
protected void onPause() {
super.onPause();
}
}}
hello I am very new to android programming.I want Button in first activity and when it is clicked second activity will open and plays mp3 file from sd card i have mention path but mp3 file not plays after running project.I am posting my player.java
This is my class
public class player extends AppCompatActivity {
Button btPv,btplay,btStop,btPause;
SeekBar sb;
MediaPlayer mp3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
;
btplay=(Button)findViewById(R.id.btplay);
btStop=(Button)findViewById(R.id.btstop);
btPause=(Button)findViewById(R.id.btpause);
mp3=new MediaPlayer();
try {
mp3.setDataSource("sdcard/musicblee/Over_the_horizon.mp3");
mp3.prepare();
} catch (IOException e) {
e.printStackTrace();
}
btplay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp3.start();
}
});
btStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
mp3.stop();
mp3.reset();
mp3.setDataSource("sdcard/musicblee/Over_the_horizon.mp3");
mp3.prepare();
} catch (Exception e) {
e.printStackTrace();
}
}
});
btPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
mp3.pause();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
refer to this link use mp3.setDataSource() like that
mp3 = MediaPlayer.create(this,Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/musicblee/Over_the_horizon.mp3"));
to set mp3 source.
And Also add this permission to your Android Manifest file :
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Below is my Code :
package com.example.videoplayer;
import android.app.Activity;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.MediaController;
import android.widget.VideoView;
public class VideoPlayerActivity extends Activity {
String TAG = "com.example.VideoPlayer";
ProgressDialog progDailog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player);
final VideoView videoView = (VideoView) findViewById(R.id.videoView1);
videoView.setVideoPath("http://www.ebookfrenzy.com/android_book/movie.mp4");
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setOnPreparedListener(new
MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
progDailog.dismiss();
Log.i(TAG, "Duration = " + videoView.getDuration());
}
});
videoView.start();
progDailog = ProgressDialog.show(this, "Please wait ...", "Retrieving data ...", `enter code here`true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.video_player, menu);
return true;
}
}
Check out this :
Play http videos in VideoView
This is my code :
public static void getVideoFromHttp(String urlPath) {
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(mContext);
mediacontroller.setAnchorView(mVideoview);
// Get the URL from String VideoURL
Uri mVideo = Uri.parse(urlPath);
mVideoview.setMediaController(mediacontroller);
mVideoview.setVideoURI(mVideo);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
mVideoview.requestFocus();
mVideoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
mVideoview.start();
}
});
mVideoview.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
}
});
}
Thanks
Don't do everything in the UI thread because it takes a lot of time. Do everything in AsyncTask and things will work out fine.
For beginners, this link describes how could you do it in AsyncTask.
Define a class which extends the AsyncTask:
public class BackgroundAsyncTask extends AsyncTask<String, Uri, Void> {
Integer track = 0;
ProgressDialog dialog;
protected void onPreExecute() {
dialog = new ProgressDialog(PlayVideo.this);
dialog.setMessage("Loading, Please Wait...");
dialog.setCancelable(true);
dialog.show();
}
protected void onProgressUpdate(final Uri... uri) {
try {
media=new MediaController(PlayVideo.this);
video.setMediaController(media);
media.setPrevNextListeners(new View.OnClickListener() {
#Override
public void onClick(View v) {
// next button clicked
}
}, new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
media.show(10000);
video.setVideoURI(uri[0]);
video.requestFocus();
video.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer arg0) {
video.start();
dialog.dismiss();
}
});
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
protected Void doInBackground(String... params) {
try {
Uri uri = Uri.parse(params[0]);
publishProgress(uri);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Then, in the onCreate() method, just call the execute() method of this BackgroundAsynchTask:
video = (VideoView) findViewById(R.id.video);
new BackgroundAsyncTask().execute("link-to-the-video");
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 have three layouts:
Layout1
-->onClick()-->show
Layout2
-->wait three seconds-->show
Layout3
The problem is that Layout2 is not shown. To set the layouts I use
setContentView(int);
The relevant code might be:
public class TrainingActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
final Button inputButton = (Button)findViewById(R.id.inputButton);
inputButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
changeLayouts();
}
});
}
public void changeLayouts() {
setContentView(R.layout.layout2);
try {
TimeUnit.MILLISECONDS.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
setContentView(R.layout.layout3);
}
}
My idea was that Android might use something like an "Event-Loop" (like Qt) and my method would block the control to get back to the "Event-Loop" which would make the layout displayed.
But I couldn't find my error.
The problem why your layout2 is not shown is because of TimeUnit.MILLISECONDS.sleep(3000); - what you are doing here is you put your UI thread into sleep, so UI thread cannot process your request to change layout. And when it wakes up - it immediately sets layout3 that's why layout2 is not shown.
You might consider using Handler.postDelayed(Runnable, long) to postpone execution
So this should work as you expected:
public class TrainingActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
final Button inputButton = (Button)findViewById(R.id.inputButton);
inputButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
changeLayouts();
}
});
}
public void changeLayouts() {
setContentView(R.layout.layout2);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
setContentView(R.layout.layout3);
}
}, 3000);
}
}
Try this, it will surely work
public void changeLayouts() {
setContentView(R.layout.layout2);
Thread Timer = new Thread(){
public void run(){
try{
sleep(3000);
} catch(InterruptedException e){
e.printStackTrace();
} finally {
setContentView(R.layout.layout3);
}
}
}; Timer.start();
}