VideoView black background in Fragment - android

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();
}
});

Related

MediaPlayer: audio file on /res/raw detected as an Integer

I want to play an audio file when a Button clicked. But my code generate error code that says:
Cannot resolve method'create(context, int)'
So, my audio file is detected as an integer.
this is my fragment code:
package com.example.suha.belajarhurufhijaiyah;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
/**
* A simple {#link Fragment} subclass.
*/
public class Hijaiyah extends Fragment implements View.OnClickListener {
ImageButton alif;
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_hijaiyah, container, false);
alif = view.findViewById(R.id.alif_sound);
alif.setOnClickListener(this);
return view;
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.alif_sound:
MediaPlayer mp = MediaPlayer.create(this, R.raw.sound_alif);
mp.start();
}
}
}
That R.raw.sound_alif generate the error..
And it's my resource hier
you are trying to create MediaPlayer in Fragment use getActivity() or getContext() instead of this so use below code
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.alif_sound:
MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.sound_alif);
mp.start();
}
All resources are represented as Integers in the R class that is not your problem the problem is the Context your passing in a Fragment change this to getActivity So change your line to:
MediaPlayer mp = MediaPlayer.create(getActivity, R.raw.sound_alif);

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)

Android VideoView Activity crashing

I used the tutorial here (https://examples.javacodegeeks.com/android/android-videoview-example/) to build a videoview activity in my app though it keeps crashing when it's opened. I can't figure it out to save my life. I've checked the other posts on here and none of the suggestions have
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;
public class DemoActivity extends AppCompatActivity {
private VideoView myVideoView;
private int position = 0;
private ProgressDialog progressDialog;
private MediaController mediaControls;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quote);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// set the main layout of the activity
setContentView(R.layout.activity_main);
//set the media controller buttons
if (mediaControls == null) {
mediaControls = new MediaController(DemoActivity.this);
}
//initialize the VideoView
myVideoView = (VideoView) findViewById(R.id.video_view);
// create a progress bar while the video file is loading
progressDialog = new ProgressDialog(DemoActivity.this);
// set a title for the progress bar
progressDialog.setTitle("JavaCodeGeeks Android Video View Example");
// set a message for the progress bar
progressDialog.setMessage("Loading...");
//set the progress bar not cancelable on users' touch
progressDialog.setCancelable(false);
// show the progress bar
progressDialog.show();
try {
//set the media controller in the VideoView
myVideoView.setMediaController(mediaControls);
//set the uri of the video to be played
myVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.demo));
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
myVideoView.requestFocus();
//we also set an setOnPreparedListener in order to know when the video file is ready for playback
myVideoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mediaPlayer) {
// close the progress bar and play the video
progressDialog.dismiss();
//if we have a position on savedInstanceState, the video playback should start from here
myVideoView.seekTo(position);
if (position == 0) {
myVideoView.start();
} else {
//if we come from a resumed activity, video playback will be paused
myVideoView.pause();
}
}
});
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
//we use onSaveInstanceState in order to store the video playback position for orientation change
savedInstanceState.putInt("Position", myVideoView.getCurrentPosition());
myVideoView.pause();
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
//we use onRestoreInstanceState in order to play the video playback from the stored position
position = savedInstanceState.getInt("Position");
myVideoView.seekTo(position);
}
}
In your code have two setContentView() for two layout. That is reason of your error. You need merge two layout and sure that VideoView available on that view.

TextView Does Not Overlay VideoView Inside of FrameLayout

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;
}
});
}

Unable to play html5 streaming video in android web view

I have bought Foscam Security Camera and I am able to see the JPEG streaming on my MacBook.
But when I open the same link in my phone browser using chrome then it starts downloading something not sure what and in notification menu shows unsuccessful download.
Plus if I open the same link on my Android Firefox browser then I am able to see the video.
I have to create an android application to show the streaming of the video just as it is viewable on laptop browser.
The following is the code I am using:
package org.securitycamera;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.VideoView;
public class SecuritycameraActivity extends Activity {
WebView webView;
FrameLayout frameLayout;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflator = getLayoutInflater();
View inflatedView = inflator.inflate(R.layout.main, null);
if (!(inflatedView instanceof FrameLayout))
{
throw new RuntimeException("inflated view not FrameLayout");
}
else
{
frameLayout = (FrameLayout)inflatedView;
}
setContentView(frameLayout);
webView = (WebView) findViewById(R.id.wv);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(android.webkit.WebSettings.PluginState.ON);
webView.setWebChromeClient(new MyWebChromeClient());
try
{
// webView.loadUrl("http://192.168.1.6/videostream.cgi?user=admin&pwd=");
webView.loadUrl("http://broken-links.com/tests/video/");
}
catch(Exception e)
{
throw new RuntimeException();
}
}
private class MyWebChromeClient extends WebChromeClient implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, MediaPlayer.OnPreparedListener {
VideoView videoView;
WebChromeClient.CustomViewCallback customViewCallback;
public void onProgressChanged(WebView view, int newProgress)
{
if (newProgress == 100)
{
view.loadUrl("javascript:playVideo()");
}
}
public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)
{
if (view instanceof FrameLayout){
FrameLayout frame = (FrameLayout) view;
if (frame.getFocusedChild() instanceof VideoView){
VideoView video = (VideoView) frame.getFocusedChild();
frame.removeView(video);
setContentView(video);
video.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
//Log.i(DVNGActivity.TAG, "LoadData_QRURL --> onCompletion...");
mp.stop();
setContentView(R.layout.main);
}
});
video.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// Log.i(DVNGActivity.TAG, "LoadData_QRURL --> onError");
return false;
}
});
video.start();
}
}
}
public void onPrepared(MediaPlayer mp)
{
}
public void onCompletion(MediaPlayer mp)
{
// this is needed to release the MediaPlayer and its resources so it can
// be used again later
videoView.stopPlayback();
// now remove the video and tell the callback to hide the custom view
frameLayout.removeView(videoView);
customViewCallback.onCustomViewHidden();
finish();
}
public boolean onError(MediaPlayer mp, int what, int extra)
{
return false; // we did not handle the error - onCompletion will be called
}
}
}
I followed this How to Play HTML5 video and YouTube Video within Android WebView?, and if instead of playing the video in example I play the video of my security camera i.e.
webView.loadUrl("http://192.168.1.6/videostream.cgi?user=admin&pwd=");
I get a white screen.
this sample https://gist.github.com/3718414 has an Android webview wrapper and HTML5 video - it's not as simple as just referencing the URL (if you want the video in the webView) but it's not difficult on ICS and above (ensuring you have hardware acceleration enabled seems to be pretty key)

Categories

Resources