Media player unable to add in new thread - java

Hello I am creating a media player app in which I retrieved the audio from Firebase database in a seperate fragment. But when I switches to that fragment it freezes for 5-10 seconds before start. It is too irritating. So I put the Media Player in a new thread, but I have use of media player outside the thread which causes error. THANKS...
Here is a sample of code =>
new Thread(new Runnable() {
#Override
public void run() {
mediaPlayer.setAudioAttributes(
new AudioAttributes
.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build());
try {
mediaPlayer.setDataSource(audioUrl);
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
runnable = new Runnable() {
#Override
public void run() {
//set progress on seekbar
seekBar.setProgress(mediaPlayer.getCurrentPosition());
//Handle post delay for 0.5 seconds
handler.postDelayed(this, 50);
}
};
}
}).start();
//Get duration of media player
int duration = mediaPlayer.getDuration();
//Convert milliseconds to minutes and seconds
String sDuration = convertFormat(duration);
//Set duration on text view
tv_seekbar_end.setText(sDuration);
play_audio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
play_audio.setVisibility(View.INVISIBLE);
pause_audio.setVisibility(View.VISIBLE);
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
//Set max. on seek bar
seekBar.setMax(mediaPlayer.getDuration());
//Start handler
handler.postDelayed(runnable, 0);
}
});

Please try the below way, the below code is in kotlin but you can use this way in java
mPlayer = MediaPlayer.create(mContext, myUri(uriAudio))
if (Build.VERSION.SDK_INT >= 21) {
val audioAttributes: AudioAttributes =
AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
mPlayer!!.setAudioAttributes(audioAttributes)
} else {
mPlayer!!.setAudioStreamType(AudioManager.STREAM_MUSIC)
}
if (mPlayer != null && !mPlayer!!.isPlaying) {
mPlayer!!.start()
}
please check this link may help you more
https://stackoverflow.com/a/68096841/4042384

Related

my mediaplayer starts a new instance instead of stopping

im making a media player that plays mp3s mixes stored on firebase. i can make the links play no problem. but when i press the item again i want it to be the equivalent of pressing stop. but for some reason it does not stop it starts a new instance of the media. can some one tell me what ive done wrong please.
my code
in my on create
mMediaplayer = null;
then my method
private void fetchAudioUrlFromFirebase() throws IOException {
String mp3 = mp3url;
mMediaplayer = new MediaPlayer();
mMediaplayer.setDataSource(mp3);
mMediaplayer.prepare();//prepare to play
if (mMediaplayer.isPlaying()) {
stopPlaying();
} else {
playMedia();
}
}
private void stopPlaying() {
if (mMediaplayer != null) {
mMediaplayer.stop();
}
}
private void playMedia() {
mMediaplayer.start();
}
}
then in item onclick
try {
fetchAudioUrlFromFirebase();
} catch (IOException e) {
e.printStackTrace();
}
issue : lets assume media player is already playing so
// song is playing
mMediaplayer = new MediaPlayer(); // you created a new player
mMediaplayer.setDataSource(mp3);
mMediaplayer.prepare();//prepare to play
if (mMediaplayer.isPlaying()) { // new player is not in playing state
stopPlaying(); // so you always checking the state of new player
} else {
playMedia();
}
check first then create
if (mMediaplayer!=null && mMediaplayer.isPlaying()) {
stopPlaying();
}
mMediaplayer = new MediaPlayer();
mMediaplayer.setDataSource(mp3);
mMediaplayer.prepare();//prepare to play
playMedia();
so the logic can be reduced to
if (mMediaplayer!=null && mMediaplayer.isPlaying()) {
mMediaplayer.stop();
mMediaplayer.release();
}
mMediaplayer = new MediaPlayer();
mMediaplayer.setDataSource(mp3);
mMediaplayer.prepare();//prepare to play
mMediaplayer.start();

mediaPlayer CompletionListener playing next song before Completion of song

I have written a mediaPlayer Application and in that mediaplayer.setOnCompletionListener is called before the completion of song and it goes on for every song...It's keep on going to next song until it reaches the last song in playList... Please Help
Here is my code:
tracksListViewForSpeakers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Track track = playlistContents.get(position);
String filepath = track.getFilePath();
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(filepath);
mediaPlayer.start();
// mediaPlayer.setVolume(0, 0);
} catch (IOException e) {
e.printStackTrace();
for(int i=0;i<4;i++)
System.out.println("MediaPlayer Exception");
}
}
});
Completion Listener
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
onNextClicked(mp);
}
});
OnNextClickedMethod
public void onNextClicked(MediaPlayer mp){
int val = myModel.getCurrentPlayingIndex();
val=val+1;
Track track = currentPlayList.get(val);
String filepath = track.getFilePath();
if (mp.isPlaying()) {
mp.stop();
}
mp.reset();
try {
mp.setDataSource(filepath);
mp.start();
// mediaPlayer.setVolume(0, 0);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Use This To Play Next Song On Completion
#Override
public void onCompletion(MediaPlayer arg0) {
if (isRepeat) {
// repeat is on play same song again
playSong(currentSongIndex);
} else if (isShuffle) {
// shuffle is on - play a random song
Random rand = new Random();
currentSongIndex = rand.nextInt((songsList.size() - 1) - 0 + 1) + 0;
playSong(currentSongIndex);
} else {
// no repeat or shuffle ON - play next song
if (currentSongIndex < (songsList.size() - 1)) {
playSong(currentSongIndex + 1);
currentSongIndex = currentSongIndex + 1;
} else {
// play first song
playSong(0);
currentSongIndex = 0;
}
}
}
Then playsong method is like this
public void playSong(int songIndex){
// Play song
try {
mp.reset();
mp.setDataSource(songsList.get(songIndex).get("songPath"));
mp.prepare();
mp.start();
// Displaying Song title
String songTitle = songsList.get(songIndex).get("songTitle");
songTitleLabel.setText(songTitle);
// Changing Button Image to pause image
btnPlay.setImageResource(R.drawable.btn_pause);
// set Progress bar values
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
// Updating progress bar
updateProgressBar();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
or u can see this tutorial and implement this
http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/
try like this
//set path
mediaPlayer.setDataSource("yourFilePath");
mediaPlayer.setOnPrepareListener(new OnPrepareListener(MediaPlayer mediaPlayer) {
mediaPlayer.start();
});
mediaPlayer.prepareAsync();
don't call start() without prepareAsync();
Hope it will solve your problem..

Taking pictures at regular intervals (Android Camera API)

So I'm trying to take multiple pictures at regular time intervals, however I get a "takePicture Failed" Exception after the first picture is previewed on the surfaceView.
Here's my takePictures() method which is called when a button is pressed:
public void takePictures() {
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
camera = Camera.open();
if (camera != null) {
try {
camera.setPreviewDisplay(surfaceView.getHolder());
camera.startPreview();
camera.takePicture(null, null, new CustomPictureCallbcak(this, cacheDir, imageView, 3, 5000));
}
catch (Exception e) {
e.printStackTrace();
}
}
}
else {
Toast.makeText(this, "No camera found.", Toast.LENGTH_SHORT).show();
}
}
And here's the onPictureTaken() method of CustomPictureCallback :
#Override
public void onPictureTaken(byte[] data, Camera camera) {
//get date info for file name
SimpleDateFormat sdf = new SimpleDateFormat("ddmmyyyyhhmmss");
String date = sdf.format(new Date());
String fileDir = createImageFileName(date);
//write the image to cache
writeImageToCache(fileDir, data);
//display file name in a toast notification
Toast.makeText(c, fileDir, Toast.LENGTH_SHORT).show();
//show picture on imageview
imageView.setImageBitmap(BitmapFactory.decodeByteArray(data, 0, data.length));
//retake images
this.camera = camera;
while (numOfImagesAlreadyTaken <= numOfImages) {
Thread thread = new Thread() {
#Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
numOfImagesAlreadyTaken++;
CustomPictureCallbcak.this.camera.stopPreview();
sleep(delay);
CustomPictureCallbcak.this.camera.takePicture(null, null, CustomPictureCallbcak.this);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
}
Toast.makeText(c, "Total images taken: " + numOfImagesAlreadyTaken, Toast.LENGTH_SHORT).show();
//release camera
camera.release();
camera = null;
}
As Aleksander Lidtke suggested, I created a single thread inside my takePictures() method and put a while loop inside it:
public void takePictures(final int numOfPictures, final int delay) {
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Thread thread = new Thread() {
#Override
public void run() {
// TODO Auto-generated method stub
super.run();
while (numOfPicturesAlreadyTaken <= numOfPictures) {
try {
camera = Camera.open();
camera.setPreviewDisplay(surfaceView.getHolder());
camera.startPreview();
camera.takePicture(null, null, new CustomPictureCallbcak(MainActivity.this, cacheDir, imageView));
numOfPicturesAlreadyTaken++;
sleep(delay);
}
catch (Exception e) {
e.printStackTrace();
Log.d("TEST", e.getMessage());
}
}
}
};
thread.start();
}
else {
Toast.makeText(this, "No camera found.", Toast.LENGTH_SHORT).show();
}
}

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);

Android MediaPlayer setNextMediaPlayer() alternative

I published an app recently, and users are reporting crashes because my program included the method setNextMediaPlayer(). I now realize that this only works for API 16+, and my app supports API 8+. I was wondering if there is an alternate way of achieving the same effect.
My app has some text to speech, and what I was doing was creating an ArrayList of MediaPlayers that each had a short sound file, and then playing them one after an other. If I remove this method from the code, the audio becomes too choppy to understand.
I was thinking about using the SoundPool class, but there is no OnCompleteListener, so I'm not sure how I would do it.
So basically my question is: Is there a way to transition seamlessly between audio files without using the setNextMediaPlayer() method?
Thanks very much for your time!
EDIT
I added this code that I found
private class CompatMediaPlayer extends MediaPlayer implements OnCompletionListener {
private boolean mCompatMode = true;
private MediaPlayer mNextPlayer;
private OnCompletionListener mCompletion;
public CompatMediaPlayer() {
try {
MediaPlayer.class.getMethod("setNextMediaPlayer", MediaPlayer.class);
mCompatMode = false;
} catch (NoSuchMethodException e) {
mCompatMode = true;
super.setOnCompletionListener(this);
}
}
public void setNextMediaPlayer(MediaPlayer next) {
if (mCompatMode) {
mNextPlayer = next;
} else {
super.setNextMediaPlayer(next);
}
}
#Override
public void setOnCompletionListener(OnCompletionListener listener) {
if (mCompatMode) {
mCompletion = listener;
} else {
super.setOnCompletionListener(listener);
}
}
#Override
public void onCompletion(MediaPlayer mp) {
if (mNextPlayer != null) {
// as it turns out, starting a new MediaPlayer on the completion
// of a previous player ends up slightly overlapping the two
// playbacks, so slightly delaying the start of the next player
// gives a better user experience
SystemClock.sleep(50);
mNextPlayer.start();
}
mCompletion.onCompletion(this);
}
}
But now how do I add the audio files? I tried this:
// assigns a file to each media player
mediaplayers = new ArrayList<CompatMediaPlayer>();
for (int i = 0; i < files.size(); i++) {
mediaplayers.add((CompatMediaPlayer) CompatMediaPlayer.create(this, files.get(i)));
}
but am getting a class cast exception because MediaPlayer cannot be casted to CompatMediaPlayer.
Create Compat player that will work with onCompletionListener to start the next player like:
public void onCompletion(MediaPlayer mp) {
if (mCompatMode && mNextPlayer != null) {
mNextPlayer.prepare();
mNextPlayer.start();
}
}
Somewhere in your constructor check if there is method (or check SDK version) named "setNextMediaPlayer"
mCompatMode = Build.VERSION.SDK_INT < 16;
Define method like this one:
public void setNextMediaPlayer(MediaPlayer next) {
if (mCompatMode) {
mNextPlayer = next;
} else {
super.setNextMediaPlayer(next);
}
}
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
mediaPlayer = new MediaPlayer();
try {
AssetFileDescriptor descriptor = context.getAssets().openFd("play1.mp3");
mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
MediaPlayer md = new MediaPlayer();
try {
AssetFileDescriptor descriptor = context.getAssets().openFd("play2.mp3");
md.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
md.setAudioStreamType(AudioManager.STREAM_MUSIC);
md.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setNextMediaPlayer(md);
MediaPlayer md1 = new MediaPlayer();
try {
AssetFileDescriptor descriptor = context.getAssets().openFd("play3.mp3");
md1.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
md1.setAudioStreamType(AudioManager.STREAM_MUSIC);
md1.prepare();
} catch (IOException e) {
e.printStackTrace();
} md.setNextMediaPlayer(md1);

Categories