TextView Does Not Overlay VideoView Inside of FrameLayout - android

I am trying to have a TextView displayed on top (overlay) of a VideoView, and it is not happening. I have the two elements inside of a FrameLayout with the TextView positions below the VideoView. From my understanding, this is supposed to place it on top.
I have tried various ways of adding the TextView programmatically and removing the additional features of the VideoView e.g. onTouchListener().
Does anyone have any suggestions on how to fix this problem or an explanation of overlaying views that could help me with this problem? Any help would be greatly appreciated. I have posted code below:
activity_splash.xml
<FrameLayout 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"
tools:context="com.androidtitan.hotspots.Activity.SplashActivity">
<VideoView
android:id="#+id/splashVideo"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView android:id="#+id/splashTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_marginTop="25dp"
android:text="placeholder text"
android:textSize="60dp"
android:textStyle="bold"
android:textColor="#android:color/black"/>
SplashActivity.java
package com.androidtitan.hotspots.Activity;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.VideoView;
import com.androidtitan.hotspots.R;
public class SplashActivity extends Activity {
private static final String TAG = "hotspots";
TextView titleTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
titleTextView = (TextView) findViewById(R.id.splashTitle);
try{
splashScreen();
} catch (Exception e) {
//todo: we could display a picture here as an alternative
Log.e(TAG, String.valueOf(e));
}
//this returns
if(titleTextView.isShown()) {
Log.e(TAG, "titleTextView.isShown()");
}
else {
Log.e(TAG, "NOT SHOWN");
}
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
return false;
}
public void splashScreen() {
VideoView videoHolder = new VideoView(this);
setContentView(videoHolder);
Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.splash);
videoHolder.setVideoURI(video);
videoHolder.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
jumpMain(); //jump to the next Activity
}
});
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
videoHolder.setLayoutParams(new FrameLayout.LayoutParams(metrics.widthPixels, metrics.heightPixels));
videoHolder.start();
videoHolder.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
((VideoView) v).stopPlayback();
jumpMain();
return true;
}
});
}
private synchronized void jumpMain() {
Intent intent = new Intent(SplashActivity.this, ChampionActivity.class);
startActivity(intent);
finish();
}
}

You are creating a new VideoView instead of using the one in your XML layout and also calling setContentView which replaces the XML layout which loaded first with your new VideoView. That's why its failing. Do not call setContentView twice and change your code as below
public void splashScreen() {
VideoView videoHolder = (VideoView) findViewById(R.id.splashVideo);
Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.splash);
videoHolder.setVideoURI(video);
videoHolder.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
jumpMain(); //jump to the next Activity
}
});
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
videoHolder.setLayoutParams(new FrameLayout.LayoutParams(metrics.widthPixels, metrics.heightPixels));
videoHolder.start();
videoHolder.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
((VideoView) v).stopPlayback();
jumpMain();
return true;
}
});
}

Related

Unexpected behaviour of MediaPlayer Android

I am trying to stream mp3 file from the internet. App successfully started but showing unexpected behavior. The playback doesn't play sound, the play button doesn't changes its icon. The playback stops after showing "please wait message" when it is expected to play the file on clicking the play icon.
Here's my code:
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import java.util.concurrent.TimeUnit;
import dyanamitechetan.vusikview.VusikView;
public class MainActivity extends AppCompatActivity implements MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnCompletionListener {
ImageButton imageButton;
SeekBar seekBar;
TextView textView;
VusikView musicView;
MediaPlayer mediaPlayer;
int mediaFileLength;
int realTimeLength;
final Handler handler = new Handler();
#SuppressLint("ClickableViewAccessibility")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
musicView = findViewById(R.id.musicView);
seekBar = findViewById(R.id.seekbar);
seekBar.setMax(99);
seekBar.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(mediaPlayer.isPlaying()){
SeekBar seekBar = (SeekBar) view;
int playPosition = (mediaFileLength/100)*seekBar.getProgress();
mediaPlayer.seekTo(playPosition);
}
return false;
}
});
imageButton = findViewById(R.id.btn_play_pause);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final ProgressDialog mDialog = new ProgressDialog(MainActivity.this);
AsyncTask<String,String,String>mp3Play = new AsyncTask<String, String, String>() {
#Override
protected void onPreExecute(){
mDialog.setMessage("Please wait");
mDialog.show();
}
#Override
protected String doInBackground(String... strings) {
try {
mediaPlayer.setDataSource(strings[0]);
mediaPlayer.prepare();
}
catch (Exception ex){
}
return "";
}
#Override
protected void onPostExecute(String s) {
mediaFileLength = mediaPlayer.getDuration();
realTimeLength = mediaFileLength;
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
imageButton.setImageResource(R.drawable.ic_pause);
} else {
mediaPlayer.pause();
imageButton.setImageResource(R.drawable.ic_play);
}
updateSeekBar();
mDialog.dismiss();
}
};
mp3Play.execute("https://soundcloud.com/theastonshuffle/nasa-feat-kanye-west-santogold-lykke-li-gifted-aston-shuffle-long-mix");
musicView.start();
}
});
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnCompletionListener(this);
}
private void updateSeekBar() {
seekBar.setProgress((int)((float)mediaPlayer.getCurrentPosition() / mediaFileLength*100));
if(mediaPlayer.isPlaying()){
Runnable updater = new Runnable() {
#Override
public void run() {
updateSeekBar();
realTimeLength-=1000;
textView.setText(String.format("%d:%d", TimeUnit.MILLISECONDS.toMinutes(realTimeLength),
TimeUnit.MILLISECONDS.toSeconds(realTimeLength) -
TimeUnit.MILLISECONDS.toSeconds(TimeUnit.MILLISECONDS.toMinutes(realTimeLength))));
}
};
handler.postDelayed(updater,1000);
}
}
#Override
public void onBufferingUpdate(MediaPlayer mediaPlayer, int i) {
seekBar.setSecondaryProgress(i);
}
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
imageButton.setImageResource(R.drawable.ic_play);
musicView.stopNotesFall();
}
}
activity_main.xml
<dyanamitechetan.vusikview.VusikView
android:id="#+id/musicView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textTimer"
android:layout_centerHorizontal="true"
android:text="00:00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageButton
android:layout_below="#id/textTimer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btn_play_pause"
android:src="#drawable/ic_play"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/btn_play_pause"
android:id="#+id/seekbar"/>
</RelativeLayout>
you APP connect Internet ?
please check manifest is add internet permission
You can use the mediaplay.setdataSource (url);method directly to load
And setmediaplay.prepareAsync (); use asynchronous method to load music
No need to use AsyncTask

VideoView black background in Fragment

I adding VideoView in fragment but only I get black background nothing more I tried with two codes but both doesn't work can you help me?
PS I don't need play, stop button and anything other just to show the video Here is the code that I add will be good if I can mute the audio of the video and replay
Code 1
package com.Hristijan.Aleksandar.GymAssistant.Exercises;
import android.media.session.MediaController;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.VideoView;
import java.net.URL;
/**
* A simple {#link Fragment} subclass.
*/
public class BenchFragment extends Fragment {
private VideoView MyVideoView;
public BenchFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_bench, container, false);
MyVideoView = (VideoView)rootView.findViewById(R.id.video_view);
Uri uri= Uri.parse("android.resource://"+getActivity().getPackageName()+"/"+R.raw.bench);
MyVideoView.setVideoURI(uri);
MyVideoView.start();
return inflater.inflate(R.layout.fragment_bench, container, false);
}
}
Code2
package com.hristijan.aleksandar.gymworkout.myapplication;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.VideoView;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private VideoView MyVideoView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyVideoView = findViewById(R.id.videoViewId);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.bench);
MyVideoView.setVideoURI(uri);
MyVideoView.start();
}
}
Layout.xml
<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:orientation="vertical"
android:background="#000000"
tools:context="com.Hristijan.Aleksandar.GymAssistant.Exercises.BenchFragment">
<VideoView
android:id="#+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Try this to play,replay and identify the error
private void startVideo() {
String path = "android.resource://" + getPackageName() + "/" + R.raw.crop;
// Log.e(TAG,Uri.parse(path) + " ");
video_view.setVideoURI(Uri.parse(path));
video_view.start();
video_view.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int i, int i1) {
Log.e(TAG,String.format("Error: What: %d, Extra: %d",i,i1));
return false;
}
});
video_view.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.start();
}
});
}
If the video doesn't play it should log error in LogCat. You can identify what kind of error using this doc
About Muting the Audio
You can refer to this Question basically you just need to get the media player object when the content is ready then you can mute the audio by setting m.setVolume(0f, 0f);
I am sure that Code 1 and Code 2 can work
First, make sure your video format is Supported Media Formats
Then, make sure your video file bench stored in ...app\src\main\res\raw (And your file name should be bench not bench.xxx)
On the other hand, in Code 2 something is wrong:
MyVideoView = findViewById(R.id.videoViewId);
should be
MyVideoView = findViewById(R.id.video_view);
in order to find a VideoView by correct Id
Try putting videoView.start() inside videoView.setOnPreparedListener() as:
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mediaPlayer){
videoView.start();
}
});

MediaController doesn't seem to show up

I have an android app with a detail activity, that detail activity has few tabs with underlying fragments. One of these fragments is plain list with a name of audio files when I click each line I'm playing the clicked audio files.
Everything works OK, but I would like to have some means of seeing a progress of the playback and ability to stop a playback, I was trying to add MediaController but even though I'm creating it without errors no media control is visible on a screen.
I'm not sure if I'm doing it in a correct way any help you would be appreciated.
Thanks a lot,
Radek
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
try {
if(convertView == null){
convertView = layoutInflater.inflate(R.layout.audio_row,parent,false);
}
TextView textView = (TextView) convertView.findViewById(R.id.rowText);
textView.setText("" + jsonAudio.getJSONObject(position).getString("description"));
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
String audioFileName = jsonAudio.getJSONObject(position).getString("file");
Toast.makeText(context, "audio item clicked: " + audioFileName, Toast.LENGTH_SHORT).show();
AssetFileDescriptor descriptor = context.getAssets().openFd(audioFileName);
long start = descriptor.getStartOffset();
long end = descriptor.getLength();
MediaPlayer mediaPlayer=new MediaPlayer();
mediaPlayer.setDataSource(descriptor.getFileDescriptor(), start, end);
mediaPlayer.prepare();
// not doing really anything
MediaController mediaController = new MediaController(context);
mediaController.setAnchorView(v);
mediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
Please check the below code for your refrence
In your code you have passed the TextView in the mediaController need to pass the SurfaceView instead of TextView and you need to set the MediaPlayer in the MediaController
JAVA file.
import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Handler; import android.util.Log;
import android.media.MediaPlayer.OnPreparedListener; import android.view.MotionEvent; import android.widget.MediaController; import android.widget.TextView;
import java.io.IOException;
public class AudioPlayer extends Activity implements OnPreparedListener, MediaController.MediaPlayerControl{ private static final String TAG = "AudioPlayer";
public static final String AUDIO_FILE_NAME = "audioFileName";
private MediaPlayer mediaPlayer; private MediaController mediaController; private String audioFile;
private Handler handler = new Handler();
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.audio_player);
audioFile = this.getIntent().getStringExtra(AUDIO_FILE_NAME);
((TextView)findViewById(R.id.now_playing_text)).setText(audioFile);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(this);
mediaController = new MediaController(this);
try {
mediaPlayer.setDataSource(audioFile);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
Log.e(TAG, "Could not open file " + audioFile + " for playback.", e);
}
}
#Override protected void onStop() { super.onStop(); mediaController.hide(); mediaPlayer.stop(); mediaPlayer.release(); }
#Override public boolean onTouchEvent(MotionEvent event) { //the MediaController will hide after 3 seconds - tap the screen to make it appear again mediaController.show(); return false; }
//--MediaPlayerControl methods---------------------------------------------------- public void start() { mediaPlayer.start(); }
public void pause() { mediaPlayer.pause(); }
public int getDuration() { return mediaPlayer.getDuration(); }
public int getCurrentPosition() { return mediaPlayer.getCurrentPosition(); }
public void seekTo(int i) { mediaPlayer.seekTo(i); }
public boolean isPlaying() { return mediaPlayer.isPlaying(); }
public int getBufferPercentage() { return 0; }
public boolean canPause() { return true; }
public boolean canSeekBackward() { return true; }
public boolean canSeekForward() { return true; } //--------------------------------------------------------------------------------
public void onPrepared(MediaPlayer mediaPlayer) { Log.d(TAG, "onPrepared"); mediaController.setMediaPlayer(this); mediaController.setAnchorView(findViewById(R.id.main_audio_view));
handler.post(new Runnable() {
public void run() {
mediaController.setEnabled(true);
mediaController.show();
}
});
} }
XML file-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_audio_view" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_gravity="center"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:text="Now playing:"
android:textSize="25sp" android:textStyle="bold" /> <TextView
android:id="#+id/now_playing_text" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginTop="20dip"
android:layout_marginLeft="10dip" android:layout_marginRight="10dip"
android:layout_gravity="center" android:text="Now playing.."
android:textSize="16sp" android:textStyle="italic" /> </LinearLayout>
I had to tweak Patrick's solution a bit to play the file from assets folder but it worked great, here is reformatted java file for easy cut and paste if anyone needs it.
import android.content.res.AssetFileDescriptor;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.media.MediaPlayer.OnPreparedListener;
import android.view.MotionEvent;
import android.widget.MediaController;
import android.widget.TextView;
import java.io.IOException;
public class MediaPlayer extends AppCompatActivity implements OnPreparedListener, MediaController.MediaPlayerControl {
private static final String TAG = "AudioPlayer";
private android.media.MediaPlayer mediaPlayer;
private MediaController mediaController;
private String audioFileName = "item1_audio1.mp3";;
private Handler handler = new Handler();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_audio_view);
((TextView) findViewById(R.id.now_playing_text)).setText(audioFileName);
mediaPlayer = new android.media.MediaPlayer();
mediaPlayer.setOnPreparedListener(this);
mediaController = new MediaController(this);
try {
AssetFileDescriptor descriptor = MediaPlayer.this.getAssets().openFd(audioFileName);
long start = descriptor.getStartOffset();
long end = descriptor.getLength();
mediaPlayer.setDataSource(descriptor.getFileDescriptor(), start, end);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
Log.e(TAG, "Could not open file " + audioFileName + " for playback.", e);
}
}
#Override
public void onPrepared(android.media.MediaPlayer mp) {
Log.d(TAG, "onPrepared");
mediaController.setMediaPlayer(this);
mediaController.setAnchorView(findViewById(R.id.main_audio_view));
handler.post(new Runnable() {
public void run() {
mediaController.setEnabled(true);
mediaController.show();
}
});
}
#Override
protected void onStop() {
super.onStop();
mediaController.hide();
}
#Override
public boolean onTouchEvent(MotionEvent event) { //the MediaController will hide after 3 seconds - tap the screen to make it appear again
mediaController.show();
return false;
}
public void start() {
mediaPlayer.start();
}
public void pause() {
mediaPlayer.pause();
}
public int getDuration() {
return mediaPlayer.getDuration();
}
public int getCurrentPosition() {
return mediaPlayer.getCurrentPosition();
}
public void seekTo(int i) {
mediaPlayer.seekTo(i);
}
public boolean isPlaying() {
return mediaPlayer.isPlaying();
}
public int getBufferPercentage() {
return 0;
}
public boolean canPause() {
return true;
}
public boolean canSeekBackward() {
return true;
}
public boolean canSeekForward() {
return true;
}
#Override
public int getAudioSessionId() {
return 0;
}
#Override
public void onStart() {
super.onStart();
}
}

Some Android smartphones can't play online video with VideoView

I use android.widget.VideoView play online video, Some phones( Common Feature is use Qualcomm CPU) can't play, other can work well.
What is the cause of this ?
Code
package com.example.vv;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
/**
* #author jren
* #parameter
* #return
*/
public class MainActivity extends Activity {
private static final String TAG = "LiveVideoPlay";
private VideoView myVideoView;
private MediaController mediaControls;
MediaController mediaController;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myVideoView = (VideoView) findViewById(R.id.videoview);
playVideo();
}
private void playVideo() {
try {
if (mediaController == null) {
mediaController = new MediaController(MainActivity.this);
}
myVideoView.setMediaController(mediaController);
String StrUri = "http://huison.oss-cn-shenzhen.aliyuncs.com/0001/huison001/live.m3u8";
Uri videoUri = Uri.parse(StrUri);
myVideoView.setVideoURI(videoUri);
myVideoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
myVideoView.start();
}
});
myVideoView.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return false;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
<RelativeLayout 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"
tools:context="com.example.vv.MainActivity" >
<VideoView
android:id="#+id/videoview"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
the error:
07-14 15:14:25.872: E/MediaPlayer-JNI(25660): QCMediaPlayer mediaplayer NOT present
07-14 15:14:27.872: W/MediaPlayer(25660): info/warning (801, 0)

Unable to access views in android

I am using metaio sdk. I am trying to simply toggle the visibility of two imageviews when pressing a button but it is not working.
My layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ma_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000" >
-----
<ImageView
android:id="#+id/zoomIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="4dp"
android:layout_marginRight="4dp"
android:background="#drawable/zooming"
android:onClick="seeZoom" />
<ImageView
android:id="#+id/scrollerBg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/scrollerbg"
android:visibility="invisible"/>
<ImageView
android:id="#+id/scroller"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/scroller"
android:visibility="invisible"/>
---
</RelativeLayout>
My code:
import android.view.MotionEvent;
import java.util.List;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import com.metaio.sdk.jni.IGeometry;
import com.metaio.sdk.jni.IMetaioSDKAndroid;
import com.metaio.sdk.jni.Vector3d;
import android.os.Environment;
import com.metaio.sdk.ARViewActivity;
import com.metaio.sdk.jni.EPLAYBACK_STATUS;
import com.metaio.sdk.jni.IMetaioSDKCallback;
import com.metaio.sdk.jni.MovieTextureStatus;
import com.metaio.sdk.jni.Rotation;
import com.metaio.sdk.MetaioDebug;
import com.company.abc.R;
public class MainActivity extends ARViewActivity
{
public RelativeLayout mGUIView;
//public ImageView imgView1;
//public ImageView imgView2;
Camera camera;
private IGeometry tdp1, tdp2, tdp3, tdp4, tdp5, tdp6, tdp7, sal1, sal2;
boolean isTorchOn=false;
Parameters camParams;
public ImageView basePng;
public ImageView zoomIcon;
ImageView scroller;
public LayoutParams scrollerParams;
ImageView scrollerBg;
public LayoutParams scrollerBgParams;
boolean afc;
int counter;
int displayWidthbyTwo;
int displayHeightbyTwo;
int scrollerW;
int scrollerH;
int scrollerBgW;
int scrollerBgH;
int ZoomValue=0;
int maxZoomLevel;
int maxZoombyfour;
boolean zoomSupported;
boolean isZoomBarVisible=false;
private MetaioSDKCallbackHandler mCallbackHandler;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
MetaioDebug.enableLogging(true);
mCallbackHandler = new MetaioSDKCallbackHandler();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
displayWidthbyTwo=metrics.widthPixels/2;
displayHeightbyTwo=metrics.heightPixels/2;
PackageManager PM= this.getPackageManager();
afc = PM.hasSystemFeature(PackageManager.FEATURE_CAMERA_AUTOFOCUS);
setContentView(R.layout.mainactivity);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mGUIView = (RelativeLayout) getLayoutInflater().inflate(R.layout.mainactivity, null);
zoomIcon = (ImageView) mGUIView.findViewById(R.id.zoomIcon);
scroller = (ImageView) mGUIView.findViewById(R.id.scroller);
scrollerBg = (ImageView) mGUIView.findViewById(R.id.scrollerBg);
scroller.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
scrollerW = scroller.getMeasuredWidth()/2;
scrollerH = scroller.getMeasuredHeight()/2;
scrollerParams = (LayoutParams) scroller.getLayoutParams();
scrollerParams.topMargin = displayHeightbyTwo-150-scrollerH;
scroller.setLayoutParams(scrollerParams);
scrollerBg.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
scrollerBgW = scrollerBg.getMeasuredWidth()/2;
scrollerBgH = scrollerBg.getMeasuredHeight()/2;
scrollerBgParams = (LayoutParams) scrollerBg.getLayoutParams();
scrollerBgParams.topMargin = displayHeightbyTwo-scrollerBgH;
scrollerBg.setLayoutParams(scrollerBgParams);
scroller.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
LayoutParams layoutParams = (LayoutParams) scroller.getLayoutParams();
switch(event.getAction())
{
case MotionEvent.ACTION_MOVE:
//Get y coord of the touch point relative to screen
int y_cord = (int) event.getRawY();
//Restrict scroller bewtween center of screen +- 150
if(y_cord>(displayHeightbyTwo+150))
y_cord = displayHeightbyTwo+150;
if(y_cord<(displayHeightbyTwo-150))
y_cord = displayHeightbyTwo-150;
//Set zoom levels at various steps
if(y_cord<=displayHeightbyTwo-90){
camParams.setZoom(0);
Log.i("Zooming:","0");
ZoomValue=0;}
if(y_cord<=displayHeightbyTwo-30 && y_cord>displayHeightbyTwo-90){
camParams.setZoom(maxZoombyfour);
Log.i("Zooming:","1");
ZoomValue=1;}
if(y_cord<=displayHeightbyTwo+30 && y_cord>displayHeightbyTwo-30){
camParams.setZoom(maxZoombyfour*2);
Log.i("Zooming:","2");
ZoomValue=2;}
if(y_cord<=displayHeightbyTwo+90 && y_cord>displayHeightbyTwo+30){
camParams.setZoom(maxZoombyfour*3);
Log.i("Zooming:","3");
ZoomValue=3;}
if(y_cord>displayHeightbyTwo+90){
camParams.setZoom(maxZoomLevel);
Log.i("Zooming:","4");
ZoomValue=4;}
camera.setParameters(camParams);
scrollerParams.topMargin = y_cord-scrollerH;
scroller.setLayoutParams(scrollerParams);
break;
default:
break;
}
return true;
}
});
}
#Override
protected int getGUILayout()
{
// TODO: return 0 in case of no GUI overlay
return R.layout.mainactivity;
}
#Override
protected void onStart()
{
super.onStart();
// hide GUI until SDK is ready
//if (!mRendererInitialized)
//mGUIView.setVisibility(View.GONE);
// add GUI layout
addContentView(mGUIView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
#Override
protected void loadContents()
{
// Load desired tracking data for planar marker tracking
String Path = "storage/sdcard1/AS/tdp/";
//final String trackingConfigFile = AssetsManager.getAssetPath("Tracking.xml");
boolean result = metaioSDK.setTrackingConfiguration(Path+"Tracking.xml");
String movie1Path = Path + "movie1.3gp";
tdp1 = metaioSDK.createGeometryFromMovie(movie1Path, false) ;
tdp1.setCoordinateSystemID(1);
tdp1.setScale(new Vector3d(4.0f,4.0f,4.0f));
tdp1.startMovieTexture(true); // loop = true;
}
#Override
protected void onGeometryTouched(IGeometry geometry) {
// TODO Auto-generated method stub
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
#Override
public void onSurfaceChanged(int width, int height) {
//always call the super implementation first
super.onSurfaceChanged(width, height);
camera=IMetaioSDKAndroid.getCamera(this);
camParams = camera.getParameters();
if(afc){
List<String> focusModes = camParams.getSupportedFocusModes();
if(focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE))
camParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
else if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO))
camParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
camera.setParameters(camParams);
}
if(camParams.isZoomSupported()){
this.runOnUiThread(new Runnable(){
public void run(){
zoomIcon.setVisibility(View.VISIBLE);
}
});
maxZoomLevel = camParams.getMaxZoom();
maxZoombyfour = Math.round(maxZoomLevel/4);
zoomSupported=true;
}
}
#Override
protected IMetaioSDKCallback getMetaioSDKCallbackHandler()
{
return mCallbackHandler;
}
final class MetaioSDKCallbackHandler extends IMetaioSDKCallback
{
}
public void showTorch(View v) {
if(isTorchOn){
IMetaioSDKAndroid.stopTorch(this);
isTorchOn=false;
}
else{
IMetaioSDKAndroid.startTorch(this);
isTorchOn=true;
}
}
public void seeZoom(View v) {
if(isZoomBarVisible){
scroller.setVisibility(0);
//scrollerBg.setVisibility(View.INVISIBLE);
isZoomBarVisible=false;
Log.i("ss","ss");
}
else{
scroller.setVisibility(1);
isZoomBarVisible=true;
Log.i("ss","ss");
}
}
public void seeSettings(View v) {
Intent intent = new Intent(this, Settings.class);
startActivity(intent);
//finish();
}
public void seeCatalog(View v) {
Intent intent = new Intent(this, CatalogueActivity.class);
startActivity(intent);
//finish();
}
}
I am trying in multiple ways since hours but none of them work . plesae tell me how to access the views from xml. This code used to work previously but it is not working now. Android programming really seems frustrating.
Thanks in advance
You must use setContent(R.layout.your_layout); inside onCreate
Like this
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//mGUIView = (RelativeLayout) getLayoutInflater().inflate(R.layout.scan, null);
setContent(R.layout.your_layout);
scroller = (ImageView) mGUIView.findViewById(R.id.scroller);
scrollerBg = (ImageView) mGUIView.findViewById(R.id.scrollerBg);
}
I guess the error is at 'R',
All you need to do is import your package.R
Example
...
import com.yourpackage.yourappname.R; //Import this
#Override
public void onCreate(Bundle savedInstanceState) {
MetaioCloudPlugin.startJunaio(null, getApplicationContext());
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
...
Then it should work fine :D
I encountered the same issue before. I managed to solve it by clearing the cache, data and uninstall the app from the device. It is working after I re-publish the app to the testing device. Maybe you could try it.

Categories

Resources