How to play video in android after Button click - android

I am trying to design simple code in android for play video after a button click but i am getting unknown problem. My present project files below. I am clueless right now. Please point out a thing or solve this problem. Thanks again!
public class VideoActivity extends Activity {
VideoView video;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play = (Button)findViewById(R.id.button1);
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
myVideoView.setVideoPath("/mnt/usb_storage/vivek.3gp");
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();
}
});
}
}

btnPlay.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
try
{
videoView.setVideoPath(videoPath);
videoView.setMediaController(new MediaController(Applicaion Context or YourActivity.this));
videoView.start();
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Error in btnInfo Click -->"+e.toString());
}
}
});
//BtnPlay

Related

playing mp3 file in second activity after button is clicked

hello I am very new to android programming.I want Button in first activity and when it is clicked second activity will open and plays mp3 file from sd card i have mention path but mp3 file not plays after running project.I am posting my player.java
This is my class
public class player extends AppCompatActivity {
Button btPv,btplay,btStop,btPause;
SeekBar sb;
MediaPlayer mp3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
;
btplay=(Button)findViewById(R.id.btplay);
btStop=(Button)findViewById(R.id.btstop);
btPause=(Button)findViewById(R.id.btpause);
mp3=new MediaPlayer();
try {
mp3.setDataSource("sdcard/musicblee/Over_the_horizon.mp3");
mp3.prepare();
} catch (IOException e) {
e.printStackTrace();
}
btplay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp3.start();
}
});
btStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
mp3.stop();
mp3.reset();
mp3.setDataSource("sdcard/musicblee/Over_the_horizon.mp3");
mp3.prepare();
} catch (Exception e) {
e.printStackTrace();
}
}
});
btPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
mp3.pause();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
refer to this link use mp3.setDataSource() like that
mp3 = MediaPlayer.create(this,Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/musicblee/Over_the_horizon.mp3"));
to set mp3 source.
And Also add this permission to your Android Manifest file :
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

MediaPlayer does not work properly

I'm using MediaPlayer for play a click sound when user clicks on a button. Sometimes the sound will play fine but other times it is too slow. For example first click is fine but second click is too slow.
Here is my code:
private MediaPlayer mClickSound;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
mClickSound = MediaPlayer.create(this, R.raw.click);
}
#Override
public void onClick(View view) {
try {
if (mClickSound.isPlaying()) {
mClickSound.stop();
mClickSound.release();
mClickSound = MediaPlayer.create(this, R.raw.click);
}
mClickSound.start();
} catch (Exception e) {
e.printStackTrace();
}
}
Try this:
mClickSound.reset();
AssetFileDescriptor afd = context.getResources().openRawResourceFd(R.raw.click);
if (afd == null) return;
mClickSound.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mClickSound.start();
afd.close();
setDataSource is taken from here:
https://stackoverflow.com/a/20111291/6159609
The reset method is supposed to be faster.
Please try below code working fine for me...
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
Button btn;
MediaPlayer mClickSound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
mClickSound = MediaPlayer.create(this, R.raw.click);
btn.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (mClickSound.isPlaying()) {
mClickSound.reset();
}
else {
mClickSound = MediaPlayer.create(this, R.raw.click);
mClickSound.start();
}
}
}

Android : Reset audio

I've some problem on implementing media player. I've no idea on how to reset the audio. What should I implement to make the audio stop and play again when play button is clicked. Please let me know. Thank you.
Here's the code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnPlay = (Button) findViewById(R.id.btnPlay);
Button btnPause = (Button) findViewById(R.id.btnPause);
Button btnStop = (Button) findViewById(R.id.btnStop);
final MediaPlayer sound = MediaPlayer.create(MainActivity.this, R.raw.doa);
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
sound.start();
Toast.makeText(getBaseContext(), "Play Audio", Toast.LENGTH_SHORT).show();
}
});
btnPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sound.pause();
Toast.makeText(getBaseContext(), "Pause Audio", Toast.LENGTH_SHORT).show();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sound.reset();
if (null!=sound){
sound.stop();
sound.reset();
sound.release();
sound=null;
}
}
});
}
}
When you stop the media, you need to prepare it again before you start it.
mediaPlayer.prepare();
mediaPlayer.start();
You can see the state diagram of MediaPlayer here at https://developer.android.com/reference/android/media/MediaPlayer.html
Media Recorder class does not support to pause and resume , see second link class overview try to use stop and restart..
In a new API level 24 it has given an methord for pause Link : https://developer.android.com/reference/android/media/MediaRecorder.html#pause() On Resume Link : https://developer.android.com/reference/android/media/MediaRecorder.html#resume()
Try This code for stop :
if (null != sound) {
sound.stop();
sound.reset();
sound.release();
sound= null;
}

why video view rotate automatically when using front camera?

I did not understand this problem, when i record a video through back camera then video view showing actually view which should showing in the video View but when i use front camera then its rotate to opposite. i am using one class for both then why i am getting this type behavior of video view?
video view code
public class VideoPreviewActivity extends Activity {
VideoView VideoPreview;
String videoURI ="VIDEO";
Button back;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_preview_screen);
VideoPreview = (VideoView) findViewById(R.id.video_preview);
back = (Button) findViewById(R.id.back);
Bundle bundle = getIntent().getExtras();
videoURI = bundle.getString("VIDEO");
PlayVideo();
back.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
Intent i = new Intent(VideoPreviewActivity.this, PicRecordShareActivity.class);
startActivity(i);
}
});
}
private void PlayVideo()
{
try
{
getWindow().setFormat(PixelFormat.TRANSLUCENT);
MediaController mediaController = new MediaController(VideoPreviewActivity.this);
mediaController.setAnchorView(VideoPreview);
// Toast.makeText(getApplicationContext(), "Video:\t"+videoIndex, Toast.LENGTH_LONG).show();
Uri video = Uri.parse(videoURI);
VideoPreview.setMediaController(mediaController);
VideoPreview.setVideoURI(video);
VideoPreview.requestFocus();
VideoPreview.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
public void onPrepared(MediaPlayer mp)
{
VideoPreview.start();
}
});
}
catch(Exception e)
{
System.out.println("Video Play Error :"+e.toString());
finish();
}
}
}
Thank You

Radio streaming android

Hi I want to implement a radio in my application...I wrote this code, it worked in the emulator YouWave but il dosen't worked in a SmartPhone and another tablet and I don't know why..can you help me please?
This is the code of the radio :
public class radio extends Activity {
MediaPlayer media;
Button buttonplay;
Button buttonStopRecord;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radio);
buttonStopRecord = (Button) findViewById(R.id.Stop);
buttonStopRecord.getBackground().setAlpha(150);
buttonStopRecord.setEnabled(false);
buttonStopRecord.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view) {
if ( media.isPlaying() ) {
media.stop();
}
}
});
buttonplay = (Button) findViewById(R.id.play);
buttonplay.getBackground().setAlpha(150);
buttonplay.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view) {
try
{
media = new MediaPlayer();
media.setAudioStreamType(AudioManager.USE_DEFAULT_STREAM_TYPE);
media.setDataSource("http://indiespectrum.com:9000");
media.prepare();
media.start();
if ( media.isPlaying()) {
buttonplay.setEnabled(false);
buttonStopRecord.setEnabled(true);
}
}
catch(Exception e)
{
//Getting Exception
}
}
});
Button accueil=(Button)findViewById(R.id.accueilr);
accueil.getBackground().setAlpha(200);
accueil.getBackground().setAlpha(150);
accueil.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view) {
try{
if ( media.isPlaying() ) {
media.stop();
}
}catch (Exception e)
{
Intent i=new Intent(radio.this,main.class);
startActivity(i);
}
Intent i=new Intent(radio.this,main.class);
startActivity(i);
}
});
}
}
From what I know this code should work on every device with Api higher than 8.
media = new MediaPlayer();
media.setAudioStreamType(AudioManager.STREAM_MUSIC);
media.setDataSource("http://indiespectrum.com:9000");
media.prepareAsync();
media.seOnPreparedListener(this);
And inside the OnPrepared method put media.start(). This worked for me.

Categories

Resources