I want to skip the video's title to play,But I don't know how to do it.If you know, please tell me, thank you.
MediaPlayer myMediaPlayer = new MediaPlayer();
myMediaPlayer.setDataSource(this, Uri.parse(uri));
holder = surfaceView.getHolder();
holder.addCallback(new MyCallBack());
myMediaPlayer.prepare();
mTimer = new Timer();
mTimerTask = new TimerTask() {
#Override
public void run() {
if (isChanging == true) {
return;
}
play_bar.setProgress(myMediaPlayer.getCurrentPosition());
}
};
mTimer.schedule(mTimerTask, 0, 100);
totale_time.setText(getTime(myMediaPlayer.getDuration() / 1000));
myMediaPlayer.start();
play_bar.setMax(myMediaPlayer.getDuration());
myMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer arg0) {
PlayVideoScreen.this.finish();
}
});
Please use mediyaPlayer.seekTo(milliseconds).
Use MediaPlayer#seekTo(int msec) to seek to specified position you wanna set
Related
i want to make a special alarm app and the user should have the possibility to be woken up with music getting louder e.g. for 60 seconds.
I could not find a way to do this, that's why I need your help.
Thank you for your help and sorry for my bad English
final AudioManager am=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
oldvolume=am.getStreamVolume(AudioManager.STREAM_MUSIC);
am.setStreamVolume(AudioManager.STREAM_MUSIC,100,0);
if (increase<=0) mediaPlayer.setVolume(volume,volume);
mediaPlayer.start();
if (increase>0){
mediaPlayer.setVolume(0,0);
final double hohe=volume/increase;
new CountDownTimer((increase*1000),1000) {
#Override
public void onTick(long millisUntilFinished) {
mediaPlayer.pause();
mediaPlayer.setVolume((float) hohe*millisUntilFinished,(float) hohe*(increase*1000-millisUntilFinished));
mediaPlayer.start();
}
#Override
public void onFinish() {
mediaPlayer.pause();
mediaPlayer.setVolume( volume,volume);
mediaPlayer.start();
}
}.start();
That's what i got until now.
increase= increasing time in seconds
volume= max volume alarm should have
Solution, works for me is:
private Handler mHandler = new Handler();
private Runnable mVolumeRunnable = new Runnable() {
#Override
public void run() {
if (mediaPlayer != null&¤tvolume<endvolume) {
currentvolume += volumeincrease;
mediaPlayer.setVolume(currentvolume/100f, currentvolume/100f);
Toast.makeText(AlertActivity.this,String.valueOf(currentvolume),Toast.LENGTH_SHORT).show();
mHandler.postDelayed(mVolumeRunnable, 1000);
}
else mHandler.removeCallbacks(mVolumeRunnable);
}
};
The audio file (.mp3) gets played when I don't use the onPreparedListener, but I get an error after the file tries to replay after some time (because it's not listening for the state obviously). So basically the code is checking the battery state and if it's 10% or below it plays the alarm sound, I also have a button which I can click to stop the sound. Now with the onPreparedListener the alarm sound doesn't get played anymore. What am I doing wrong?
tv_battery = (TextView) findViewById(R.id.tv_battery);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.alarm);
Button b = (Button) findViewById(R.id.stop_alarm);
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp.isPlaying()) {
mp.stop();
mp.release();
}
}
});
runnable = new Runnable() {
#Override
public void run() {
int level = (int) batteryLevel();
tv_battery.setText("Battery: " + level + "%");
handler.postDelayed(runnable, 5000);
if(level <= 10) {
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(final MediaPlayer mp) {
CountDownTimer count = new CountDownTimer(7000, 1000) {
public void onTick(long millisUntilFinished) {
mp.start();
}
public void onFinish() {
//code fire after finish
mp.stop();
}
};
count.start();
}
I think you have place mp.start(); on wrong place please move it before count.start(); as below :
CountDownTimer count = new CountDownTimer(7000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
//code fire after finish
mp.stop();
}
};
mp.start();
count.start();
Also correct below condition if media player already started when battery is under 10 otherwise media player again prepared even after started :
if(level <= 10 && !mp.isPlaying()) {
I am trying to develop a simple test project that plays sound when I tap the button and stop automatically after a few minutes after playing the sound.
Here is a code snippet:
Code for playing:
if (mPlayer != null) mPlayer = null;
mPlayer = MediaPlayer.create(this, R.raw.shush_v2);
mPlayer.setLooping(true);
mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
Code for stopping:
if(mPlayer != null && mPlayer.isPlaying()) {
mPlayer.stop();
mPlayer.reset();
mPlayer.release();
mPlayer = null;
}
But sometimes I can still hear two sounds playing after I've stopped the sound.
Have so ever seen this behaviour before?
You should clear your media player.
//example usage of clearing media player when its over.
ourSong.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
clearMediaPlayer(mp);
}
});
//You can use this to stop media player.
private void clearMediaPlayer(MediaPlayer mp){
if(mp!=null){
mp.stop();
mp.release();// this will clear memory
mp = null;
}
}
public class PlayerApp extends Activity {
Button btnStart;
MediaPlayer mediaPlayer = null;
// Use the handler to stop the Player, after specific time
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player_app);
btnStart = (Button)findViewById(R.id.btnStart);
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
// Initialize Player and start it.
// Call the Handler same time.
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.test);
mediaPlayer.start();
startHandler();
}
});
}
private void startHandler()
{
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// if Player is not null, then Stop it and Reset Null.
if(mediaPlayer!=null)
{
mediaPlayer.stop();
mediaPlayer = null;
}
}
}, 2500);
}
}
I have developed app in which there are 4 sound files playing together
First sound :
on next button press
-Second sound :
on previous button press
Third sound :
plays sound of item
Fourth sound :
plays on item (How item speaks)
Everything works well until user play back and fourth quickly, text-to-speech engine crash with
MediaPlayer: error (-19, 0) android
i tried to reset media player , release and stop in next and previous button, but still sound of items stops after few back and fourth.
What i have done :
public class A extends Activity {
int imgArr[] = {R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d};
int soundsrc[] = {R.raw.a, R.raw.b, R.raw.c, R.raw.d};
String pro[] = new String[]{"a", "b", "c", "d"};
ImageView iView;
int count = 0;
TextToSpeech txtToSpeech1;
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_names);
stopService(new Intent(this, PlayMusic.class));
iView = (ImageView) findViewById(R.id.imageView5);
iView.setImageResource(R.drawable.befora);
txtToSpeech1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
txtToSpeech1.setLanguage(Locale.UK);
txtToSpeech1.speak(pro[count], TextToSpeech.QUEUE_FLUSH, null);
}
}
});
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
aSounds();
}
}, 1000);
}
#Override
public void onDestroy() {
// Don't forget to shutdown!
if (txtToSpeech1 != null) {
txtToSpeech1.stop();
txtToSpeech1.shutdown();
}
super.onDestroy();
}
public void onPause(){
if(txtToSpeech1 !=null){
txtToSpeech1.stop();
txtToSpeech1.shutdown();
}
super.onPause();
}
public void aSounds() {
mp = MediaPlayer.create(ANames.this, soundsrc[count]);
mp.start();
}
public void forwardd(View v) {
buttonSounds(R.raw.multimedia_button_click);
if (count < imgArr.length && count >= 0) {
count++;
if (count == imgArr.length)
count = 0;
iView.setImageResource(imgArr[count]);
if (txtToSpeech1.isSpeaking()) {
Log.i("Tag", "Stop speaking");
txtToSpeech1.stop();
}
txtToSpeech1.speak(pro[count], TextToSpeech.QUEUE_FLUSH, null);
// Execute some code after 2 seconds have passed
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
aSounds();
}
}, 1000);
}
}
public void backwardd(View v) {
buttonSounds(R.raw.multimedia_button_click);
if (count >= 0 && count < imgArr.length) {
count--;
if (count == -1) {
count = imgArr.length - 1;
}
iView.setImageResource(imgArr[count]);
txtToSpeech1.speak(pro[count], TextToSpeech.QUEUE_FLUSH, null);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
aSounds();
}
}, 1000);
}
}
public void onImageClick(View v) {
txtToSpeech1.speak(pro[count], TextToSpeech.QUEUE_FLUSH, null);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
aSounds();
}
}, 1000);
}
public void home(View v) {
buttonSounds(R.raw.multimedia_button_click);
Intent ii = new Intent(ANames.this, PlayMusic.class);
finish();
startService(ii);
}
public void buttonSounds(int src) {
mp = MediaPlayer.create(ANames.this, R.raw.multimedia_button_click);
mp.start();
}
}
-- i have been trying since very very very long
-- tried almost all stack-overflow answers also Google regarding media player working .
-- also how to stop , release , reset, start media player on button click, but nothing works for me..
-- So at last decided to put question in stack-overflow for help
-- any help is heartly welcome and thanks in advance
If the buttons plays a sound effect like 1-4 seconds. Consider using SoundPool class. It should be used for that stuff as I know. Media player errors LINK and LINK. It seems whoever were getting error 19 needed to release the resource which he was playing.
I am trying to create a delay for an Audio file that I am repeatedly playing.
However, the code doesn't seems to have any effect whatsoever. Here is my code:
try {
mp.setDataSource(text);
mp.prepare();
mp.setLooping(true);
mp.setVolume(0.5f,0.5f);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp)
{
final Handler h = new Handler();
h.postDelayed(new Runnable()
{
private long time = 0;
#Override
public void run()
{
time += 1000;
Log.d("TimerExample", "Going for... " + time);
h.postDelayed(this, 5000);
}
}, 5000);
}
});
Any fix/solution would be highly appreciated.
Please refer this https://stackoverflow.com/questions/31247607/adding-delay-while-mediaplayer-loops
Mainly you have to use listener like this
private final Runnable loopingRunnable = new Runnable() {
#Override
public void run() {
if (mp != null) {
if (mp.isPlaying() {
mp.stop();
}
mp.start();
}
}
}
mp.setDataSource(filePath);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
button.postDelayed(loopingRunnable, 5000);
}
});