android media player stops playing after some minutes - android

I have an issue with Media player always stopping randomly after some few mins, most times, when the phone sleeps and you touch it again, the video hangs and stops playing.
I have tried many solutions here on stackoverflow, none worked. I'm almost pulling my hair out !!!
I've tried this answer
Android MediaPlayer stops playing after some time
MediaPlayer randomly stops on Android 4.4 (19)
How to prevent mediaplayer to stop when screen goes off?
and many more answers, they all didn't work
This is my code below
public class MainActivity {
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp = MediaPlayer.create(this, R.raw.my_webm_video);
mp.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
playVideo();
}
#Override
public void onResume() {
super.onResume();
mp = MediaPlayer.create(this, R.raw.my_webm_video);
mp.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
playVideo();
}
public void playVideo(){
SurfaceView sv = (SurfaceView) findViewById(R.id.surfaceView);
SurfaceHolder holder = sv.getHolder();
holder.addCallback(new SurfaceHolder.Callback(){
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }
#Override
public void surfaceCreated(SurfaceHolder holder) {
mp.setDisplay(holder);
mp.start();
mp.setLooping(true);
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
}
}
This is my XML layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<SurfaceView
android:id="#+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>

ok, so i finally solved the problem. Incase any one else run into this issue
in onCreate i checked if the video was already playing, to make sure it isn't called to re-play each time.
int firstPlayed = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mp = MediaPlayer.create(this, R.raw.my_webm_video);
mp.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
if(!mp.isPlaying()) {
firstPlayed = 1;
playVideo();
}
}
I checked in onResume incase the user navigates out of the activity and goes back, Which makes the video screen turn black.
public void onResume() {
super.onResume();
if(!mp.isPlaying() && firstPlayed == 0) {
mp = MediaPlayer.create(this, R.raw.my_webm_video);
mp.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
playVideo();
}
firstPlayed = 0;
}
And then i noticed that i needed a firstPlayed check to be sure.

Related

Android: MediaPlayer not getting released as expected

So I have an application with a ListView that plays audio in one of the fragments. The issue is, that some users are reporting that the application stops playing audio after a certain amount of clicks. I know this is an issue that has to do with the MediaPlayer not being released correctly, but I cannot find a place where I am not releasing it. Hopefully you guys can help.
Here is where MediaPlayer is being used in MyAudioFragment.java. I am fairly certain that the issue is located in these lines of code.
MyAudioFragment.java
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(MainActivity.mediaPlayer != null && MainActivity.mediaPlayer.isPlaying()){
MainActivity.mediaPlayer.release();
MainActivity.mediaPlayer = new MediaPlayer();
}
MainActivity.mediaPlayer = MediaPlayer.create(getContext(), MainActivity.hashMap.get(adapterView.getItemAtPosition(i)));
MainActivity.mediaPlayer.start();
}
});
Here is where I deal with MediaPlayer in MainActivity.java
//...
public static MediaPlayer mediaPlayer;
//...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...
//Layout stuff
mediaPlayer = new MediaPlayer();
//Setting up fragment stuff
//...
}
#Override
public void onPause(){
super.onPause();
mediaPlayer.stop();
mediaPlayer.release();
}
#Override
public void onResume(){
super.onResume();
mediaPlayer = new MediaPlayer();
}
Edit: Could it possibly be that I am not releasing it "OnCompletion" ?
On onCompletion, you must do release();

Restart the audio from where it had stopped when the audio played after clicking a button finishes

I am playing an audio (mySound) as background music. On clicking a button, the background audio should stop and a different audio (mySound11) should start playing. After this audio gets done, the background music should automatically resume from where it had left. I have tried this code, but the background audio doesn't resume after mySound11 is over. Please help. Thanks in advance.
MainActivity.java
public class MainActivity extends ActionBarActivity {
MediaPlayer mySound;
MediaPlayer mySound11;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySound11 = MediaPlayer.create(this, R.raw.tiktik);
mySound = MediaPlayer.create(this, R.raw.jhakmaarke);
mySound.setLooping(true);
mySound.start();
}
public void playMusic (View view){
mySound.pause();
mySound11.start();
}
#Override
public void onResume() {
super.onResume();
mySound.start();
}
#Override
protected void onPause() {
super.onPause();
mySound.release();
mySound11.release();
finish();
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button Dabaa be"
android:onClick="playMusic"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp" />
I Solved your problem with the help of Thread.
I tested it working as you wished.
int mySound11Duration;
MediaPlayer mySound;
MediaPlayer mySound11;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySound11 = MediaPlayer.create(this, R.raw.tiktik);
mySound = MediaPlayer.create(this, R.raw.jhakmaarke);
mySound.setLooping(true);
mySound.start();
}
public void playMusic(View view) {
if (mySound.isPlaying()) { //check mysound is playing or not
mySound11Duration = mySound11.getDuration();
Thread thread=new Thread() {
#Override
public void run() {
try {
mySound.pause();
int mySoundPausePosition = mySound.getCurrentPosition();//get the time where the song is paused.
mySound11.start();
sleep(mySound11Duration);//sleep method causes the currently executing thread to sleep for specified number of time(miliseconds).
mySound.seekTo(mySoundPausePosition);
mySound.start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
}
}
}

MediaPlayer doesn't resume video after activity restart

I have a MediaPlayer object that uses a SurfaceHolder object as a surface. There is a button on top of the video that takes me out of the video to a website. When that happens, I pause the player with player.pause(). When I return from the website, I resume the player with player.start(). I know that the surface gets destroyed when the activity is not displayed anymore, and it gets recreated as soon as the activity is restarted. In my surfaceCreated(), I set the surface for the player again (since it no longer has a surface at that point), and then resume. However, the player simply restarts the video from the beginning.
I've tried commenting out the line that takes me to the website, just to see if pause/start works properly and resumes from last spot. It does. I'm not sure why this behaviour doesn't happen when I leave and re-enter the video activity though.
I also tried using the player.seekTo() call. There was no difference. In fact, when I disabled the button taking me to a site to just pausing the video, with the seekTo() call the video ALSO started from the beginning despite position being not 0.
The player object is the same all the way throughout.
Just because the surface is a new one on restart, it doesn't know or care of its contents, does it? The player should be managing that, right?
I'm out of ideas at this point. Can anyone please offer any tips?
UPDATE: So I threw together a quick app just to eliminate any other external factors. Here's the full code for the video class (other class is just an activity with a play button):
public class VideoPlayer extends Activity implements MediaPlayer.OnCompletionListener,
MediaPlayer.OnErrorListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnSeekCompleteListener, MediaPlayer.OnVideoSizeChangedListener,
SurfaceHolder.Callback {
private MediaPlayer player;
private SurfaceHolder mSurfaceHolder;
private SurfaceView mSurfaceView;
private Button leaveVideoButton;
private boolean isPaused = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_layout);
leaveVideoButton = (Button) findViewById(R.id.go_to_web);
leaveVideoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
pauseSteps();
startActivity(intent);
}
});
createPlayer();
createSurface();
}
private void createSurface() {
mSurfaceView = (SurfaceView) findViewById(R.id.surface);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
private void createPlayer() {
player = new MediaPlayer();
player.setOnCompletionListener(this);
player.setOnErrorListener(this);
player.setOnPreparedListener(this);
player.setOnVideoSizeChangedListener(this);
player.setOnSeekCompleteListener(this);
}
private void pauseSteps() {
if(player.isPlaying()) {
player.pause();
isPaused = true;
}
}
private void playSteps() {
if(isPaused) {
isPaused = false;
player.start();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if (player.isPlaying()) {
player.stop();
}
player.reset();
player.release();
player = null;
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
player.setDisplay(holder);
if (!isPaused) {
try {
// player.setDataSource(path);
AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.video);
if (afd == null) return;
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
player.prepare();
} catch (IOException e) {
e.printStackTrace();
}
} else {
playSteps();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
#Override
public void onCompletion(MediaPlayer mp) {
}
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return false;
}
#Override
public void onPrepared(MediaPlayer mp) {
player.start();
}
#Override
public void onSeekComplete(MediaPlayer mp) {
}
#Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
}
}
UPDATE 2: So I tried a different video and it resumed just fine from the same spot. This must be some encoding issue.

How can I get Uri of a media file in android?

I want to play a video with MediaPlayer in android, code is something like this:
MediaPlayer mMediaPlayer = new MediaPlayer()
mMediaPlayer.setDataSource(this.getContext, mUri, mHeaders)
I have a local file: /storage/emulated/0/Movies/test.mp4, how can I get the mUri of this file?
In fact, android.net.Uri have a method fromFile(File file), it returns a Uri like this: file:///storage/emulated/0/Movies/test.mp4. But this does work in setDataSource() method.
UPDATED:
I don't want to misleading someone in this problem.
In fact, Uri.parse("/storage/emulated/0/Movies/test.mp4") and Uri.parse("file:///storage/emulated/0/Movies/test.mp4") should be ok in my situation. But it is just my mp4 file's problem!
just change your file:/... to file:/// and it will work
Uri.parse(uri.toString().replace("file:/", "file:///"));
or simply if you have a file try
Uri.parse(String.valueOf(Uri.fromFile(myFile)));
Actually the thing is that the media player requires MRL followed by your path to file
EDIT
I have answered a question on how to initialise MediaPlayer using Uri here
Use the above MRL and initialise your player as suggested and enjoy your video :)
UPDATE:
I thought I should share part of my implementation so that it may help you out:
1st
Add below line to your manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
2nd
Make your activity look like
public class MyActivity extends Activity implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener {
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private MediaPlayer mMediaPlayer;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().setFormat(PixelFormat.UNKNOWN);
mSurfaceView = (SurfaceView) findViewById(R.id.surfaceview);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
String stringPath = "/storage/emulated/0/Movies/Lesson%201/30%20-%20Create%20Some%20Fake%20Data.mp4";
stringPath = android.net.Uri.parse("file://" + stringPath).getPath();
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setOnErrorListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setDataSource(stringPath);
mMediaPlayer.setDisplay(holder);
mMediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return false;
}
}
and for testing your xml can be like
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<SurfaceView
android:id="#+id/surfaceview"
android:layout_width="300dp"
android:layout_height="169dp"
/>
</LinearLayout>
Try This
String filePath = "file:///storage/emulated/0/Movies/test.mp4"
filePath.replace("file:///", "/"));
MediaPlayer player = MediaPlayer.create(this, Uri.parse(filePath));
player.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
I'm offering another answer to the original question, which may not be applicable to the specific problem as described by #roger later on. I have found that there is no need to further process/parse the media file path. Just use it directly. For example, I have a locally-stored media file on my Samsung Galaxy phone: "/storage/emulated/0/Samsung/Music/Over_the_Horizon.mp3"
. The following simple code works for playing the file:
String url = "/storage/emulated/0/Samsung/Music/Over_the_Horizon.mp3";
MediaPlayer mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(url);
mPlayer.prepare();
mPlayer.start();
(I have omitted try-catch blocks and other non-basic code). If SecurityException or IOException is issued while running the code, most likely a user permission needs to be added at the right place inside AndroidManifest.xml file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
<application
</application>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>

Stop audio playback

I am new to Android programming and i have a problem that i can not solve without your help...Please, give me a hand... ;-)
When i start playing my audio file, i can not stop it. Instead, I am quitting the application.
I play audio file with this code:
public class Word1Audio extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word1video);
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.lige);
mediaPlayer.start();
}
}
HOWEVER, when i try to stop it when the back button is pressed by this piece of code
#Override
public void onBackPressed() {
// do something on back.
return;
}
my applications closes down and audio continues to play...
Do you have an idea why the application closes down? And how can i just stop the music and go back to the previous page...??
Thank you for your time and help... :-)
you could try
MediaPlayer mediaPlayer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word1video);
mediaPlayer = MediaPlayer.create(this, R.raw.lige);
mediaPlayer.start();
}
#Override
public void onBackPressed() {
if( mediaPlayer.isPlaying() ) {
mediaPlayer.stop();
}
super.onBackPressed();
}

Categories

Resources