Android: Playing video issue - android

Out there, there are a lot of examples about how to play video on Android (API level 16).
But I can't understand why they don't work for me.
This is a simple example:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="my.owm.package" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".ContentActivity"
android:label="#string/title_activity_content" >
</activity>
</application>
</manifest>
activity_content.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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="my.owm.package.ContentActivity"
android:orientation="vertical">
<VideoView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/videoView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
contentActivity.java
public class ContentActivity extends Activity {
private static final String TAG = ContentActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
VideoView videoView =(VideoView) findViewById(R.id.videoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setVideoPath("/sdcard/Movies/test.mp4");
videoView.setMediaController(mediaController);
videoView.requestFocus();
videoView.start();
}
}
The test.mp4 files is actually there and runs fine with any player (btw it's a H264 w/ Baseline Profile). Anyway i tried several other videos, most of them used by the authors of the tutorial on this topic.
I get no errors but no video and no audio.
I'm pretty sure I'm missing something obvious because I found this code in a lot of Android tutorials!
Any idea?
Thanks!
EDIT:
I run the code on a physical device.
However I discovered the code is actually working until I add the following item in the layout xml file:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_margin="0dp" />

I worked on this way :
public class AnVideoView extends Activity {
String SrcPath = "/sdcard/Video/Android in Spaaaaaace!_low.mp4";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
myVideoView.setVideoPath(SrcPath);
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();
}
}

I also have come across issues with VideoView like this across multiple devices before.
Firstly I would consider re-encoding your videos using a tool such as ffmpeg that can be downloaded here.
The following encoding has always worked for me:
ffmpeg -i input.mp4 -c:v libx264 -profile:v baseline -c:a libfaac -ar 44100 -ac 2 -b:a 128k -movflags faststart output.mp4
Alternatively, some devices cannot handle videos with 'large' dimensions. Try resizing your videos to a smaller size and seeing if that helps also.

SOLVED: the DrawerLayout should be added at the END of the layout... it seems the order is important!

Related

How to enable video playing in full screen in my Android Webview

I tried to enable graphics acceleration but didn't worked for me
Here is my code
Click here to see
I am getting this error too click here too see
Please help me I am stuck
Can someone help me with the code
Probably you should use VideoView for this purpose. It allows to view videos from some url in fullscreen.
First, create a player activity xml file with VideoView:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<VideoView android:id="#+id/video_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
In your AndroidManifest.xml for your PlayerActivity set screenOrientation attribute to landscape:
<activity android:name=".PlayerActivity"
android:screenOrientation="landscape" />
Finally, in your PlayerActivity.kt set fullscreen flags and load your video url (or local file - check documentation):
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Fullscreen mode
requestWindowFeature(Window.FEATURE_NO_TITLE)
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
setContentView(R.layout.activity_player)
// Load your video file
video_view.setVideoURI(Uri.parse(intent.getStringExtra(URL_TO_VIEW)))
video_view.setMediaController(MediaController(this))
video_view.requestFocus(0)
video_view.start()
}
Or if you write in Java, in your PlayerActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Fullscreen mode
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_player);
// Load your video file
VideoView videoView = findViewById(R.id.video_view);
videoView.setVideoURI(Uri.parse(getIntent().getStringExtra(URL_TO_VIEW)));
videoView.setMediaController(new MediaController(this));
videoView.requestFocus(0);
videoView.start();
}

Android WebView resize default audio player

I am using webview in my application where i am giving it a audio url.
Its working absolutely fine in some devices. see below screenshot:
But in some device its not looking user friendly.
Is there any way to set the size of webview audio player?
Update: Code snippet:
// open DOI activity
Intent podcastIntent = new Intent(mContext, DoiWebActivity.class);
podcastIntent.setAction(Constants.INTENT_VIEW_LINK);
podcastIntent.putExtra(Constants.EXTRA_DOI_LINK_URL, podcasts.get(0).getPodcastLink());
podcastIntent.putExtra(Constants.CALLING_FRAGMENT,Constants.CALLING_FRAGMENT_PODCAST_LISTING);
podcastIntent.putExtra(Constants.EXTRA_PODCAST_NAME,mContext.getString(R.string.podcast));
mContext.startActivity(podcastIntent);
//Called from podcastListingFragment : podcast name
if (bundle != null && bundle.containsKey(Constants.CALLING_FRAGMENT) &&
bundle.containsKey(Constants.EXTRA_PODCAST_NAME)) {
String title = bundle.getString(SilverChairConstants.EXTRA_PODCAST_NAME);
}
if (Utility.isNetworkAvailable(mActivity)) {
/**
* Showing external link.
*/
mWebView.loadUrl(mIntentText);
} else {
networkNotAvailable();
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="#android:color/white"
tools:context=".fragments.WebFragment">
<WebView
android:layout_below="#+id/toolbar"
android:id="#+id/wv_webcontainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--progress bar-->
<include layout="#layout/silverchair_progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="#id/wv_webcontainer"
android:layout_alignBottom="#id/wv_webcontainer"/>
<include layout="#layout/empty_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>
add this line in your code where you are initializing your webview. mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
If you have already done this I would recommend you to use MediaPlayer for straming.
MediaPlayer mediaPlayer = MediaPlayer.create(this, Uri.parse("http://vprbbc.streamguys.net:80/vprbbc24.mp3"));
mediaPlayer.start();
See this answer for more details about MediaPlayer streaming.

How to display two videos simultaneously with surface view

I am a beginner in android development.
I was trying to display a video with surface view using media codec for which i am successful.
Now I want to add one more video at run time which has to be displayed or hidden as per the wish of the user or to switch between the two.
Can I have some suggestions for the same...
Thanks......
Try this
public class CustomPictureActivity extends Activity {
/** Called when the activity is first created. */
VideoView vd1,vd2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vd1=(VideoView) findViewById(R.id.v1);
vd2=(VideoView) findViewById(R.id.v2);
vd1.setVideoURI(Uri.parse("/mnt/sdcard/file.mp4"));
vd1.setMediaController(new MediaController(this));
vd1.requestFocus();
vd1.start();
vd2.setVideoURI(Uri.parse("/mnt/sdcard/android.mp4"));
vd2.setMediaController(new MediaController(this));
vd2.requestFocus();
vd2.start();
}
}
Your xml code should be like this:
<?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="horizontal" >
<VideoView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:id="#+id/v1"/>
<VideoView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:id="#+id/v2"/>
</LinearLayout>
May this will help you.

Android VideoView live tv stream (HLS)

I'am trying to develop app for tv streaming (HLS). Using code below I tested stream on 2.3.3, 3.0 and 4.0.1 version Android devices, but encountered several problems.
On Android 2.3.3 stream plays for >1 minute and then just stops. On Android 3.0 it plays well and on Android 4.0.3 it displays message 'This file cannot be played' (if I remember correctly).
So my question would be:
How can I play stream on ether of these devices, without having stream playing problems? Or where can I read more about solutions to these problems (tried to search but found nothing useful)?
Code in Main_Activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView player = (VideoView)findViewById(R.id.player);
String httpLiveUrl = "http://aj.lsops.net/live/aljazeer_en_high.sdp/playlist.m3u8";
//for Android 2.3.3 I used httplive:// prefix
player.setVideoURI(Uri.parse(httpLiveUrl));
player.setMediaController(new MediaController(this));
player.requestFocus();
player.start();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
Code in xml:
<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" >
<VideoView
android:id="#+id/player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Sorry if my english is poor.
Thank You.
This will not solve all the streaming issues. But one thing you should do is call player.start() when the MediaPlayer is ready. The selected answer to this SO post sets a listener on the MediaPlayer object so that it will run start() when onPrepared(MediaPlayer mp) is called.
Insert this Code to AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />

Problems with loading flash files into WebView

I'm trying to load a simple swf into an embebed WebView.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView wv = (WebView)findViewById(R.id.web);
WebSettings settings = wv.getSettings();
settings.setPluginState(PluginState.ON);
wv.loadUrl("file:///android_asset/example.swf");
}
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" >
<WebView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/web"/>
</LinearLayout>
I don't see anything wrong in this code... but still my web view shows black
By Checking the LogChat I have found this error:
11-30 12:46:32.170: E/libEGL(13839): call to OpenGL ES API with no current context (logged once per thread)
By is that? How can I solve it?
I am using this swf: http://www.tizag.com/pics/example.swf
I am working with a Motorola Xoom - Android 3.1.
Could anyone helpme with this?
I just find my own answer!!!! You should activate the attribute android:hardwareAccelerated in your aplication in your manifest like so:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:hardwareAccelerated="true" >

Categories

Resources