I am trying to add a video played on button click with android studio.
However, when I click the button a "sorry, this video cannot be played" message box appears on the emulator screen.
Can you help me see where I'm going wrong.
Below is the code I approached the goal with
Trialvideo.java
package android.com.trialvideo;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
public class TrialVideoActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** // Video view: to view our video
VideoView video = (VideoView) findViewById(R.id.surface_view);
//set video path to our video(in this case man-cheetah-gazalle.3gp)
video.setVideoPath("/raw/jeewan.mp4");
video.start();
**/
final Button play =(Button)findViewById(R.id.play);
play.setOnClickListener(new OnClickListener(){
public void onClick(View V){
videoPlayer();
}
});}
public void videoPlayer(){
getWindow().setFormat(PixelFormat.TRANSLUCENT);
VideoView videoHolder = (VideoView)findViewById(R.id.surface_view);
videoHolder.setMediaController(new MediaController(this));
videoHolder.setVideoPath("/TrialVideo/raw/lic.3gp");
videoHolder.requestFocus();
videoHolder.start();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_height="50dip"
android:text="play"
android:id="#+id/play"
android:layout_width="50dip"
>
</Button>
<VideoView android:id="#+id/surface_view"
android:layout_width="475px"
android:layout_height="440px"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_height="50dip"
android:text="play"
android:id="#+id/play"
android:layout_width="50dip"
>
</Button>
<VideoView android:id="#+id/surface_view"
android:layout_width="475px"
android:layout_height="440px"
/>
</LinearLayout>
Hi try the following code:
VideoPlaying.java
public class VideoPlaying extends Activity {
private MediaController mc;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView vd = (VideoView) findViewById(R.id.VideoView);
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"+R.raw.VideoName);
mc = new MediaController(this);
vd.setMediaController(mc);
vd.requestFocus();
vd.setVideoURI(uri);
vd.start();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center">
<VideoView android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/VideoView"></VideoView>
</LinearLayout>
place the video on the raw folder and run the code. Sometimes video will not be correctly shown on the emulator, try to check it also on the actual device.
Android does not have a C: drive. You need to put the video file on the device (e.g., copy it to the device's external storage), then supply VideoView with an appropriate path to the file (e.g., using Environment.getExternalStorageDirectory()).
Related
When I am trying to run my app on my phone it is getting successfully installed in my phone but I am getting white screen instead of my splash page . Attaching my xml code and java code for your reference . Your help is highly appreciated !!
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="100dp"
android:fontFamily="sans-serif-black"
android:text="MY APP"
android:textColor="#color/Black"
android:textSize="50sp" />
</Relativelayout>
activity_main.java
package com.example.kriova;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(intent);
finish();
}
}
The finish() method is called and the activity destroys and returns to the home screen.
Your code should be like this
package com.example.kriova;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}}
remove the last line which is :-
finish();
I'm learning how videoView works. I followed a tutorial; this is my activity_main.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.simoc.videoviewdemo.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<VideoView
android:id="#+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
and this is my mainActivity.java:
<package com.simoc.videoviewdemo;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView myVideoView = (VideoView) findViewById(R.id.videoView);
myVideoView.setVideoPath("android.resource://"+ getPackageName()+"/"+R.raw.myvideo.3gp);
//myVideoView.setVideoURI(Uri.parse("http://archive.org/download/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4"));
//String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/myvideo.3gp";
//myVideoView.setVideoPath(path);
//Log.i("MY_PATH: ", path);
myVideoView.start();
}
}
Ok. First I create a "raw" folder unde "res" and put my video in it. I've set the videoPath and all works fine.
So I decided to try to stream a video. It works the link in the code but it doesn't work a random YouTube link (and I don't understand why).
But the real question is: why this 2 lines:
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/myvideo.3gp";
myVideoView.setVideoPath(path);
are wrong? I put the video in the /storage/emulated/0/myvideo but I always get the error can't play the video. I didn't find a different way to use videoView and so I really don't know where is my fault.
Oh, in the manifest I added the permissons:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
try this don't add extension name
VideoView myVideoView = (VideoView) findViewById(R.id.videoView);
String path = "android.resource://" + getPackageName() + "/" + R.raw.myvideo;
myVideoView.setVideoURI(Uri.parse(path));
myVideoView.start();
I've found out the ImageViewTouch since I wanted to create a zooming image preview on my app though I need to display that image on one of my DialogFragment. But it works just fine on a normal FragmentActivity which is the MainActivity but not in my DialogImage and I'm not sure why.
I imported the library using the gradle and here's my code so far:
The MainActivity
import it.sephiroth.android.library.imagezoom.ImageViewTouch;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
public class MainActivity extends FragmentActivity {
ImageViewTouch mImage;
#Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView( R.layout.fragment_main );
mImage = (ImageViewTouch) findViewById( R.id.image );
ImageDownloader2 imgDownload = new ImageDownloader2();
imgDownload.download("MY_URL", mImage);
Button click = (Button)findViewById(R.id.button);
click.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
showPromoImage();
}
});
}
private void showPromoImage(){
DialogImage promo_details = new DialogImage();
promo_details.show(getFragmentManager(), "promo_details");
}
}
The DialogImage
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import it.sephiroth.android.library.imagezoom.ImageViewTouch;
/**
* Created by Bahay on 2/19/14.
*/
public class DialogImage extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_image, container);
getDialog().setTitle("Test");
getDialog().setCanceledOnTouchOutside(true);
ImageViewTouch promo = (ImageViewTouch) view.findViewById(R.id.image);
ImageDownloader2 imgDownload = new ImageDownloader2();
imgDownload.download("MY_URL", promo);
return view;
}
}
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<it.sephiroth.android.library.imagezoom.ImageViewTouch
android:id="#+id/image"
android:layout_width="158dp"
android:layout_height="322dp"
android:scaleType="fitCenter" />
<LinearLayout
android:layout_weight="0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:gravity="center"
android:weightSum="2" >
<Button
android:id="#+id/button"
android:layout_width="0dp"
android:layout_weight="1"
android:padding="20dp"
android:layout_height="wrap_content"
android:text="Load Image" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_weight="1"
android:padding="20dp"
android:layout_height="wrap_content"
android:text="Change Display" />
</LinearLayout>
</LinearLayout>
dialog_image.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<it.sephiroth.android.library.imagezoom.ImageViewTouch
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter" />
</LinearLayout>
The ImageDownloader2 is just a library which downloads the image from the URL then convert it into bitmap. I just made some customizations for this to work with ImageViewTouch.
As for the ImageViewTouch you can see it here:
https://github.com/sephiroth74/ImageViewZoom
For the ImageDownloader2 class you can see it here:
http://theandroidcoder.com/utilities/android-image-download-and-caching/
I just replaced the ImageView with ImageViewTouch for the download method.
UPDATE:
Tried using a normal old-school dialog wherein I use Activity and Intent where I just themed the DialogImage as android:theme="#android:style/Theme.DeviceDefault.Dialog". Output is same where image is not loaded.
Hope someone can help. Thanks!
I am trying to open one screen from another in an Android App. I copied the outline of the following code from a tutorial online and then tried to substitute my own screens. When I try to run the following code, I get a "Force Close" when I click the button on the first screen (the second screen never displays). Can someone tell me what I am doing wrong? There are four files: ScreenTestActivity.java, main.xml, screen2.java and screen2.xml.
ScreenTestActivity.java
package com.birdscreen.android.testscreen;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ScreenTestActivity extends Activity
{
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.btnClick);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(ScreenTestActivity.this, screen2.class);
startActivity(i);
}
});
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You are in the first Screen"
/>
<Button
android:id ="#+id/btnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open New Screen"
/>
</LinearLayout>
screen2.java:
package com.birdscreen.android.testscreen;
import android.app.Activity;
import android.os.Bundle;
public class screen2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
}
}
screen2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You are in the New Screen"
/>
</LinearLayout>
<activity android:name="screen2"></activity>
<activity android:name="ScreenTestActivity"></activity>
put this 2 line of code in your androidmanifest.xml file
Because i'm create an app similar to what was shown below but i have no idea how to create a view similar to what was shown above after i click a buttton to navigate to this page where all the video and map log file is shown..
I'm kinna new in android/java can someone guide me on this?
EDIT: This is a series of code for creating a directory to store my video files but these files can only be seen outside the application but i wanted it to be seen in the application where when a button is pressed it navigates to the VideoList to browse the various videa file i have filmed and when press it display a custom screen seen here. But what are the things should be done to achieve this?
File dirlist = new File(Environment.getExternalStorageDirectory() + "/VideoList");
if(!(dirlist.exists()))
dirlist.mkdir();
File TempFile = new File(Environment.getExternalStorageDirectory() + "/VideoList", dateFormat.format(date) + fileFormat);
mediaRecorder.setOutputFile(TempFile.getPath());
Hay Zack its a custom Dialog check out this... code
main.xml(Your main page layout)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is my main activity, from here, I want to display a dialog, after the user clicked the button below this text.">
</TextView>
<Button android:layout_height="wrap_content"
android:layout_below="#+id/TextView01"
android:layout_width="wrap_content"
android:id="#+id/Button01main"
android:text="Hey! There is more..."></Button>
</RelativeLayout>
maindialog.xml(Your Dialog layout page)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView android:id="#+id/ImageView01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<ScrollView android:id="#+id/ScrollView01"
android:layout_width="wrap_content" android:layout_below="#+id/ImageView01"
android:layout_height="200px">
<TextView android:text="#+id/TextView01" android:id="#+id/TextView01"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</ScrollView>
<Button android:id="#+id/Button01" android:layout_below="#id/ScrollView01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:text="Cancel" />
</RelativeLayout>
dilo.java(Display dialog on click button code)
package com.example.dilo;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class dilo extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1main = (Button) findViewById(R.id.Button01main);
button1main.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//set up dialog
final Dialog dialog = new Dialog(dilo.this);
dialog.setContentView(R.layout.maindialog);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!
//set up text
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText(R.string.lots_of_text);
//set up image view
ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
img.setImageResource(R.drawable.icon);
//set up button
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.cancel();
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}
});
}
}
i think it should help you .... ok... N joy
zack it is custom dialog u need to create custom dialog
for more info.. go this link 1) link 2