I am creating a qr code scanning app, which continuously scan the qr code and show the result on the same screen without closing the camera.
I am using ZXing library but when I trigger the continuous scanning activity it fails to open the camera. I can't figure out the issue. Please check, I already added camera permission in my manifest.
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="#+id/barcode_scanner"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_above="#+id/buttonsLayout"
android:layout_alignParentTop="true">
</com.journeyapps.barcodescanner.DecoratedBarcodeView>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/buttonsLayout"
android:layout_toLeftOf="#+id/centerHorizont">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pause"
android:onClick="pause" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Resume"
android:onClick="resume" />
</LinearLayout>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:id="#+id/centerHorizont" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/centerHorizont"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#id/buttonsLayout"
android:id="#+id/barcodePreview" />
</RelativeLayout>
Java Code:
public class ContinuousCaptureActivity extends Activity {
private static final String TAG = ContinuousCaptureActivity.class.getSimpleName();
private DecoratedBarcodeView barcodeView;
private BeepManager beepManager;
private String lastText;
private BarcodeCallback callback = new BarcodeCallback() {
#Override
public void barcodeResult(BarcodeResult result) {
if(result.getText() == null || result.getText().equals(lastText)) {
// Prevent duplicate scans
return;
}
lastText = result.getText();
barcodeView.setStatusText(result.getText());
beepManager.playBeepSoundAndVibrate();
//Added preview of scanned barcode
ImageView imageView = (ImageView) findViewById(R.id.barcodePreview);
imageView.setImageBitmap(result.getBitmapWithResultPoints(Color.YELLOW));
}
#Override
public void possibleResultPoints(List<ResultPoint> resultPoints) {
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.continuous_scan);
barcodeView = (DecoratedBarcodeView) findViewById(R.id.barcode_scanner);
Collection<BarcodeFormat> formats = Arrays.asList(BarcodeFormat.QR_CODE, BarcodeFormat.CODE_39);
barcodeView.getBarcodeView().setDecoderFactory(new DefaultDecoderFactory(formats));
barcodeView.decodeContinuous(callback);
beepManager = new BeepManager(this);
}
#Override
protected void onResume() {
super.onResume();
barcodeView.resume();
}
#Override
protected void onPause() {
super.onPause();
barcodeView.pause();
}
public void pause(View view) {
barcodeView.pause();
}
public void resume(View view) {
barcodeView.resume();
}
public void triggerScan(View view) {
barcodeView.decodeSingle(callback);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return barcodeView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}
Here is the error message, I am getting!
The problem occur because of the permission. I have added code to request permission manually, Now problem is solved.
Thanks to #Roberto Manfreda
Related
I am building a simple video player Ui and I have a : seekbar and two buttons to controle the video view, when I click on any point of seekbar every thing is ok and video view progress will be the same with seekbar and the buttons is working too ! but,when I handle the seekbar thumb and move it ,then leave it, every thing messing up and the seekbar and the buttons will never work again, so I am not getting an Exception error it's just will not work again. why that happening?
Activity
public class Player extends AppCompatActivity {
View controller;
Handler handler;
SeekBar seekBar;
VideoView videoView;
ImageButton play_btn, pause_btn;
ImageButton forward_btn, replay_btn;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
handler = new Handler();
videoView = (VideoView) findViewById(R.id.videoView);
controller = findViewById(R.id.player_controller); // the controller View
hideController(controller);
forward_btn = (ImageButton) findViewById(R.id.playerController_forward_10_Button);
replay_btn = (ImageButton) findViewById(R.id.playerController_replay_10_Button);
forward_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
forward_10_sec();
}
});
replay_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
reply_10_sec();
}
});
play_btn = (ImageButton) findViewById(R.id.playerController_playButton);
pause_btn = (ImageButton) findViewById(R.id.playerController_pauseButton);
play_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
play();
}
});
pause_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pause();
}
});
seekBar = findViewById(R.id.playerController_seekBar); // video seekBar
seekBar.setOnSeekBarChangeListener(onSeekBarChangeListener);
videoView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (controller.getVisibility() == View.INVISIBLE)
showController(controller);
else
hideController(controller);
}
});
Intent intent = getIntent();
if (intent != null)
setVideoSource(intent);
}
private void play() {
if(!videoView.isPlaying()) {
videoView.start();
play_btn.setVisibility(View.INVISIBLE);
pause_btn.setVisibility(View.VISIBLE);
}
}
private void pause() {
if(videoView.isPlaying()) {
videoView.pause();
pause_btn.setVisibility(View.INVISIBLE);
play_btn.setVisibility(View.VISIBLE);
}
}
private void forward_10_sec() {
if (videoView.canSeekForward())
videoView.seekTo(videoView.getCurrentPosition() + 1000);
}
private void reply_10_sec() {
if (videoView.canSeekBackward())
videoView.seekTo(videoView.getCurrentPosition() - 1000);
}
// to show the controllers
private void showController(View controller) {
controller.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
hideController(controller);
}
}, 6000);
}
// to hide the controllers
private void hideController(View controller) {
controller.setVisibility(View.INVISIBLE);
}
// to set video source from the main app or file manager
private void setVideoSource(Intent intent) {
... some codes to set path ...
play();
showController(controller);
updateSeekBarProgress();
}
// to move seekBar to real video progress
private void updateSeekBarProgress() {
Player.this.runOnUiThread(updateSeekBar);
}
private Runnable updateSeekBar = new Runnable() {
#Override
public void run() {
seekBar.setMax(videoView.getDuration() / 1000);
seekBar.setProgress(videoView.getCurrentPosition() / 1000);
seekBar.postDelayed(this,500);
}
};
SeekBar.OnSeekBarChangeListener onSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (b) {
videoView.seekTo(i*1000);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
}
activity_player
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
tools:context=".Player">
<VideoView
android:id="#+id/videoView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include
android:id="#+id/player_controller"
layout="#layout/player_controller" />
</androidx.constraintlayout.widget.ConstraintLayout>
player_controller
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/transparent_black">
<ImageButton
android:id="#+id/playerController_playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/play_icon_48"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/playerController_pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/pause_icon_48"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/playerController_replay_10_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/replay_10_icon_48"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/playerController_playButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/playerController_forward_10_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/forward_10_icon_48"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/playerController_playButton"
app:layout_constraintTop_toTopOf="parent" />
<SeekBar
android:id="#+id/playerController_seekBar"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:progressBackgroundTint="#color/white"
android:progressTint="#color/white"
android:secondaryProgressTint="#color/light_purple"
android:thumbTint="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/playerController_playButton" />
</androidx.constraintlayout.widget.ConstraintLayout>
so, in a simple way : I want to set video view progress like seekbar progress with any way that I move the seek bar with.
(now it's just works when click on it and when handle and move it , nothing works again).
thanks...
It's finally has been done with that :
SeekBar.OnSeekBarChangeListener onSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (b) {
videoView.seekTo(i * 1000);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
handler.removeCallbacks(updateSeekBar);
pause();
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
videoView.seekTo(seekBar.getProgress()*1000);
play();
updateSeekBarProgress();
}
};
The RecyclerView is showing all data, but the item click is not working. Here I am attaching what I have done so far. For better understanding I am removing all the unnecessary code.
This is my recyclerview item xml.
<data>
<variable
name="model"
type="com.xyz.abc.pojo.EmployeeListWithDesignationSetGet" />
<variable
name="viewModel"
type="com.xyz.abc.viewmodels.EmpListWithDesigViewModel" />
</data>
<LinearLayout
android:id="#+id/ll_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:clickable="true"
android:focusable="true"
android:onClick="#{() -> viewModel.itemClick(model)}">
<TextView
android:id="#+id/tv_show_details"
android:layout_width="70dp"
android:layout_height="30dp"
android:text="Show"
android:textColor="#FFFFFF" />
</LinearLayout>
The ViewModel class where I have written the click method.
public class EmpListWithDesigViewModel extends ViewModel {
private MutableLiveData<List<EmployeeListWithDesignationSetGet>> mutableLiveData;
private EmpListWithDesigClickListener listener;
private EmpListWithDesigRepository empListWithDesigRepository;
public void setListener(EmpListWithDesigClickListener listener) {
this.listener = listener;
}
public void init() {
if (mutableLiveData != null) {
return;
}
empListWithDesigRepository = EmpListWithDesigRepository.getInstance();
mutableLiveData = empListWithDesigRepository.getEmpList();
}
public MutableLiveData<List<EmployeeListWithDesignationSetGet>> getEmpList() {
return mutableLiveData;
}
public void itemClick(EmployeeListWithDesignationSetGet employeeListWithDesignationSetGet) {
listener.onItemClick(employeeListWithDesignationSetGet);
}
}
Now in activity I am implementing the click interface.
public class EmployeeDesignationActivity extends AppCompatActivity implements EmpListWithDesigClickListener {
private RecyclerView mRv_recyclerView;
private List<EmployeeListWithDesignationSetGet> arrayList;
private EmployeeListWithDesigAdapter employeeListWithDesigAdapter;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employee_designation);
setViewReferences();
arrayList = new ArrayList<>();
employeeListWithDesigAdapter = new EmployeeListWithDesigAdapter(this,arrayList);
mRv_recyclerView.setAdapter(employeeListWithDesigAdapter);
EmpListWithDesigViewModel empListWithDesigViewModel = new ViewModelProvider(this,new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(EmpListWithDesigViewModel.class);
empListWithDesigViewModel.setListener(this);
empListWithDesigViewModel.init();
empListWithDesigViewModel.getEmpList().observe(this, new Observer<List<EmployeeListWithDesignationSetGet>>() {
#Override
public void onChanged(List<EmployeeListWithDesignationSetGet> employeeListWithDesignationSetGets) {
arrayList.addAll(employeeListWithDesignationSetGets);
employeeListWithDesigAdapter.notifyDataSetChanged();
}
});
}
private void setViewReferences(){
mRv_recyclerView = findViewById(R.id.rv_activity_employee_designation);
}
#Override
public void onItemClick(EmployeeListWithDesignationSetGet employeeListWithDesignationSetGet) {
String phone = employeeListWithDesignationSetGet.getEmpPhone();
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
startActivity(intent);
}
}
Pardon me if I have not provided enough info, this is my first SO post. Thanks
You should remove the android:onClick="#{() -viewModel.itemClick(model)}" from Linearlayout. Also add the below properties.
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
Then your item layout will be as below:
<LinearLayout
android:id="#+id/ll_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
>
<TextView
android:id="#+id/tv_show_details"
android:layout_width="70dp"
android:layout_height="30dp"
android:text="Show"
android:textColor="#FFFFFF" />
</LinearLayout>
Problem fixed. I forgot to bind the viewmodel in recyclerview adapter.
I have used exoPlayer Library what i am trying to do is i pass data from recyclerview to next activity that works fine video is been played and title as well as desc is been fetched but when i rotate the phone i only want simpleexovideoview to displayed and video is playing but the activity name is still there.
I have used < android:configChanges="orientation|screenSize" > that handles the orientation change following is snapshot of activity
Portrait view
Landscape view
and code is as follows
videoplayer.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.kaushal.myapplication.MainActivity"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="81dp">
<android.support.constraint.Guideline
android:id="#+id/horizontalHalf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="256dp" />
<TextView
android:id="#+id/VideoTitle"
android:textSize="22sp"
android:text="video title"
android:textStyle="bold"
android:layout_margin="12dp"
android:textColor="#016699"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="#+id/horizontalHalf" />
<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="#+id/videoplayer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
app:layout_constraintBottom_toTopOf="#+id/horizontalHalf"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="#+id/VideoDesc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:text="Video Desc"
app:layout_constraintLeft_toLeftOf="parent"
android:textSize="18sp"
android:layout_margin="12dp"
app:layout_constraintTop_toBottomOf="#+id/VideoTitle"
tools:layout_editor_absoluteY="477dp"
android:layout_marginLeft="12dp" />
</android.support.constraint.ConstraintLayout>
videoActivity
package com.example.kaushal.myapplication;
/**
* Created by kaushal on 06-09-2017.
*/
public class videoplay extends AppCompatActivity implements
ExoPlayer.EventListener {
TextView vidtitle, videodesc;
String videpath;
SimpleExoPlayer exoplayer;
SimpleExoPlayerView exoPlayerView;
PlaybackStateCompat.Builder videosessionBuilder;
final static String TAG = videoplay.class.getName();
private RelativeLayout.LayoutParams paramsNotFullscreen;
RelativeLayout rl;
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoplayer);
vidtitle = (TextView) findViewById(R.id.VideoTitle);
videodesc = (TextView) findViewById(R.id.VideoDesc);
exoPlayerView = (SimpleExoPlayerView)
findViewById(R.id.videoplayer);
vidtitle.setText(getIntent().getStringExtra("videotitle"));
videodesc.setText(getIntent().getStringExtra("videodesc"));
videpath = getIntent().getStringExtra("videourl");
mediaSession();
Uri uri = Uri.parse(videpath);
intializePlayer(uri);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
paramsnotfullscreen= (RelativeLayout.LayoutParams)exoPlayerView.getLayoutParams();
RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(paramsnotfullscreen);
params.setMargins(0, 0, 0, 0);
params.height= ViewGroup.LayoutParams.MATCH_PARENT;
params.width=ViewGroup.LayoutParams.MATCH_PARENT;
params.addRule(RelativeLayout.CENTER_IN_PARENT);
exoPlayerView.setLayoutParams(params);
}else if (newConfig.orientation==Configuration.ORIENTATION_PORTRAIT){
exoPlayerView.setLayoutParams(paramsnotfullscreen);
}
} //refrence = https://stackoverflow.com/questions/13011891/make-a-fullscreen-in-only-layout-land-android-when-play-videoview
public void intializePlayer(Uri uri) {
DefaultTrackSelector dfs = new DefaultTrackSelector();
DefaultLoadControl dfc = new DefaultLoadControl();
exoplayer = ExoPlayerFactory.newSimpleInstance(this, dfs, dfc);
exoPlayerView.setPlayer(exoplayer);
//Prepare Media source
String useragent = Util.getUserAgent(this, "MyApplication");
MediaSource mediaSource = new ExtractorMediaSource(uri, new DefaultDataSourceFactory(this, useragent),
new DefaultExtractorsFactory(), null, null);
exoplayer.prepare(mediaSource);
exoplayer.setPlayWhenReady(true);
}
public void releasePlayer() {
exoplayer.stop();
exoplayer.release();
exoplayer = null;
}
public void mediaSession() {
MediaSessionCompat mediaSessionCompat = new MediaSessionCompat(this, TAG);
mediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSessionCompat.setMediaButtonReceiver(null);
videosessionBuilder = new PlaybackStateCompat.Builder().setActions(PlaybackStateCompat.ACTION_PLAY |
PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE);
mediaSessionCompat.setPlaybackState(videosessionBuilder.build());
mediaSessionCompat.setCallback(new mediaSessionCallback());
mediaSessionCompat.setActive(true);
}
public class mediaSessionCallback extends MediaSessionCompat.Callback {
#Override
public void onPlay() {
exoplayer.setPlayWhenReady(true);
}
#Override
public void onPause() {
exoplayer.setPlayWhenReady(false);
}
#Override
public void onSkipToPrevious() {
exoplayer.seekTo(0);
}
}
//Exo player methods
#Override
public void onTimelineChanged(Timeline timeline, Object manifest) {
}
#Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
}
#Override
public void onLoadingChanged(boolean isLoading) {
}
#Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if ((playbackState == exoplayer.STATE_READY) && playWhenReady) {
Log.d(TAG, "Player running");
} else if (playbackState == exoplayer.STATE_READY) {
Log.d(TAG, "paused");
}
}
#Override
public void onPlayerError(ExoPlaybackException error) {
}
#Override
public void onPositionDiscontinuity() {
}
//When Activity is been destroyed
#Override
protected void onDestroy() {
super.onDestroy();
releasePlayer();
}
}
Here is an option called same-name-layout-land.xml layout that you can handle your landscape situation and Android will take it and inflate automatically when your device rotated, with this you can manage how your Activity should be shown to the user, as long as this cool option exist, you just have to put your VideoPlayer xml tag with "match_parent" for height and width in landscape version of your xml layout.
UPDATE:
Of course, if you want to your video player to take whole of screen, you have to delete the default margin of it, and for making toolbar disappear you have to create two different styles.xml. Put one into res/values-port and the other into res/values-land, and in the landscape version you have to choose a *.NoActionBar version of your themes for it.
VideoView in RelativeLayout and the code to open and play video in video view is below. by changing the backgroud color of videoview i can check the visibility of videoview but video is not being played one more thing i can hear the sound of the video but the content in the video is not shown.
Thank you.
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#9057ac"
android:visibility="gone"
android:id="#+id/videoContainer">
<Button
android:id="#+id/CancelRecording"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginRight="10dp" />
<VideoView
android:id="#+id/VideoPlayer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e3e3e3"/>
<Button
android:id="#+id/UploadButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginRight="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
public class CameraActivity extends Activity{
super.onCreate(savedInstanceState);
setContentView(R.layout.third_activity_viewpager);
private RelativeLayout videoContainer;
videoContainer = (RelativeLayout) findViewById(R.id.videoContainer);
videoContainer.setVisibility(View.VISIBLE);
String video = file.toString();
videoView.setVideoURI(Uri.parse(video));
videoView.start();
//i checked the *file* path is correct no need to worry about Path.
}
Change video video view to textureview and use it
In xml:
<RelativeLayout
android:id="#+id/preview_video_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="62dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<TextureView
android:id="#+id/preview_video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5" />
<TextureView
android:id="#+id/preview_video_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5" />
</LinearLayout>
<ImageView
android:id="#+id/previre_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="center"
android:src="#drawable/icn_play_big" />
</RelativeLayout>
In your actvity:
implements TextureView.SurfaceTextureListener , OnClickListener, OnCompletionListener
private TextureView surfaceView;
private ImageView imagePlay;
surfaceView = (TextureView) findViewById(R.id.preview_video_2);
surfaceView.setSurfaceTextureListener(this);
surfaceView.setOnClickListener(this);
path = getIntent().getStringExtra("your path");
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(this);
}
#Override
protected void onStop() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
imagePlay.setVisibility(View.GONE);
}
super.onStop();
}
private void prepare(Surface surface) {
try {
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(path);
mediaPlayer.setSurface(surface);
mediaPlayer.setLooping(true);
mediaPlayer.prepare();
mediaPlayer.seekTo(0);
} catch (Exception e) {
}
}
#Override
public void onSurfaceTextureAvailable(SurfaceTexture arg0, int arg1,
int arg2) {
prepare(new Surface(arg0));
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0) {
return false;
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1,
int arg2) {
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture arg0) {
}
I have a PreferenceFragment containing some TextEditPreferences etc. and then I have this custom Preference that uses Volley NetworkImageView instead of the default ImageView and has a "Go to" icon at the end. Layout-wise otherwise it's copy-pasted from compat-v14 preference.xml file.
The problem is, I can't get any focus, selection or click events to fire when touching the Custom Preference item. I've tries everything I've come up: onClick(), onPreferenceTreeClick(), setOnPreferenceClickListener, setting layout xml android:selectable="true", android:enabled="true", android:descendantFocusability="blocksDescendants"...
xml/preferences.xml
<PreferenceCategory android:title="Icon">
<dev.niko.project.views.GravatarIconPreference android:key="auth.user.avatar"
android:title="Gravatar" android:summary="This has no focus animation, no click events are fired and certainly no Intent"
android:selectable="true" android:enabled="true"
android:layout="#layout/preference_gravatar"
android:defaultValue="">
<intent android:action="android.intent.action.VIEW" android:data="https://en.gravatar.com/connect/" />
</dev.niko.project.views.GravatarIconPreference>
<Preference
android:title="Gravatar" android:summary="This works fine">
<intent android:action="android.intent.action.VIEW" android:data="https://en.gravatar.com/connect/" />
</Preference>
</PreferenceCategory>
MyPreferencesFragment.java
public class AccountSettingsFragment extends PreferenceFragment
implements SharedPreferences.OnSharedPreferenceChangeListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
preferences = context.getSharedPreferences(PREF_NAME_AUTH, Context.MODE_PRIVATE);
GravatarIconPreference gravatarIconPref = ((GravatarIconPreference)findPreference(AVATAR));
gravatarIconPref.setImageUrl(API.gravatar(preferences.getString(EMAIL, null)));
gravatarIconPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
Log.wtf(TAG, "gravatarIconPref.onPreferenceClick()");
return false;
}
});
for (String key : preferences.getAll().keySet()) {
onSharedPreferenceChanged(preferences, key);
}
}
#Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Log.wtf(TAG, "onPreferenceTreeClick(..., "+preference.getKey()+")");
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, final String key) {
final Preference preference = findPreference(key);
if (preference != null) {
Log.d(TAG, "onSharedPreferenceChanged. key="+key+" preference.getKey="+preference.getKey());
}
}
}
GravatarIconPreference.java
public class GravatarIconPreference extends Preference {
private static final String TAG = GravatarIconPreference.class.getSimpleName();
private Context context;
private View view;
private Drawable icon;
private String url;
public GravatarIconPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setSelectable(true);
setEnabled(true);
this.context = context;
}
// also tried overriding onCreateView, no change
#Override
protected void onBindView(View view) {
super.onBindView(view);
view.setClickable(true);
if (view != null && !TextUtils.isEmpty(url)) {
NetworkImageView gravatar = ((NetworkImageView) view.findViewById(R.id.gravatar));
if (gravatar != null) {
gravatar.setImageUrl(url, App.getInstance().getImageLoader());
}
}
}
#Override
protected void onClick() {
Log.wtf(TAG, "onClick()!!!");
super.onClick();
}
#Override
public Drawable getIcon() {
// TODO: gravatar-networkimageview
return super.getIcon();
}
public void setImageUrl(#Nullable String url) { this.url = url; }
}
preference_gravatar.xml source
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:background="?android:attr/activatedBackgroundIndicator"
android:clipToPadding="false"
android:focusable="true" android:descendantFocusability="blocksDescendants"
android:baselineAligned="false">
<LinearLayout android:id="#+id/icon_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="-4dp"
android:minWidth="60dp"
android:gravity="start|center_vertical"
android:orientation="horizontal"
android:paddingEnd="12dp"
android:paddingTop="4dp"
android:paddingBottom="4dp" android:focusable="false">
<com.android.volley.toolbox.NetworkImageView android:id="#+id/gravatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:maxWidth="48dp"
app:maxHeight="48dp"
android:layout_marginStart="#dimen/list_icon_margin"
android:layout_gravity="center" />
</LinearLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingTop="16dp"
android:paddingBottom="16dp">
<TextView android:id="#android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItem"
android:ellipsize="marquee"
android:layout_alignParentStart="true"
android:layout_toStartOf="#+id/chevron"/>
<TextView android:id="#android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary"
android:maxLines="10"
android:layout_alignParentStart="true"
android:layout_toStartOf="#+id/chevron"/>
<ImageView android:id="#+id/chevron"
android:src="#mipmap/ic_chevron_right_black_48dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:alpha="0.5"
android:contentDescription="go to url" />
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout android:id="#android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="end|center_vertical"
android:paddingStart="16dp"
android:orientation="vertical" />
</LinearLayout>
For custom preference, you need to set the click listener programmatically.
#Override
protected void onBindView(View view) {
super.onBindView(view);
view.setClickable(true);
if (view != null && !TextUtils.isEmpty(url)) {
NetworkImageView gravatar = ((NetworkImageView) view.findViewById(R.id.gravatar));
if (gravatar != null) {
gravatar.setImageUrl(url, App.getInstance().getImageLoader());
gravatar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("test","clicked");
}
}
}
}
}