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();
}
Related
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.
I have an App that is receiving a video file from another App that is working as a Server. While the App is saving the file received on the socket, the video stream starts playing the file (which is under construction). In the code sample, after I press the btnStream, I press the btnPlay and App runs successfully. However, if the playing rate is greater than the download rate, an error will occur. I want to avoid this case. So I need to have a listener on the Video Playing that will pause the videoview when it predicts that this error will occur. I know a solution where if I know the video size, I can counter the bytes received and monitor how many seconds have been buffered and see if the videoview should pause or not. However, is it possible to do it without knowing the video file size? Or having two threads that depends on each other? Thanks.
Note: the VideoView used is a custom one where it can play FileDescriptor.
btnStream.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String s = etURL.getText().toString();
String ip = "10.0.0.24";
int port = 7878;
mct= new VideoDownloadTask(ip,port);
mct.execute();
}});
final MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(mVideoView);
Button btnPlay = (Button) findViewById(R.id.button2);
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
mVideoView.setVideoFD((new FileInputStream(new File("/sdcard/tempVideo.mp4")).getFD()));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mVideoView.seekTo(0);
mVideoView.start();
}
});
}
public class VideoDownloadTask extends AsyncTask<Void, Void, Void> {
String dstAddress;
int dstPort;
String response = "";
Socket socket=null;
VideoDownloadTask(String addr, int port){
dstAddress = addr;
dstPort = port;
}
#Override
protected Void doInBackground(Void... arg0) {
try {
socket = new Socket(InetAddress.getByName(dstAddress), dstPort);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
if(socket!=null)socket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
File f = new File("/sdcard/tempVideo.mp4");
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DataInputStream in=null;
try {
in = new DataInputStream (socket.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FileOutputStream videoFile = null;
try {
videoFile = new FileOutputStream(f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int len;
byte buffer[] = new byte[8192];
try {
while((len = in.read(buffer)) != -1) {
videoFile.write(buffer, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
videoFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
Toast.makeText(getApplicationContext(), "Done Downloading File",
Toast.LENGTH_LONG).show();
super.onPostExecute(result);
}
}
}
I applied a simple solution that resolved the problem. I am sharing it if anyone is having the same problem. The solution was simply to add an error listener to the videoView that will block the error popups and pauses the video.
mVideoView.setOnErrorListener(new OnErrorListener(){
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
statusText.setText("ERROR PLAYING VIDEO");
mVideoView.pause();
return true;
}
});
pDialog = new ProgressDialog(PlayVideoActivity.this);
pDialog.setTitle("Gajacharitra");
pDialog.setMessage("Buffering video...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
try {
// Start the MediaController
mediacontroller.setAnchorView(mVideoView);
// Get the URL from String VideoURL
Uri video = Uri.parse(mVideoURL);
mVideoView.setMediaController(mediacontroller);
mVideoView.setVideoURI(video);
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
pDialog.dismiss();
mVideoView.start();
}
});
mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
mVideoView.pause();
pDialog.dismiss();
Toast.makeText(PlayVideoActivity.this, "Can't play this video.", Toast.LENGTH_LONG).show();
finish();
return true;
}
});
} catch (Exception e) {
/*Log.e("Error", e.getMessage());
e.printStackTrace();*/
pDialog.dismiss();
Toast.makeText(PlayVideoActivity.this, "Can't play this video.", Toast.LENGTH_LONG).show();
finish();
}
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);
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.
I have 2 buttons that both play an mp3. When the first song is playing I want a click of the second song to stop the first song, and start the song that was clicked. also if the first button is pressed twice the second press resets the song.
basically I don't want the songs to play on top of each other when multiple buttons are pressed.
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.placeyouknow:
String url = "http://www.katastro.com/audio/facts/01%20That%20Place%20You%20Know.mp3";
mp = new MediaPlayer();
try {
mp.setDataSource(url);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
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();
}
case R.id.fallen:
String url1 = "http://www.katastro.com/audio/fallen/01%20Fallen.mp3";
mp = new MediaPlayer();
try {
mp.setDataSource(url1);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
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();
Seems like you need to implement a state machine to manage your player (start-stop-restart) instead of creating a new player every time a button is pressed
Here is a sample sound helper class I use for simple apps. I use it to play at most one resource at a time within my app, but you could modify it to play audio from some url instead.
public class Sound
{
private static MediaPlayer mp = null;
/** Stop old sound and start new one */
public static void play(Context context, int resource)
{
stop(context);
mp = MediaPlayer.create(context, resource);
mp.setLooping(false);
mp.start();
}
/** Stop the music */
public static void stop(Context context)
{
if (mp != null)
{
mp.stop();
mp.release();
mp = null;
}
}
}
Whoooaaaaa, losing handles on MediaPlayers left and right.
First of all, for this particular activity, you should only put
mp = new MediaPlayer();
in the onCreate() method of your activity. Delete the two you call in the onClick() method and move it to the onCreate() method.
From there, all you have to do is include a call to mp.reset() right before you call mp.setDataSource() and it should work just like you want it to!
Cheers