Then I change the Project Structure to target sdk version Android 4.4 (KitKat), it can be improved, but not 100%.
I have no idea is it related to new Android Studio/SDK? or related to new Android version? or related to individual device(s)?
package com.blogspot.android_er.androidautoupdatelocation;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import java.text.DateFormat;
public class MainActivity extends AppCompatActivity
implements LocationListener, GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest;
TextView textAutoUpdateLocation;
static final int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textAutoUpdateLocation = (TextView) findViewById(R.id.autoupdatelocation);
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(5000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
@Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onLocationChanged(Location location) {
if (location != null) {
String strLocation =
DateFormat.getTimeInstance().format(location.getTime()) + "\n" +
"Latitude=" + location.getLatitude() + "\n" +
"Longitude=" + location.getLongitude();
textAutoUpdateLocation.setText(strLocation);
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onRequestPermissionsResult(
int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,
"permission was granted, :)",
Toast.LENGTH_LONG).show();
try{
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}catch(SecurityException e){
Toast.makeText(MainActivity.this,
"SecurityException:\n" + e.toString(),
Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(MainActivity.this,
"permission denied, ...:(",
Toast.LENGTH_LONG).show();
}
return;
}
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(MainActivity.this,
"onConnectionFailed: \n" + connectionResult.toString(),
Toast.LENGTH_LONG).show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidautoupdatelocation.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<TextView
android:id="@+id/autoupdatelocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:textStyle="italic|bold"/>
</LinearLayout>
Android Studio 2.0 on Ubuntu 16.04 |
package com.blogspot.android_er.androidgetlastlocation;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
public class MainActivity extends AppCompatActivity
implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
Button btnGetLastLocation;
TextView textLastLocation;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
static final int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGetLastLocation = (Button) findViewById(R.id.getlastlocation);
btnGetLastLocation.setOnClickListener(btnGetLastLocationOnClickListener);
textLastLocation = (TextView) findViewById(R.id.lastlocation);
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}
View.OnClickListener btnGetLastLocationOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mGoogleApiClient != null) {
if (mGoogleApiClient.isConnected()) {
getMyLocation();
} else {
Toast.makeText(MainActivity.this,
"!mGoogleApiClient.isConnected()", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(MainActivity.this,
"mGoogleApiClient == null", Toast.LENGTH_LONG).show();
}
}
};
/*
// Handle 'SecurityException' with try/catch
private void getMyLocation(){
try{
//code should explicitly check to see if permission is available
//(with 'checkPermission') or explicitly handle a potential 'SecurityException'
//
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
textLastLocation.setText(
String.valueOf(mLastLocation.getLatitude()) + "\n"
+ String.valueOf(mLastLocation.getLongitude()));
Toast.makeText(MainActivity.this,
String.valueOf(mLastLocation.getLatitude()) + "\n"
+ String.valueOf(mLastLocation.getLongitude()),
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,
"mLastLocation == null",
Toast.LENGTH_LONG).show();
}
} catch (SecurityException e){
Toast.makeText(MainActivity.this,
"SecurityException:\n" + e.toString(),
Toast.LENGTH_LONG).show();
}
}
*/
//------------------------------------------------------------------------------
//ref: Requesting Permissions at Run Time
//http://developer.android.com/training/permissions/requesting.html
//------------------------------------------------------------------------------
private void getMyLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
//------------------------------------------------------------------------------
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
return;
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
textLastLocation.setText(
String.valueOf(mLastLocation.getLatitude()) + "\n"
+ String.valueOf(mLastLocation.getLongitude()));
Toast.makeText(MainActivity.this,
String.valueOf(mLastLocation.getLatitude()) + "\n"
+ String.valueOf(mLastLocation.getLongitude()),
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,
"mLastLocation == null",
Toast.LENGTH_LONG).show();
}
}
@Override
public void onRequestPermissionsResult(
int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,
"permission was granted, :)",
Toast.LENGTH_LONG).show();
getMyLocation();
} else {
Toast.makeText(MainActivity.this,
"permission denied, ...:(",
Toast.LENGTH_LONG).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
@Override
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
@Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
getMyLocation();
}
@Override
public void onConnectionSuspended(int i) {
Toast.makeText(MainActivity.this,
"onConnectionSuspended: " + String.valueOf(i),
Toast.LENGTH_LONG).show();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(MainActivity.this,
"onConnectionFailed: \n" + connectionResult.toString(),
Toast.LENGTH_LONG).show();
}
}
package com.blogspot.android_er.androidgetlastlocation;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
public class MainActivity extends AppCompatActivity
implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
Button btnGetLastLocation;
TextView textLastLocation;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGetLastLocation = (Button) findViewById(R.id.getlastlocation);
btnGetLastLocation.setOnClickListener(btnGetLastLocationOnClickListener);
textLastLocation = (TextView) findViewById(R.id.lastlocation);
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}
View.OnClickListener btnGetLastLocationOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mGoogleApiClient != null){
if(mGoogleApiClient.isConnected()){
getMyLocation();
}else{
Toast.makeText(MainActivity.this,
"!mGoogleApiClient.isConnected()", Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(MainActivity.this,
"mGoogleApiClient == null", Toast.LENGTH_LONG).show();
}
}
};
private void getMyLocation(){
try{
/* code should explicitly check to see if permission is available
(with 'checkPermission') or explicitly handle a potential 'SecurityException'
*/
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
textLastLocation.setText(
String.valueOf(mLastLocation.getLatitude()) + "\n"
+ String.valueOf(mLastLocation.getLongitude()));
Toast.makeText(MainActivity.this,
String.valueOf(mLastLocation.getLatitude()) + "\n"
+ String.valueOf(mLastLocation.getLongitude()),
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,
"mLastLocation == null",
Toast.LENGTH_LONG).show();
}
} catch (SecurityException e){
Toast.makeText(MainActivity.this,
"SecurityException:\n" + e.toString(),
Toast.LENGTH_LONG).show();
}
}
@Override
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
@Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
getMyLocation();
}
@Override
public void onConnectionSuspended(int i) {
Toast.makeText(MainActivity.this,
"onConnectionSuspended: " + String.valueOf(i),
Toast.LENGTH_LONG).show();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(MainActivity.this,
"onConnectionFailed: \n" + connectionResult.toString(),
Toast.LENGTH_LONG).show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidgetlastlocation.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<Button
android:id="@+id/getlastlocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get my last location"/>
<TextView
android:id="@+id/lastlocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
package com.blogspot.android_er.androidmediaplayer;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.MediaController;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity
implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener,
MediaController.MediaPlayerControl {
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private MediaPlayer mediaPlayer;
private MediaController mediaController;
private Handler handler = new Handler();
String videoSource =
"https://sites.google.com/site/androidexample9/download/RunningClock.mp4";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(mediaController != null){
mediaController.show();
}
return false;
}
});
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Toast.makeText(MainActivity.this,
"surfaceCreated()", Toast.LENGTH_LONG).show();
mediaPlayer = new MediaPlayer();
mediaPlayer.setDisplay(surfaceHolder);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnPreparedListener(this);
try {
mediaPlayer.setDataSource(videoSource);
mediaPlayer.prepare();
mediaController = new MediaController(this);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this,
"something wrong!\n" + e.toString(),
Toast.LENGTH_LONG).show();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder,
int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
Toast.makeText(MainActivity.this,
"onPrepared()", Toast.LENGTH_LONG).show();
mediaController.setMediaPlayer(this);
mediaController.setAnchorView(surfaceView);
handler.post(new Runnable() {
public void run() {
mediaController.setEnabled(true);
mediaController.show();
}
});
}
@Override
public void start() {
mediaPlayer.start();
}
@Override
public void pause() {
mediaPlayer.pause();
}
@Override
public int getDuration() {
return mediaPlayer.getDuration();
}
@Override
public int getCurrentPosition() {
return mediaPlayer.getCurrentPosition();
}
@Override
public void seekTo(int pos) {
mediaPlayer.seekTo(pos);
}
@Override
public boolean isPlaying() {
return mediaPlayer.isPlaying();
}
@Override
public int getBufferPercentage() {
return 0;
}
@Override
public boolean canPause() {
return true;
}
@Override
public boolean canSeekBackward() {
return true;
}
@Override
public boolean canSeekForward() {
return true;
}
@Override
public int getAudioSessionId() {
return mediaPlayer.getAudioSessionId();
}
}
package com.blogspot.android_er.androidmediaplayer;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity
implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener {
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private MediaPlayer mediaPlayer;
String videoSource =
"https://sites.google.com/site/androidexample9/download/RunningClock.mp4";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Toast.makeText(MainActivity.this,
"surfaceCreated()", Toast.LENGTH_LONG).show();
mediaPlayer = new MediaPlayer();
mediaPlayer.setDisplay(surfaceHolder);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnPreparedListener(this);
try {
mediaPlayer.setDataSource(videoSource);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this,
"something wrong!\n" + e.toString(),
Toast.LENGTH_LONG).show();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder,
int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
Toast.makeText(MainActivity.this,
"onPrepared()", Toast.LENGTH_LONG).show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidmediaplayer.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
package com.blogspot.android_er.androidvideoview;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.VideoView;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
Button btnCapture;
Spinner spOption;
VideoView myVideoView;
String videoSource =
"https://sites.google.com/site/androidexample9/download/RunningClock.mp4";
Uri uriVideoSource;
MediaController myMediaController;
MediaMetadataRetriever myMediaMetadataRetriever;
String[] stringOpts = {
"none",
"OPTION_CLOSEST",
"OPTION_CLOSEST_SYNC",
"OPTION_NEXT_SYNC",
"OPTION_PREVIOUS_SYNC"};
int[] valOptions ={
0, //will not be used
MediaMetadataRetriever.OPTION_CLOSEST,
MediaMetadataRetriever.OPTION_CLOSEST_SYNC,
MediaMetadataRetriever.OPTION_NEXT_SYNC,
MediaMetadataRetriever.OPTION_PREVIOUS_SYNC};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myVideoView = (VideoView)findViewById(R.id.vview);
prepareVideo();
spOption = (Spinner)findViewById(R.id.option);
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, stringOpts);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spOption.setAdapter(adapter);
btnCapture = (Button)findViewById(R.id.capture);
btnCapture.setOnClickListener(btnCaptureOnClickListener);
}
View.OnClickListener btnCaptureOnClickListener = new View.OnClickListener(){
@Override
public void onClick(View v) {
int currentPosition = myVideoView.getCurrentPosition(); //in millisecond
Toast.makeText(MainActivity.this,
"Current Position: " + currentPosition + " (ms)",
Toast.LENGTH_LONG).show();
Bitmap bmFrame;
int pos = currentPosition * 1000; //unit in microsecond
int opt = spOption.getSelectedItemPosition();
if(opt == 0){
bmFrame = myMediaMetadataRetriever
.getFrameAtTime(pos);
}else{
bmFrame = myMediaMetadataRetriever
.getFrameAtTime(pos,
valOptions[opt]);
}
if(bmFrame == null){
Toast.makeText(MainActivity.this,
"bmFrame == null!",
Toast.LENGTH_LONG).show();
}else {
AlertDialog.Builder myCaptureDialog =
new AlertDialog.Builder(MainActivity.this);
ImageView capturedImageView = new ImageView(MainActivity.this);
capturedImageView.setImageBitmap(bmFrame);
LinearLayout.LayoutParams capturedImageViewLayoutParams =
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
capturedImageView.setLayoutParams(capturedImageViewLayoutParams);
myCaptureDialog.setView(capturedImageView);
myCaptureDialog.show();
}
}
};
private void prepareVideo(){
myMediaMetadataRetriever = new MediaMetadataRetriever();
myMediaMetadataRetriever.setDataSource(
videoSource, new HashMap<String, String>());
myMediaController = new MediaController(MainActivity.this);
myVideoView.setMediaController(myMediaController);
Toast.makeText(MainActivity.this, videoSource, Toast.LENGTH_LONG).show();
uriVideoSource = Uri.parse(videoSource);
myVideoView.setVideoURI(uriVideoSource);
myVideoView.setOnCompletionListener(myVideoViewCompletionListener);
myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);
myVideoView.setOnErrorListener(myVideoViewErrorListener);
myVideoView.requestFocus();
myVideoView.start();
}
MediaPlayer.OnCompletionListener myVideoViewCompletionListener =
new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
Toast.makeText(MainActivity.this, "End of Video",
Toast.LENGTH_LONG).show();
}
};
MediaPlayer.OnPreparedListener MyVideoViewPreparedListener =
new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
long duration = myVideoView.getDuration(); //in millisecond
Toast.makeText(MainActivity.this,
"Duration: " + duration + " (ms)",
Toast.LENGTH_LONG).show();
}
};
MediaPlayer.OnErrorListener myVideoViewErrorListener =
new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
String errWhat = "";
switch (what){
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
errWhat = "MEDIA_ERROR_UNKNOWN";
break;
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
errWhat = "MEDIA_ERROR_SERVER_DIED";
break;
default: errWhat = "unknown what";
}
String errExtra = "";
switch (extra){
case MediaPlayer.MEDIA_ERROR_IO:
errExtra = "MEDIA_ERROR_IO";
break;
case MediaPlayer.MEDIA_ERROR_MALFORMED:
errExtra = "MEDIA_ERROR_MALFORMED";
break;
case MediaPlayer.MEDIA_ERROR_UNSUPPORTED:
errExtra = "MEDIA_ERROR_UNSUPPORTED";
break;
case MediaPlayer.MEDIA_ERROR_TIMED_OUT:
errExtra = "MEDIA_ERROR_TIMED_OUT";
break;
default:
errExtra = "...others";
}
Toast.makeText(MainActivity.this,
"Error!!!\n" +
"what: " + errWhat + "\n" +
"extra: " + errExtra,
Toast.LENGTH_LONG).show();
return true;
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidvideoview.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/capture"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Capture"/>
<Spinner
android:id="@+id/option"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</LinearLayout>
<VideoView
android:id="@+id/vview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
package com.blogspot.android_er.androidvideoview;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
VideoView myVideoView;
String videoSource =
"https://sites.google.com/site/androidexample9/download/RunningClock.mp4";
Uri uriVideoSource;
MediaController myMediaController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myVideoView = (VideoView)findViewById(R.id.vview);
prepareVideo();
}
private void prepareVideo(){
myMediaController = new MediaController(MainActivity.this);
myVideoView.setMediaController(myMediaController);
Toast.makeText(MainActivity.this, videoSource, Toast.LENGTH_LONG).show();
uriVideoSource = Uri.parse(videoSource);
myVideoView.setVideoURI(uriVideoSource);
myVideoView.setOnCompletionListener(myVideoViewCompletionListener);
myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);
myVideoView.setOnErrorListener(myVideoViewErrorListener);
myVideoView.requestFocus();
myVideoView.start();
}
MediaPlayer.OnCompletionListener myVideoViewCompletionListener =
new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
Toast.makeText(MainActivity.this, "End of Video",
Toast.LENGTH_LONG).show();
}
};
MediaPlayer.OnPreparedListener MyVideoViewPreparedListener =
new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
long duration = myVideoView.getDuration(); //in millisecond
Toast.makeText(MainActivity.this,
"Duration: " + duration + " (ms)",
Toast.LENGTH_LONG).show();
}
};
MediaPlayer.OnErrorListener myVideoViewErrorListener =
new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
String errWhat = "";
switch (what){
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
errWhat = "MEDIA_ERROR_UNKNOWN";
break;
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
errWhat = "MEDIA_ERROR_SERVER_DIED";
break;
default: errWhat = "unknown what";
}
String errExtra = "";
switch (extra){
case MediaPlayer.MEDIA_ERROR_IO:
errExtra = "MEDIA_ERROR_IO";
break;
case MediaPlayer.MEDIA_ERROR_MALFORMED:
errExtra = "MEDIA_ERROR_MALFORMED";
break;
case MediaPlayer.MEDIA_ERROR_UNSUPPORTED:
errExtra = "MEDIA_ERROR_UNSUPPORTED";
break;
case MediaPlayer.MEDIA_ERROR_TIMED_OUT:
errExtra = "MEDIA_ERROR_TIMED_OUT";
break;
default:
errExtra = "...others";
}
Toast.makeText(MainActivity.this,
"Error!!!\n" +
"what: " + errWhat + "\n" +
"extra: " + errExtra,
Toast.LENGTH_LONG).show();
return true;
}
};
}
package com.blogspot.android_er.androidvideoview;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
VideoView myVideoView;
String videoSource =
"https://sites.google.com/site/androidexample9/download/RunningClock.mp4";
Uri uriVideoSource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myVideoView = (VideoView)findViewById(R.id.vview);
prepareVideo();
}
private void prepareVideo(){
Toast.makeText(MainActivity.this, videoSource, Toast.LENGTH_LONG).show();
uriVideoSource = Uri.parse(videoSource);
myVideoView.setVideoURI(uriVideoSource);
myVideoView.setOnCompletionListener(myVideoViewCompletionListener);
myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);
myVideoView.setOnErrorListener(myVideoViewErrorListener);
myVideoView.requestFocus();
myVideoView.start();
}
MediaPlayer.OnCompletionListener myVideoViewCompletionListener =
new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
Toast.makeText(MainActivity.this, "End of Video",
Toast.LENGTH_LONG).show();
}
};
MediaPlayer.OnPreparedListener MyVideoViewPreparedListener =
new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
long duration = myVideoView.getDuration(); //in millisecond
Toast.makeText(MainActivity.this,
"Duration: " + duration + " (ms)",
Toast.LENGTH_LONG).show();
}
};
MediaPlayer.OnErrorListener myVideoViewErrorListener =
new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
String errWhat = "";
switch (what){
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
errWhat = "MEDIA_ERROR_UNKNOWN";
break;
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
errWhat = "MEDIA_ERROR_SERVER_DIED";
break;
default: errWhat = "unknown what";
}
String errExtra = "";
switch (extra){
case MediaPlayer.MEDIA_ERROR_IO:
errExtra = "MEDIA_ERROR_IO";
break;
case MediaPlayer.MEDIA_ERROR_MALFORMED:
errExtra = "MEDIA_ERROR_MALFORMED";
break;
case MediaPlayer.MEDIA_ERROR_UNSUPPORTED:
errExtra = "MEDIA_ERROR_UNSUPPORTED";
break;
case MediaPlayer.MEDIA_ERROR_TIMED_OUT:
errExtra = "MEDIA_ERROR_TIMED_OUT";
break;
default:
errExtra = "...others";
}
Toast.makeText(MainActivity.this,
"Error!!!\n" +
"what: " + errWhat + "\n" +
"extra: " + errExtra,
Toast.LENGTH_LONG).show();
return true;
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidvideoview.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<VideoView
android:id="@+id/vview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>