Play audio when splash screen start - java

there is a method to play a music when the splash screen is run?
this is my splash screen code:
// Splash screen timer
private static int SPLASH_TIME_OUT= 2000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// This method will be executed once the timer is over
// Start main activity
Intent i = new Intent(SplashScreen.this, MyActivity.class);
startActivity(i);
// close activity
finish();
}
}, SPLASH_TIME_OUT);
}
I created the raw folder in res / raw and put in my song, how can I play it when the run splash screen?

Simply you can use,
myMediaPlayer = MediaPlayer.create(this, R.raw.loveme);
if (myMediaPlayer != null) {
myMediaPlayer.start();
} else {
myMediaPlayer.reset();
try {
myMediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myMediaPlayer.start();
}
}

MediaPlayer mPlayer = MediaPlayer.create(xxx.this, R.raw.xxx);

Related

Android MediaPlayer error

E/MediaPlayer: EventHandler handleMessage thread id is 1
E/MediaPlayer: EventHandler handleMessage thread id is 1
E/MediaPlayer: currentThread is 1, handleMessage mTimeProvider hashcode is 1112571032, mTimeProvider is android.media.MediaPlayer$TimeProvider#42507c98, msg is { when=-19ms what=7 target=android.media.MediaPlayer$EventHandler }
I get the above error when trying to play audio, any ideas why I get this error, the same code works in some places, I'm calling the static function from a fragment?
Global.playAudio("sounds/add_comment.mp3",context);
public static void playAudio(String aud, Context context) {
final MediaPlayer mp;
try {
AssetFileDescriptor fileDescriptor =
context.getAssets().openFd(aud);
mp = new MediaPlayer();
mp.setDataSource(fileDescriptor.getFileDescriptor(),
fileDescriptor.getStartOffset(),
fileDescriptor.getLength());
fileDescriptor.close();
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Let me know if this helped -
public class MainActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener,AudioManager.OnAudioFocusChangeListener{
MediaPlayer mp;
AudioManager mAudioManager ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int result = mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
//got audio focus
playAudio("bell.mp3",this);
}
}
private void playAudio(String aud, Context context) {
try {
AssetFileDescriptor fileDescriptor =
context.getAssets().openFd(aud);
mp = new MediaPlayer();
mp.setDataSource(fileDescriptor.getFileDescriptor(),
fileDescriptor.getStartOffset(),
fileDescriptor.getLength());
fileDescriptor.close();
mp.prepareAsync();
mp.setOnPreparedListener(this);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp.start();
}
#Override
public void onAudioFocusChange(int i) {
}
}
Here, I am first trying to get the audio focus and then letting the mediaplayer do the task asynchronously.

View video in full screen mode

can someone help me with this. i'm doing a project in which user can watch video and i use surface view, and media player.. Now, i want to have a small screen that plays a video and continues playing on the big screen.. what i have in my code now, is i have two different surface view on the same activity when i click play button it plays video but when i click full screen it does'nt continue playing, its just play the video from the start.
public class MainActivity extends Activity implements OnClickListener ,
SurfaceHolder.Callback{
//For surface
MediaPlayer mediaPlayer, mediaPlayer_fullscreen;
SurfaceView surfaceView, surfaceView_fullscreen;
SurfaceHolder surfaceHolder, surfaceholder_fullscreen;
boolean pausing = false;
boolean pausing_2 = false;
boolean reset = false;
public static int media_length;
//FULL SCREEN
Button play, pause;
ImageButton mExit, resume;
public static int mediaPlayer_length;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//For Surface
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setFixedSize(176, 144);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer = new MediaPlayer();
//FULLSCREEN
//For Surface
surfaceView_fullscreen =
(SurfaceView)findViewById(R.id.surfaceView2);
surfaceholder_fullscreen = surfaceView_fullscreen.getHolder();
surfaceholder_fullscreen.addCallback(this);
surfaceholder_fullscreen.setFixedSize(300, 300);
surfaceholder_fullscreen.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer_fullscreen = new MediaPlayer();
media_length = mediaPlayer.getCurrentPosition();
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.play:
playvideo();
break;
case R.id.view_fullscreen:
mediaPlayer.release();
linearlayout_head.setVisibility(View.GONE);
linearlayout_fullscreen.setVisibility(View.VISIBLE);
playvideo_fullscreen();
break;
}
public void playvideo() {
path_path = EditText_path.getText().toString();
pausing = false;
//reset = false;
if(mediaPlayer.isPlaying()){
mediaPlayer.reset();
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(surfaceHolder);
try {
mediaPlayer.setDataSource(getDataSource(path_path));
Log.d("LOGPLAY", path_path);
mediaPlayer.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
//mediaPlayer.pause();
//media_length = mediaPlayer.getCurrentPosition();
}
public void playvideo_fullscreen() {
pausing_2 = false;
//reset = false;
if(mediaPlayer_fullscreen.isPlaying()){
mediaPlayer_fullscreen.reset();
//mediaPlayer_fullscreen.seekTo(media_length);
}
mediaPlayer_fullscreen.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer_fullscreen.setDisplay(surfaceholder_fullscreen);
try {
mediaPlayer_fullscreen.setDataSource(getDataSource(path_path));
Log.d("LOGPLAYFullscreen", path_path);
mediaPlayer_fullscreen.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//mediaPlayer_fullscreen.start();
mediaPlayer_fullscreen.seekTo(media_length);
}
can someone help me with this please. Thank you.
When you switch between two views media player will not resume where you are paused. So you must do it manual. You will get the current time of video using
MediaPlayer mp=new MediaPlayer();
int currentTime=mp.getCurrentPosition();
After switching the view just seek the video to the 'currentTime'.
mp.seekTo(currentTime);

Mediaplayer and finish() method

I was just testing mediaplayer in android, i started a stream in the onCreate method and I have a button that calls the finish() method. After clicking the button I can still hear the stream playing even though the activity is close, I am wondering if this is a leak of sorts and i will have to stop the player first before calling the finish() method, or if finish() method actually does not full kill the app to free up resources. Thank you for reading
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button add_function,exit_btn;
add_function = (Button) findViewById(R.id.view_chat);
exit_btn = (Button) findViewById(R.id.exit_btn);
MediaPlayer mp = new MediaPlayer();
String URL_OF_FILE = "http://stream.radiosai.net:8002/";
try {
mp.setDataSource(URL_OF_FILE);
mp.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
exit_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}// EOF ONCREATE
Calling finish() does not kill the application, it just destroys the current Activity that you're finishing. Although I would think finishing the Activity would stop the MediaPlayer, what you should probably do in this case is override onDestroy(), and release your MediaPlayer object there. For instance:
#Override
public void onDestroy() {
if(mediaPlayer != null) mediaPlayer.release();
super.onDestroy();
}

refresh content of Activity or add refresh button

As I can refresh the content of an activity?, for example, I have a menu and a button send me an application content that displays information online, but to go back and return again, the information is not updated.
This is my Activity.
public class Bovalpo extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bovalpo);
Button buttonExit = (Button)this.findViewById(R.id.cerrar);
buttonExit.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
System.exit(0);
}
}
);
TextView myListView = (TextView) findViewById(R.id.tv);
try {
myListView.setText(getPage());
if(getPage().contains("Abierto")){
myListView.setTextColor(Color.parseColor("#008000"));
}else{
myListView.setTextColor(Color.parseColor("#FF0000"));
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String getPage() throws MalformedURLException, IOException {
HttpURLConnection con = (HttpURLConnection) new URL("http://www.bovalpo.com/cgi-local/xml_bcv.pl?URL=1").openConnection();
con.connect();
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
return inputStreamToString(con.getInputStream());
} else {
return null;
}
}
private String inputStreamToString(InputStream in) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append("Mercado: " + line + "\n");
}
bufferedReader.close();
return stringBuilder.toString();
}
public void lanzar(View view){
Intent i = new Intent(this, xml7009.class);
startActivity(i);
}
public void lanzar3(View view){
Intent i = new Intent(this, tabla7009.class);
startActivity(i);
}
public void lanzar4(View view){
Intent i = new Intent(this, xml6503.class);
startActivity(i);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
}
put your code here
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// make your work to data bind
}
The code that fetches your data and sets list view color should be put in onResume() instead of onCreate if you want it to run each time your Activity is shown.
Simply you can put your update code in the onResume() method of the activity. OnResume() method will be called when ever you return from the other activity.
But onResume() method is often called when your activity is resume for example. If you open and dismiss the dialog then your activity will be Resume. SO if you are calling some network call in onResume then it will consume the process and Network speed.
The alternate solution is use startActivityForResult() method to receive the result from the next activity and bases of the activity result you can call your web API or any work. You can get the result of the next activity in onActivityResult() method.
But before using the startActivityForResult method ensure that the next activity will set the result by calling setResult() method.
If you want to update your data every time you came to activity, you need to set your updated values in onResume
like below
#Override
protected void onResume() {
super.onResume();
try {
myListView.setText(getPage());
if(getPage().contains("Abierto")){
myListView.setTextColor(Color.parseColor("#008000"));
}else{
myListView.setTextColor(Color.parseColor("#FF0000"));
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

In an android soundboard how to make one sound stop playing after another one is clicked on?

How do I modify this so that if one sound is playing already and another is clicked on, the previous stops playing and the newly selected starts? Thanks everyone for their help in advance. (this is not all the code just the most important)
public class newBoard extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Toast.makeText(this, "Thank you for using this App.", Toast.LENGTH_LONG).show();
// ads - load request to display
AdView layout = (AdView)this.findViewById(R.id.adView);
// ads - load display with an ad
AdRequest adRequest = new AdRequest();
adRequest.setTesting(true);
layout.loadAd(adRequest);
// import sound files
final MediaPlayer sound01 = MediaPlayer.create(this, R.raw.sound01);
final MediaPlayer sound02 = MediaPlayer.create(this, R.raw.sound02);
final MediaPlayer sound03 = MediaPlayer.create(this, R.raw.sound03);
final MediaPlayer sound04 = MediaPlayer.create(this, R.raw.sound04);
final MediaPlayer sound05 = MediaPlayer.create(this, R.raw.sound05);
final MediaPlayer sound06 = MediaPlayer.create(this, R.raw.sound06);
final MediaPlayer sound07 = MediaPlayer.create(this, R.raw.sound07);
final MediaPlayer sound08 = MediaPlayer.create(this, R.raw.sound08);
final MediaPlayer sound09 = MediaPlayer.create(this, R.raw.sound09);
final MediaPlayer sound10 = MediaPlayer.create(this, R.raw.sound10);
final MediaPlayer sound11 = MediaPlayer.create(this, R.raw.sound11);
final MediaPlayer sound12 = MediaPlayer.create(this, R.raw.sound12);
final MediaPlayer sound13 = MediaPlayer.create(this, R.raw.sound13);
final MediaPlayer sound14 = MediaPlayer.create(this, R.raw.sound14);
final MediaPlayer sound15 = MediaPlayer.create(this, R.raw.sound15);
final MediaPlayer sound16 = MediaPlayer.create(this, R.raw.sound16);
final MediaPlayer sound17 = MediaPlayer.create(this, R.raw.sound17);
final MediaPlayer sound18 = MediaPlayer.create(this, R.raw.sound18);
final MediaPlayer sound19 = MediaPlayer.create(this, R.raw.sound19);
final MediaPlayer sound20 = MediaPlayer.create(this, R.raw.sound20);
final MediaPlayer sound21 = MediaPlayer.create(this, R.raw.sound21);
final MediaPlayer sound22 = MediaPlayer.create(this, R.raw.sound22);
final MediaPlayer sound23 = MediaPlayer.create(this, R.raw.sound23);
final MediaPlayer sound24 = MediaPlayer.create(this, R.raw.sound24);
final MediaPlayer sound25 = MediaPlayer.create(this, R.raw.sound25);
// play sound files on clicks
Button s01 = (Button) findViewById(R.id.button01);
s01.setText(this.getString(R.string.quote01));
s01.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
sound01.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sound01.start();
}
});
registerForContextMenu(s01);
Button s02 = (Button) findViewById(R.id.button02);
s02.setText(this.getString(R.string.quote02));
s02.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
sound02.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sound02.start();
}
});
registerForContextMenu(s02);
Button s03 = (Button) findViewById(R.id.button03);
s03.setText(this.getString(R.string.quote03));
s03.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
sound03.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sound03.start();
}
});
registerForContextMenu(s03);
Button s04 = (Button) findViewById(R.id.button04);
s04.setText(this.getString(R.string.quote04));
s04.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
sound04.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sound04.start();
}
});
registerForContextMenu(s04);
Button s05 = (Button) findViewById(R.id.button05);
s05.setText(this.getString(R.string.quote05));
s05.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
sound05.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sound05.start();
}
});
registerForContextMenu(s05);
Button s06 = (Button) findViewById(R.id.button06);
s06.setText(this.getString(R.string.quote06));
s06.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
sound06.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sound06.start();
}
});
registerForContextMenu(s06);
Use a object member variable to hold on to your currently playing sound so that you can call stop() and don't forget to null check it. Don't forget to release() your MediaPlayer object once you leave your activity.
The following is probably a better design: move all your audio files to your assets directory and put the file name in the button's tag, use the same click event handler to get the state of the one MediaPlayer object, stop it if its currently playing, set the new file to play from the button's tag and play the file. This will reduce your duplicate code significantly.
Use a single MediaPlayer instance. Yours is going to fail on most devices as it is anyway for allocating to many instances of MediaPlayer. Also, repetitious code is bad. Bad bad:
public class NewBoard extends Activity {
private MediaPlayer player;
private Resources res;
private int buttonIds = { R.id.button01, R.id.button02, R.id.button03,
R.id.button04, R.id.button05, R.id.button06,
R.id.button07, R.id.button08, R.id.button09,
R.id.button10, R.id.button11, R.id.button12,
R.id.button13, R.id.button14, R.id.button15,
R.id.button16, R.id.button16, R.id.button17,
R.id.button18, R.id.button19, R.id.button20,
R.id.button21, R.id.button22, R.id.button23,
R.id.button24, R.id.button25 };
private int soundIds = { R.raw.sound01, R.raw.sound02, R.raw.sound03,
R.raw.sound04, R.raw.sound05, R.raw.sound06,
R.raw.sound07, R.raw.sound08, R.raw.sound09,
R.raw.sound10, R.raw.sound11, R.raw.sound12,
R.raw.sound13, R.raw.sound14, R.raw.sound15,
R.raw.sound16, R.raw.sound16, R.raw.sound17,
R.raw.sound18, R.raw.sound19, R.raw.sound20,
R.raw.sound21, R.raw.sound22, R.raw.sound23,
R.raw.sound24, R.raw.sound25 };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
//Kill this with fire -- just an unnecessary user annoyance.
Toast.makeText(this,
"Thank you for using this App.", Toast.LENGTH_LONG).show();
AdView layout = (AdView)findViewById(R.id.adView);
AdRequest adRequest = new AdRequest();
adRequest.setTesting(true);
layout.loadAd(adRequest);
player = new MediaPlayer();
res = getResources();
for(int i = 0, n = buttonIds.length(); i < n; i++) {
Button button = (Button)findViewById(buttonIds[i]);
button.setOnClickListener(new SoundClickListener(soundIds[i]));
}
}
private class SoundClickListener implements OnClickListener {
private int id;
public SoundClickListener(int soundId) {
id = soundId;
}
public void onClick(View v) {
player.reset();
player.setDataSource(
res.openRawResourceFd(id).getFileDescriptor());
player.prepare();
player.start();
}
}
}
Something like this. May or may not compile as is. Also, as commented, kill the Toast with fire -- that's just annoying.

Categories