I am trying to make a video player in android.
It plays the 3GP format videos.
But it does not support the mp4 video.Below is my code in android for the same.Why does it not support the mp4 format on the device and emulator?
package com.example.videoplayer;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.URLUtil;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.VideoView;
public class VideoViewDemo extends Activity {
private static final String TAG = "VideoViewDemo";
private VideoView mVideoView;
private EditText mPath;
private ImageButton mPlay;
private ImageButton mPause;
private ImageButton mReset;
private ImageButton mStop;
private String current;
#SuppressLint({ "NewApi", "NewApi", "NewApi" })
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mPath = (EditText) findViewById(R.id.path);
mPath.setText("ooklnet.com/files/368/368007/video.mp4");
mPlay = (ImageButton) findViewById(R.id.play);
mPause = (ImageButton) findViewById(R.id.pause);
mReset = (ImageButton) findViewById(R.id.reset);
mStop = (ImageButton) findViewById(R.id.stop);
mPlay.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
playVideo();
}
});
mPause.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.pause();
}
}
});
mReset.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.seekTo(0);
}
}
});
mStop.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
current = null;
mVideoView.stopPlayback();
}
}
});
/*runOnUiThread(new Runnable(){
public void run() {
sleep(2000);
playVideo();
}
});*/
Thread _trd1 = new Thread() {
public void run() {
try {
sleep(2000);
runOnUiThread(new Runnable() {
public void run() {
playVideo();
}
});
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
_trd1.start();
// new DoBackgroundTask().execute();
}
public class DoBackgroundTask extends AsyncTask
<String, Void, String> {
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
}
protected String doInBackground(String... locationNames) {
playVideo();
return null;
}
protected void onPostExecute(String addresses){
}
}
private void playVideo() {
try {
final String path = mPath.getText().toString();
Log.v(TAG, "path: " + path);
if (path == null || path.length() == 0) {
Toast.makeText(VideoViewDemo.this, "File URL/path is empty",
Toast.LENGTH_LONG).show();
} else {
// If the path has not changed, just start the media player
if (path.equals(current) && mVideoView != null) {
mVideoView.start();
mVideoView.requestFocus();
return;
}
current = path;
mVideoView.setVideoPath(getDataSource("ooklnet.com/files/368/368007/video.mp4"));
mVideoView.start();
mVideoView.requestFocus();
}
} catch (Exception e) {
Log.e(TAG, "error: " + e.getMessage(), e);
if (mVideoView != null) {
mVideoView.stopPlayback();
}
}
}
private String getDataSource(String path) throws IOException {
if (!URLUtil.isNetworkUrl(path)) {
return path;
} else {
URL url = new URL(path);
URLConnection cn = url.openConnection();
cn.connect();
InputStream stream = cn.getInputStream();
if (stream == null)
throw new RuntimeException("stream is null");
File temp = File.createTempFile("mediaplayertmp", "dat");
temp.deleteOnExit();
String tempPath = temp.getAbsolutePath();
FileOutputStream out = new FileOutputStream(temp);
byte buf[] = new byte[128];
do {
int numread = stream.read(buf);
if (numread <= 0)
break;
out.write(buf, 0, numread);
} while (true);
try {
stream.close();
} catch (IOException ex) {
Log.e(TAG, "error: " + ex.getMessage(), ex);
}
return tempPath;
}
}
}
Please advise as soon as possible.
Thanks.
The problem might be with the video encoding. Android Froyo and Gingerbread doesn't support H264 formats other than "Baseline" H264. So if your video is Mp4 & H264 encoded make sure its "AVC baseline" encoded. Use some tools like "Media info" in windows/Linux and check your video encoding. Convert the video to Baseline if possible.
An alternative workaround is to skip the Videoview and use a video play intent and redirect the playback to an app. User will be prompted to pick a player to handle the playback. Obviously if the video view cant play the file, the default player also wont be able to handle the file. you can choose some other installed player like Mx-Player which will stream the file perfectly.
Hope that solved your issue.
Try correct video url with protocol: http://www.ooklnet.com/files/368/368007/video.mp4
Related
I know how to record an audio file using MediaRecorder.
I want to upload the audio file on server, but before that, I want to compress it.
NOTE: I am recording audio in .3gpp format.
This is my class:
package com.iotaconcepts.task3;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import java.io.File;
import java.io.IOException;
public class MainActivity extends AppCompatActivity
{
MediaPlayer mediaPlayer;
MediaRecorder recorder;
String OUTPUT_FILE;
Button beginRecording, stopRecording, playRecording, stopPlayback;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OUTPUT_FILE = Environment.getExternalStorageDirectory() + "/audiorecorder.3gpp";
beginRecording = (Button)findViewById(R.id.bt_begin);
stopRecording = (Button)findViewById(R.id.bt_stop_recording);
playRecording = (Button)findViewById(R.id.bt_play_recording);
stopPlayback = (Button)findViewById(R.id.bt_stop_playback);
beginRecording.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
beginRecording();
} catch (IOException e) {
e.printStackTrace();
}
}
});
stopRecording.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopRecording();
}
});
playRecording.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
playRecording();
} catch (IOException e) {
e.printStackTrace();
}
}
});
stopPlayback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlayback();
}
});
}
public void beginRecording() throws IOException {
// If media recorder is working ... stop it
ditchMediaRecorder();
File outFile = new File(OUTPUT_FILE);
if(outFile.exists())
outFile.delete();
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(OUTPUT_FILE);
recorder.prepare();
recorder.start();
}
public void stopRecording()
{
if (recorder != null)
recorder.stop();
}
public void playRecording() throws IOException
{
ditchMediaPlayer();
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(OUTPUT_FILE);
mediaPlayer.prepare();
mediaPlayer.start();
}
public void stopPlayback()
{
if (mediaPlayer != null)
{
mediaPlayer.stop();
}
}
public void ditchMediaRecorder()
{
if(recorder!=null)
{
recorder.release();
}
}
public void ditchMediaPlayer()
{
if (mediaPlayer != null)
{
mediaPlayer.release();
}
}
}
I have already included the required permissions in manifest.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
If you are looking for fast lossless compression have a look at LZ4: https://github.com/jpountz/lz4-java. It provides two compression methods: fast scan (LZ4) and high compression (LZ4 HC). The jar file can be found here: http://java2s.com/Code/Jar/l/Downloadlz4120jar.htm
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
In my application I want to play the music file from online server.
How do I implement the music streaming in my application.
I can't find any suitable solution on the internet.
package org.apache.android.media;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.URLUtil;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.VideoView;
public class VideoViewDemo extends Activity {
private static final String TAG = "VideoViewDemo";
private VideoView mVideoView;
private EditText mPath;
private ImageButton mPlay;
private ImageButton mPause;
private ImageButton mReset;
private ImageButton mStop;
private String current;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mPath = (EditText) findViewById(R.id.path);
mPath.setText("http://daily3gp.com/vids/747.3gp");
mPlay = (ImageButton) findViewById(R.id.play);
mPause = (ImageButton) findViewById(R.id.pause);
mReset = (ImageButton) findViewById(R.id.reset);
mStop = (ImageButton) findViewById(R.id.stop);
mPlay.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
playVideo();
}
});
mPause.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.pause();
}
}
});
mReset.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.seekTo(0);
}
}
});
mStop.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
current = null;
mVideoView.stopPlayback();
}
}
});
runOnUiThread(new Runnable(){
public void run() {
playVideo();
}
});
}
private void playVideo() {
try {
final String path = mPath.getText().toString();
Log.v(TAG, "path: " + path);
if (path == null || path.length() == 0) {
Toast.makeText(VideoViewDemo.this, "File URL/path is empty",
Toast.LENGTH_LONG).show();
} else {
// If the path has not changed, just start the media player
if (path.equals(current) && mVideoView != null) {
mVideoView.start();
mVideoView.requestFocus();
return;
}
current = path;
mVideoView.setVideoPath(getDataSource(path));
mVideoView.start();
mVideoView.requestFocus();
}
} catch (Exception e) {
Log.e(TAG, "error: " + e.getMessage(), e);
if (mVideoView != null) {
mVideoView.stopPlayback();
}
}
}
private String getDataSource(String path) throws IOException {
if (!URLUtil.isNetworkUrl(path)) {
return path;
} else {
URL url = new URL(path);
URLConnection cn = url.openConnection();
cn.connect();
InputStream stream = cn.getInputStream();
if (stream == null)
throw new RuntimeException("stream is null");
File temp = File.createTempFile("mediaplayertmp", "dat");
temp.deleteOnExit();
String tempPath = temp.getAbsolutePath();
FileOutputStream out = new FileOutputStream(temp);
byte buf[] = new byte[128];
do {
int numread = stream.read(buf);
if (numread <= 0)
break;
out.write(buf, 0, numread);
} while (true);
try {
stream.close();
} catch (IOException ex) {
Log.e(TAG, "error: " + ex.getMessage(), ex);
}
return tempPath;
}
}
}
Reference: https://davanum.wordpress.com/2009/12/04/android-%E2%80%93-videomusic-player-sample-take-2/
Hi i have made a custom video played in android.with some simple "play","pause","play again" and "capture" button.NOw i have done all this functionalities except "capture".I have seacrched lot many links and googled a lot,But i can't find how to capture a frame from running video in android? my code is as below: please help me for is:
Main.java
package org.apache.android.media;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.URLUtil;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.VideoView;
public class VideoViewDemo extends Activity {
private static final String TAG = "VideoViewDemo";
private VideoView mVideoView;
private EditText mPath;
private ImageButton mPlay;
private ImageButton mPause;
private ImageButton mReset;
private ImageButton mStop;
private ImageButton mcaptur;
private String current;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mPath = (EditText) findViewById(R.id.path);
mPath.setText("http://daily3gp.com/vids/747.3gp");
mPlay = (ImageButton) findViewById(R.id.play);
mPause = (ImageButton) findViewById(R.id.pause);
mReset = (ImageButton) findViewById(R.id.reset);
mStop = (ImageButton) findViewById(R.id.stop);
mcaptur = (ImageButton) findViewById(R.id.Captur);
mPlay.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
playVideo();
}
});
mPause.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.pause();
}
}
});
mReset.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.seekTo(0);
}
}
});
mStop.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
current = null;
mVideoView.stopPlayback();
}
}
});
runOnUiThread(new Runnable() {
public void run() {
playVideo();
}
});
mcaptur.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
private void playVideo() {
try {
final String path = mPath.getText().toString();
Log.v(TAG, "path: " + path);
if (path == null || path.length() == 0) {
Toast.makeText(VideoViewDemo.this, "File URL/path is empty",
Toast.LENGTH_LONG).show();
} else {
// If the path has not changed, just start the media player
if (path.equals(current) && mVideoView != null) {
mVideoView.start();
mVideoView.requestFocus();
return;
}
current = path;
mVideoView.setVideoPath(getDataSource(path));
mVideoView.start();
mVideoView.requestFocus();
}
} catch (Exception e) {
Log.e(TAG, "error: " + e.getMessage(), e);
if (mVideoView != null) {
mVideoView.stopPlayback();
}
}
}
private String getDataSource(String path) throws IOException {
if (!URLUtil.isNetworkUrl(path)) {
return path;
} else {
URL url = new URL(path);
URLConnection cn = url.openConnection();
cn.connect();
InputStream stream = cn.getInputStream();
if (stream == null)
throw new RuntimeException("stream is null");
File temp = File.createTempFile("mediaplayertmp", "dat");
temp.deleteOnExit();
String tempPath = temp.getAbsolutePath();
FileOutputStream out = new FileOutputStream(temp);
byte buf[] = new byte[128];
do {
int numread = stream.read(buf);
if (numread <= 0)
break;
out.write(buf, 0, numread);
} while (true);
try {
stream.close();
} catch (IOException ex) {
Log.e(TAG, "error: " + ex.getMessage(), ex);
}
return tempPath;
}
}
}
You can get a video frame with MediaMetadataRetriever. The basic usage is as follows.
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
// Set data source to retriever.
// From your code, you might want to use your 'String path' here.
retriever.setDataSource(yourPath);
// Get a frame in Bitmap by specifying time.
// Be aware that the parameter must be in "microseconds", not milliseconds.
Bitmap bitmap = retriever.getFrameAtTime(timeInMicroSeconds);
// Do something with your bitmap.
You might want to use FFmpegMediaMetadataRetriever for better performance.
I too have searched the same for some days and finally i came across opencv library through that we can able to read the frames from a live video and do the operations accordingly.
I have did successfully and keeping the project in github for your reference.
https://github.com/Karthi96/Video-Recorder-with-Frames-Analysis
Let me know if you still need a help.
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.webkit.URLUtil;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.VideoView;
public class VideoViewDemo extends Activity implements OnClickListener, OnTouchListener {
private static final String TAG = "VideoViewDemo";
private VideoView mVideoView;
private EditText mPath;
private ImageButton mPlay;
private ImageButton mPause;
private ImageButton mReset;
private ImageButton mStop;
private String current;
private int lengthOfAudio;
private SeekBar seekBar;
private final Handler handler = new Handler();
private final Runnable r = new Runnable() {#
Override
public void run() {
updateSeekProgress();
}
};
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mPath = (EditText) findViewById(R.id.path);
mPath.setText("http://daily3gp.com/vids/747.3gp");
mPlay = (ImageButton) findViewById(R.id.play);
mPause = (ImageButton) findViewById(R.id.pause);
mReset = (ImageButton) findViewById(R.id.reset);
mStop = (ImageButton) findViewById(R.id.stop);
seekBar = (SeekBar) findViewById(R.id.seekBar);
seekBar.setOnTouchListener(this);
mPlay.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
playVideo();
}
});
mPause.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.pause();
}
}
});
mReset.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.seekTo(0);
}
}
});
mStop.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
current = null;
mVideoView.stopPlayback();
}
}
});
runOnUiThread(new Runnable() {
public void run() {
playVideo();
updateSeekProgress();
}
});
}
public void onClick(View view) {
updateSeekProgress();
}
public boolean onTouch(View v, MotionEvent event) {
if (mVideoView.isPlaying()) {
SeekBar tmpSeekBar = (SeekBar) v;
mVideoView.seekTo((lengthOfAudio / 100) * tmpSeekBar.getProgress());
}
return false;
}
private void playVideo() {
try {
final String path = mPath.getText().toString();
Log.v(TAG, "path: " + path);
if (path == null || path.length() == 0) {
Toast.makeText(VideoViewDemo.this, "File URL/path is empty",
Toast.LENGTH_LONG).show();
} else {
// If the path has not changed, just start the media player
if (path.equals(current) && mVideoView != null) {
mVideoView.start();
//mVideoView.requestFocus();
return;
}
current = path;
mVideoView.setVideoPath(getDataSource(path));
lengthOfAudio = 180000;
mVideoView.start();
//mVideoView.requestFocus();
}
} catch (Exception e) {
Log.e(TAG, "error: " + e.getMessage(), e);
if (mVideoView != null) {
mVideoView.stopPlayback();
}
}
}
private void updateSeekProgress() {
if (mVideoView.isPlaying()) {
seekBar.setProgress((int)(((float) mVideoView.getCurrentPosition() / lengthOfAudio) * 100));
handler.postDelayed(r, 1000);
}
}
private String getDataSource(String path) throws IOException {
if (!URLUtil.isNetworkUrl(path)) {
return path;
} else {
URL url = new URL(path);
URLConnection cn = url.openConnection();
cn.connect();
InputStream stream = cn.getInputStream();
if (stream == null)
throw new RuntimeException("stream is null");
File temp = File.createTempFile("mediaplayertmp", "dat");
temp.deleteOnExit();
String tempPath = temp.getAbsolutePath();
FileOutputStream out = new FileOutputStream(temp);
byte buf[] = new byte[128];
do {
int numread = stream.read(buf);
if (numread <= 0)
break;
out.write(buf, 0, numread);
} while (true);
try {
stream.close();
} catch (IOException ex) {
Log.e(TAG, "error: " + ex.getMessage(), ex);
}
return tempPath;
}
}
}
I took a sample tutorial from http://davanum.wordpress.com/2009/12/04/android-%E2%80%93-videomusic-player-sample-take-2/
and further enhancing it to implement seek bar. I have added the code but seek bar doesn't move as the video progresses. Am I doing anything wrong? I am new to android programming.
I am trying to displaying a ProgressDialog before a video with the following code:
package com.Boodang;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.URLUtil;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.VideoView;
public class VideoLoading extends Activity implements Runnable {
private static final String TAG = "VideoViewDemo";
private VideoView mVideoView;
private EditText mPath;
private ImageView mPlay;
private ImageView mPause;
private ImageButton mReset;
private ImageView mStop;
private String current;
private ProgressDialog dialog;
private Context mContext;
String s=null;
#Override
public void onCreate(Bundle state){
super.onCreate(state);
dialog=ProgressDialog.show(this,"","Loding.PleaseWait",true);
Thread t=new Thread(this);
t.start();
Log.e("VideoPlay1 page","OnStart");
setContentView(R.layout.videoplay);
mPlay = (ImageView) findViewById(R.id.play);
mPause = (ImageView) findViewById(R.id.pause);
mStop = (ImageView) findViewById(R.id.stop);
mPlay.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
}
});
mPause.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.pause();
}
}
});
mStop.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
current = null;
mVideoView.stopPlayback();
}
}
});
runOnUiThread(new Runnable(){
public void run() {
dialog.dismiss();
}
});
}
public void run(){
Message msg;
handler.sendEmptyMessage(0);
}
Handler handler=new Handler(){
#Override
public void handleMessage(Message msg) {
try {
mVideoView = (VideoView) findViewById(R.id.video);
s=getIntent().getStringExtra("id");
final String path = s;
Log.v(TAG, "path: " + path);
if (path == null || path.length() == 0) {
Toast.makeText(VideoLoading.this, "File URL/path is empty",
Toast.LENGTH_LONG).show();
} else {
// If the path has not changed, just start the media player
if (path.equals(current) && mVideoView != null) {
mVideoView.start();
mVideoView.requestFocus();
return;
}
current = path;
mVideoView.setVideoPath(getDataSource(path));
mVideoView.start();
mVideoView.requestFocus();
}
} catch (Exception e) {
Log.e(TAG, "error123: " + e.getMessage(), e);
if (mVideoView != null) {
mVideoView.stopPlayback();
}
}
}
};
private String getDataSource(String path) throws IOException {
if (!URLUtil.isNetworkUrl(path)) {
return path;
} else {
URL url = new URL(path);
URLConnection cn = url.openConnection();
cn.connect();
InputStream stream = cn.getInputStream();
if (stream == null)
throw new RuntimeException("stream is null");
File temp = File.createTempFile("mediaplayertmp", "dat");
temp.deleteOnExit();
String tempPath = temp.getAbsolutePath();
FileOutputStream out = new FileOutputStream(temp);
byte buf[] = new byte[128];
do {
int numread = stream.read(buf);
if (numread <= 0)
break;
out.write(buf, 0, numread);
} while (true);
try {
stream.close();
} catch (IOException ex) {
Log.e(TAG, "error: " + ex.getMessage(), ex);
}
return tempPath;
}
}
}
but I do not get the progress bar here. all I get is the the video directly.
All help is appreciated.
Instead of dismissing the progress dialog in runOnUiThread, dismiss it in the onPreparedListener:
mVideoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer arg0) {
dialog.dismiss();
mVideoView.start();
}
});
runOnUiThread is called right away, therefore dimissing the dialog before it can be seen. This code will keep it going until the video is ready to start.
you should use an AsyncTask to do that. have a look on this page. it gives you detailed information on how to do what you want.
You'll have to move away from the synchronous MediaPlayer calls to do this. The MediaPlayer has series of player state change callbacks such as onPreparedListener which you'll need to make use of to determine when to display your dialog.
What you are experiencing is due to the fact that, although you are putting work to the uiThread, you are basically putting all your work to the uiThread.
The call:
mVideoView.setVideoPath(getDataSource(path));
is going on the uiThread (seemingly). You need to use the async listeners to avoid having to build those facilities for yourself.