I've got a very basic Android application that I've been writing in Android Studio. Firstly the purpose of the app is when launched to play a video full screen on loop until the screen is touched and the application exits. It's essentially being used as a screensaver for a digital signage installation.
I've got the below code which handles playing the video but cannot seem to get any code for exiting on touch to work. I am a bit of a novice when it comes to Android development.
package halifax.screensaver.screensaver;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.VideoView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView videoView = findViewById(R.id.videoView);
Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.hab2);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
}
}
}
Any help would be greatly appreciated
have you tried setting an onclick on your videoview and finishing the activity?
videoView.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
finish();
}
});
Set an OnClickListener and call finish() to exit the app and display the screen that was open before your activity was launched. If you want to go to a previously opened activity in your own app call onBackPressed(). Or if you want the user to get the home screen/launcher call the intent:
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
Try this
Use setOnTouchListener for touch event of videoview
videoView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
finish();
return false;
}
});
Related
I have used MUSIC_PLAYER in my application by starting like this:
Intent myint = new Intent("android.intent.action.MUSIC_PLAYER");
startActivity(myint);
And I want to pause this player. How do I do that?
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread mtr = new Thread(){
public void run()
{
try {
Intent myint = new Intent("android.intent.action.MUSIC_PLAYER");
startActivity(myint);
sleep(50000);
// Here I want to pause my application after those 50 seconds.
}
catch(InterruptedException e){
e.printStackTrace();
}
finally{
Intent myint = new Intent("nextscreen");
startActivity(myint);
}
}
};
mtr.start();
}
}
In Samsung devices, you can control the music player with these intents:
ICS:
"com.android.music.musicservicecommand.play"
"com.android.music.musicservicecommand.pause"
"com.android.music.musicservicecommand.next"
...
JB:
"com.sec.android.app.music.musicservicecommand.play"
"com.sec.android.app.music.musicservicecommand.pause"
"com.sec.android.app.music.musicservicecommand.next"
...
context.sendBroadcast(new Intent("com.sec.android.app.music.musicservicecommand.pause"));
Another solution is requestAudioFocus in AudioManager (do not forget to release it with abandonAudioFocus). For example:
...
((AudioManager)getSystemService(Context.AUDIO_SERVICE)).requestAudioFocus(listener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
...
...
((AudioManager)getSystemService(Context.AUDIO_SERVICE)).abandonAudioFocus(listener);
...
You can't do that. Pausing the music player is an internal action inside the application available to the user exclusively, so it cannot be accessed/called from outside of the application.
You can, however, mute the music audio stream using the AudioManager class by calling setStreamMute(AudioManager.STREAM_MUSIC, true).
Code sample:
AudioManager am = (AudioManager)MainActivity.this.getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_MUSIC, true);
See answer here, you can send broadcast to pause or stop the default music player very easy.
I have no problem getting some audio files in eclipse:java to play properly when buttons on the android's screen are pressed and all of the audio quality sounds great. However, the program I am trying to make is a beat maker studio and you can only press the buttons so fast. There is a slight delay in when you can press a button to fire an audio sample after pressing that same button immediately before(about .5 seconds). Obviously this presents problems with a music app when being able to press a button at the desired time is pretty important. Any advice on how I can get my application to allow faster button presses?
package studio.music;
import android.app.Activity;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class StudioActivity extends Activity implements OnClickListener{
MediaPlayer mp1,mp2;
Button button_one,button_two;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) { //The activity is being Created Create any threads/streams here
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp1 = MediaPlayer.create(this, R.raw.bassfifteen);
mp2 = MediaPlayer.create(this, R.raw.snarenine);
button_one = (Button) findViewById(R.id.buttonOne);
button_one.setOnClickListener(this);
button_two = (Button) findViewById(R.id.buttonTwo);
button_two.setOnClickListener(this);
}
#Override
protected void onStart(){ //The activity is about to become visible
super.onStart();
//TODO set state for program start
}
#Override
protected void onResume(){ //The activity has become visible(it is now "resumed")
super.onResume();
//TODO set state for program resume
}
#Override
protected void onPause(){ // Another activity is taking focus (this activity will be "paused")
super.onPause();
//TODO set state for program hang
}
#Override
protected void onStop(){ // This activity is no longer visibled (it is "stopped")
super.onStop();
//TODO set state for program drop back
}
#Override
protected void onDestroy(){ // The Activity is about too be destoryed (exiting) Destroy all threads and streams here
super.onDestroy();
//TODO set state for program exit
}
#Override
public void onClick(View v) {
//Where the audio is fired
if(v == button_one){ mp1.start();}
if(v == button_two){ mp2.start();}
}
}
Maybe you should go for SoundPool. Because soundpool is considerably a light weight object when comapred to MediaPlayer. here is a comparision between the two.
http://www.stealthcopter.com/blog/2010/08/android-soundpool-vs-mediaplayer-focus-on-soundboards-and-memory-problems/
And as you can see SoundPool is desgined for small files.
Here is an other link which states SoundPool does the job simple and good..
https://stackoverflow.com/a/5339437/603744
Use SoundPool which provides a low-latency playback.
A ready to use sample is available at Android Snippets. It would be easy for you to adapt it to your requirements.
I am trying to create my first android application and what I'm trying to accomplish here is to play a sound and then stop it via the same button.
It kind of works as it plays the sound when I click it and stops when I click it again but will not play when I click it the third time to start the sound again.
I'm eventually going to have a few sounds in here and so would like to know if how my project is laid out correctly? Can I save some time anywhere? Have I got something the wrong way round?
package test.soundy.com;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class TestActivity extends Activity {
private MediaPlayer sound;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sound = MediaPlayer.create(Test.this, R.raw.sound1);
Button test = (Button)this.findViewById(R.id.button1);
test.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (sound.isPlaying()) {
sound.stop();
} else {
sound.start();
}
}
});
}
}
Any help would be much appreciated, thanks.
WHEN START/PAUSE:
if(sound.isPlaying()){
sound.pause();
}else{
sound.start();
}
WHEN START/STOP:
if(sound.isPlaying()) {
sound.stop();
} else {
sound.reset();
sound.setDataSource(yourURL); //or InputStream etc.
sound.prepare();
sound.start();
}
Also you can use sound.seekTo(time) to skip to a position.
Remember when you want to play a new sound(or restart) you should first reset, setDataSource, prepare and then start it.
EDIT: get the FileDescripter
AssetManager assetManager=Context.getAssets();
AssetFileDescriptor fileDescriptor = assetManager.openFd("a2.mp3");
mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor());
EDIT: I haven't found a way to turn raw file into filedescriptor so I use the static method of MediaPlayer
MediaPlayer mediaPlayer = MediaPlayer.create(Activity.this,R.raw.a1);
mediaPlayer.setOnCompletionListener(new musicCompletionListener());
mediaPlayer.start();
private class musicCompletionListener implements OnCompletionListener {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
}
}
alse release the mediaplayer when stop it.
Have you tried resetting the MP?
if (sound.isPlaying()) {
sound.stop();
} else {
sound.reset();
sound.prepare();
sound.start();
}
Edited...
The full state diagram is here: http://developer.android.com/reference/android/media/MediaPlayer.html
Below is my code and I'm gonna integrate with the application. My problem is when the button clicked video recorder invoked and video recoded smoothly but I want to get the response from the camera when the video recording is done every time.
public class AndroidVideoActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnVideoRecorder = (Button)findViewById(R.id.buttonClick);
btnVideoRecorder.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("android.media.action.VIDEO_CAMERA");
startActivity(intent);
}
});
}
}
Can someone please guide me.
Thanks for your time in advance.
Try changing
startActivity(intent);
to
startActivityForResult(intent);
Then when the video is done recordng, it will return to your app and you will be able to do something with the video
This is a critical issue pertaining to an application we did. The code snippet attached here was working like a charm for the past several months. All of a sudden, it gives us the message in mediaplayer that video cannot be played.
Can anyone please suggest what need to be done, in order that this code will work for me by playing the video with no hassles.
Looking forward for your valuable comments, help and suggestions,
Please follow this link in order to view the code which was working perfectly -- http://pastebin.com/NVT8eBC0
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class videoact extends Activity implements OnPreparedListener{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MediaController mc = new MediaController(this);
VideoView vv = (VideoView)findViewById(R.id.VideoView01);
try
{
Uri ur = Uri.parse("http://youtube.com/get_video?video_id=ZmX7zLCrcAI&t=vjVQa1PpcFPmrj_j5y370BhPYfq3qHoWsFICYcBqEl4%3D&asv=&fmt=18");
vv.setVideoURI(ur);
vv.setMediaController(mc);
vv.requestFocus();
vv.start();
mc.show();
}
catch(Exception e)
{
System.out.print(e.getMessage() + "error");
}
}
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
}
}
You can't play YouTube videos like that. Use the Android YouTube Player Api or the IFrame API in a WebView.
Using the get_video endpoint is against youtube's terms of service.
https://www.youtube.com/static?template=terms
You agree not to access Content through any technology or means other than the video playback pages of the Service itself, the Embeddable Player, or other explicitly authorized means YouTube may designate.