play Video in VideoView - android

In my activity I am trying to play a video which is stored in raw folder. below is my activity.
In the first button I am not getting error also like can't play this video, but only black screen appears. while clicking the second button I am getting a message that can't play this video. below is my activity
package com.example.college;
import android.app.Activity;
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 FirstYear extends Activity {
Button cse,it,ece,eee;
VideoView vv;
MediaController mc;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.first_year);
cse=(Button)findViewById(R.id.button1);
it=(Button)findViewById(R.id.button2);
ece=(Button)findViewById(R.id.button3);
eee=(Button)findViewById(R.id.button4);
vv=(VideoView)findViewById(R.id.videoView1);
mc=new MediaController(this);
mc.setAnchorView(vv);
cse.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
vv.setVideoURI(Uri.parse("android.resource://com.example.college/"+R.raw.c));
vv.start();
}
});
it.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
vv.setVideoURI(Uri.parse("android.resource://com.example.college/"+R.raw.k));
vv.start();
}
});
ece.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
vv.setVideoURI(Uri.parse("android.resource://com.example.college/"+R.raw.a));
vv.start();
}
});
eee.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
vv.setVideoURI(Uri.parse("android.resource://com.example.college/"+R.raw.s));
vv.start();
}
});
}
}

The code is correct. All the errors are related to video resolution.
I have replaced video with different resolution , and it is working

Related

back button shows blank screen

When I press the 'back' button of a mobile-phone it shows me a blank page. When I again press the 'back' button it then shows me the main page of the application. I want to get to the mainActivity page on first back press.
mainActivity code:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.text.style.SuperscriptSpan;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
Button b1,b2,b3,b4,b5,b6,b7;
EditText e1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.call1);
e1=(EditText)findViewById(R.id.call);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String no=e1.getText().toString();
Intent call=new Intent(Intent.ACTION_CALL);
call.setData(Uri.parse("tel:"+no));
startActivity(call);
}
});
b2=(Button)findViewById(R.id.sens_sms);
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent sendsms=new
Intent(getApplicationContext(),waytosms.class);
startActivity(sendsms);
}
});
b3=(Button)findViewById(R.id.facebook);
b3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent facebook=new Intent(MainActivity.this,facebook.class);
startActivity(facebook);
finish();
}
});
b4=(Button)findViewById(R.id.gmail);
b4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent facebook=new Intent(MainActivity.this,gmail.class);
startActivity(facebook);
}
});
b5=(Button)findViewById(R.id.utube);
b5.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent facebook=new Intent(MainActivity.this,utube.class);
startActivity(facebook);
}
});
b6=(Button)findViewById(R.id.google);
b6.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent facebook=new Intent(MainActivity.this,google.class);
startActivity(facebook);
}
});
b7=(Button)findViewById(R.id.twitter);
b7.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent facebook=new Intent(MainActivity.this,twitter.class);
startActivity(facebook);
}
});
}
}
This is the Facebook class I have used here with webview to open the Facebook page:
public class facebook extends Activity{
WebView facebook;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.facebook);
facebook=(WebView)findViewById(R.id.webfacebook);
facebook.getSettings().setJavaScriptEnabled(true);
facebook.loadUrl("https://facebook.com");
}
Here I have implemented onbackPressed() and called finished() The onbackpressed method isn't affecting this at all.
#SuppressLint("NewApi")
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
Intent i=new Intent(facebook.this,MainActivity.class);
startActivity(i);
}
}
1> first is main activity page where there are icon of social sites when
i click on facebook icon it will open facebook as 2nd facebookActivity page,3> when i clicked back it will open a blank page and4>. nd when i again press back button it will opened the main activity page...
Change your onBackPressed() method like this:
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
Intent i=new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
}

Android SpeechRecognizer service doesn't invoke listeners

My code for implementing speech recognition using SpeechRecognizer class seem correct....yet it does not function whatsoever. None of the log statements in any of the RecognitionListener methods are being logged.
I have follow the Android reference docs on this quite carefully and I am not seeing what I am missing.
I have added the record audio permission.
package com.example.voicetest;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class VoiceActivity extends Activity {
SpeechRecognizer recognizer;
TextView display;
private boolean listening;
private Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voice);
listening=false;
display=(TextView)findViewById(R.id.textView1);
button1=(Button)findViewById(R.id.button1);
recognizer=SpeechRecognizer.createSpeechRecognizer(this);
recognizer.setRecognitionListener(
new RecognitionListener()
{
#Override
public void onBeginningOfSpeech() {
// TODO Auto-generated method stub
Log.i("test","onBeginning");
}
#Override
public void onBufferReceived(byte[] arg0) {
// TODO Auto-generated method stub
Log.i("test","onBuffer");
}
#Override
public void onEndOfSpeech() {
// TODO Auto-generated method stub
Log.i("test","onEndOfSpeech");
}
#Override
public void onError(int error) {
// TODO Auto-generated method stub
Log.i("test","onError");
}
#Override
public void onEvent(int eventType, Bundle params) {
// TODO Auto-generated method stub
}
#Override
public void onPartialResults(Bundle partialResults) {
// TODO Auto-generated method stub
Log.i("test","onPartial");
}
#Override
public void onReadyForSpeech(Bundle params) {
// TODO Auto-generated method stub
display.setText("Ready and waiting..");
Log.i("test","onReady");
}
#Override
public void onResults(Bundle results) {
// TODO Auto-generated method stub
Log.i("test","onResults");
String result=stringListToString(results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION));
display.setText(result);
}
#Override
public void onRmsChanged(float rmsdB) {
// TODO Auto-generated method stub
}
}
);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.voice, menu);
return true;
}
public void buttonOnClick(View v)
{
if(!listening)
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);
recognizer.startListening(intent);
Log.i("test","recognizer listening");
listening=true;
button1.setText("Stop Listening");
}
else
{
recognizer.stopListening();
listening=false;
Log.i("test","recognizer stopped");
button1.setText("Listen");
}
}
private String stringListToString(ArrayList<String> strings)
{
String result="";
for(int i=0;i<strings.size();i++)
{
result=result+strings.get(i)+'\n';
}
return result;
}
#Override
public void onPause()
{
recognizer.stopListening();
}
}
You also need
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
Try the following:
Log out the error code. If it is giving server error then you are not connected to the internet.
If you want to use offline speech recognition, you need language models downloaded. Just verify that Speech input works on your keyboard (Press the mic button, say something and see if it is getting converted). If it is not getting recognized, then there is no error with your code, you just need to download speech models.

Open a new scene after video playback

I am learning android and I am trying to figure out how to open a new scene adfter a video view has finished.
This is my code:
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.VideoView;
public class SplashScene extends Activity implements OnCompletionListener {
BaseActivity activity;
VideoView video;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
video = (VideoView) findViewById(R.id.vvSplashVideo);
String path = "android.resource://" + getPackageName()
+ "/raw/splashscreen";
video.setVideoURI(Uri.parse(path));
video.setOnCompletionListener(this);
video.start();
loadResources();
}
public void loadResources() {
}
#Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
Log.v("Fin", "done");
activity.setCurrentScene(new MainMenuScene());
}
When I run the app, the video works and the log message works but then I get this error:
http://puu.sh/1YuiA before the app crashes.
Any ideas as to what I'm doing wrong.
you have to initialize activity;inside SplashScene class.
code:
public class SplashScene extends Activity implements OnCompletionListener {
BaseActivity activity;
....
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
activity=new BaseActivity (); <===== initiate this
// rest of code }
}
}

Seek bar progress when button pressed

I have made a simple android project with a single activity containing three buttons and a seek bar,Now i want is that when i press 1st button the seek bar should directly progressed 33%(without showing progress),when 2nd button pressed the seek bar should progressed again by 33% second time and when third button pressed the seek bar should full progressed..my code is as below:
Main.java
package com.example.seekabardemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
public class MainActivity extends Activity {
Button btn,btn1,btn2;
SeekBar sk;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.button1);
btn1=(Button)findViewById(R.id.button2);
btn2=(Button)findViewById(R.id.button3);
sk=(SeekBar)findViewById(R.id.seekBar1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Thank you....Please help me..!
Please help me ..thank you..!
write this three statements in click events of each button respectively.
sk.setProgress(sk.getMax()/3);
sk.setProgress(sk.getMax()*2/3);
sk.setProgress(sk.getMax());
To set a seekBar progress just :
sk.setProgress(ProgressToSet);
to get the maximum possible progress just:
sk.getMax();
to get a porcentage of a seekbar just:
sk.setProgress(sk.getMax()/100*YourPercentage);

Playback error on using two VideoViews

When starting playback of two HTTP streams in two VideoViews simultaneously, it works. When I try to stop one and start the other, it throws an error. Code used:
mVideoView1.setVideoPath(videoPath);
mVideoView2.setVideoPath(videoPath);
mVideoView1.start();
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mVideoView1.stopPlayback();
mVideoView2.start();
}
});
Any ideas what's causing this error? I get the same behaviour when I use MediaPlayer and SurfaceView (preparing second MediaPlayer in background, release() the first, then start() the second)
you can use the following code it work for me,
package com.materialexample;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.VideoView;
import in.co.bhadreshtech.materialexample.R;
public class VideoViews extends AppCompatActivity {
ProgressBar progressBar = null;
VideoView videoView1 = null;
VideoView video_views2 = null;
String videoUrl = "http://www.androidbegin.com/tutorial/AndroidCommercial.3gp";
Context context = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_views);
context = null;
videoView1 = (VideoView) findViewById(R.id.video_view1);
video_views2 = (VideoView) findViewById(R.id.video_views2);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
Uri videoUri = Uri.parse(videoUrl);
videoView1.setVideoURI(videoUri);
video_views2.setVideoURI(videoUri);
videoView1.start();
video_views2.start();
progressBar.setVisibility(View.VISIBLE);
videoView1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
#Override
public void onVideoSizeChanged(MediaPlayer mp, int arg1,
int arg2) {
// TODO Auto-generated method stub
progressBar.setVisibility(View.GONE);
mp.start();
}
});
}
});
video_views2.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
#Override
public void onVideoSizeChanged(MediaPlayer mp, int arg1,
int arg2) {
// TODO Auto-generated method stub
progressBar.setVisibility(View.GONE);
mp.start();
}
});
}
});
}
}
also don miss to add tow VideoView and one ProgressBar in layout

Categories

Resources