Ionic nav-view and external resource - android

I'm trying to display a video within my application. I've created a link to it such:
Start movie
Everything is fine and the video spins up, but when I push the back button the nav state is resetted and I have to restart my app in order to get back to the "main" page of my app. Is there any trick to maintain the nav state when starting my video?

Yes There is trick you can use the below code in your back button listener
below is the example for your reference you can adjust it by getting the status of your media player like mediaplayer.isPlaying()
final MediaPlayer mp1 = MediaPlayer.create(PlayaudioActivity.this, R.raw.beet);
mBackBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp1.isPlaying()) {
}else{
finish();
}
}
});

Related

Detecting tab change when using Android Navigation Component with Bottom Navigation

I currently have an app (Java) that uses the Navigation Component with Bottom Navigation. I have a fragment for each tab.
One of the fragments navigates to a secondary fragment that features an audio player (using Media Player). This audi fragment has the following code that stops the audio when I go back to the parent fragment:
#Override
public void onDestroy() {
super.onDestroy();
if (mp != null) {
mp.pause();
mp = null;
}
}
The problem is that when the audio is playing and I tap on a different tab from the bottom navigation, the audio continues to play. I thought of using the following:
#Override
public void onStop() {
super.onStop();
if (mp != null) {
mp.pause();
mp = null;
}
}
And this solves the problem but it also prevents the audio from playing when the screen is turned off. (This doesn't happen when the onStop is not there).
The idea is to have the pause() function execute when the user taps on any of the bottom navigation tabs.
With my current structure, how can I get the sound to stop when the user taps on a bottom tab?
You can use "setOnItemSelectedListener()" to listen for clicks on Bottom Navigation.
If the user selects an item other than the one inside the audio player, stopAudioPlayer function is called
Example :-
navBottom.setOnNavigationItemSelectedListener(item -> {
if (item.getItemId() != R.id.nav_bottom_audio_player)
stopAudioPlayer();
return true;
});

How to add resume in amazon polly's text to speech in android?

I am using Amazon Polly , and I want to resume pronounced speech where it was paused but it starts from beginning whenever i tried to resume. I set source in media player as follows
mediaPlayer.setDataSource(presignedSynthesizeSpeechUrl.toString());
The code I tried to use pause and resume is as follows,
playPauseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(pauseVal){
mediaPlayer.seekTo(length);
mediaPlayer.start();
pauseVal = false;
}else{
mediaPlayer.pause();
length= mediaPlayer.getCurrentPosition();
pauseVal = true;
}
}
});
I am stuck here , can't move further with resume functionality. I will be thankful if any of you help.
Media player doesn't support resume functionality for passed URL . since I was using webview so I just passed that URL to Audio tag which supports resume functionality.
https://www.w3schools.com/html/html5_audio.asp

how can stop background runnig process of media player stop when pressed back button of mobile

i am searched many solution but could not find proper result when i press the back button of mobile i am using one counter variable and put the condition like when counter is 0 it called the music file.
but when i am comeout from that activity means press back button of mobile still that activity is runnig and called the music file .i am using this below code for music play.please anyone have solution then help me.i am using one timer function.like this when i==0 that time call one music file but when i press back button. this timer i wants to stop.
public void btntimer(){
new Handler().postDelayed((new Runnable() {
#Override
public void run() {
if(i!=32) {
matchNo();
}
else {
try{
showdialog();
media=1;
}catch (Exception e){
media=0;
}
updatepoints();
}
}
}), 4000);
}
song=MediaPlayer.create(this,R.raw.cartoon1);
song.start();
song.setLooping(false);
If I understand you correct, you want to stop playing music on back button pressed.
Call stop() in onBackPressed method.
#Override
public void onBackPressed() {
song.stop();
song.release();
song = null;
}

Play random sound on button click

I want to my application play random sound when user click on button. So I don't want android natural sound, I want to play my own sound every time user click on one button. Does anyone knows the solution of this problem.
To play a sound:
Play sound on button click android
For a random sound you just need to add all sounds in a list.
And in the onClickListener just get a random sound from your list.
Something like this:
List<Integer> soundList = new ArrayList<Integer>();
soundList.add(R.raw.sound1);
soundList.add(R.raw.sound2);
soundList.add(R.raw.sound3);
soundList.add(R.raw.sound4);
myButton.setOnClickListener(new OnClickListener {
#Override
public void onClick(View v) {
playRandomSound();
}
});
private void playRandomSound() {
int randomInt = (new Random().nextInt(soundList.size()));
int sound = soundList.get(randomInt);
MediaPlayer mp = MediaPlayer.create(this, sound);
mp.start();
}
No guarantee that this works! It's just an example, how you could do it!
One way to achieve that is:
Embed your sound , as an MP3 encoded file
Attaching a click event handler in the Android App
myButtonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//code that runs when button is clicked
}
});
Access and play the embedded MP3 file from the application in the event handler
MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile);
mPlayer.start();
see
How do I play an mp3 in the res/raw folder of my android app?
A tutorial explaining a complete process
http://www.accelerated-ideas.com/news/android-app-development--how-to-add-and-play-music-and-audio-files.aspx
Edit:
To disable native android click sound
yourbutton.setSoundEffectsEnabled(false);
source :
Disable Button click sound in android

Android Media Player doesn't play after app has been left for a while in the background

Bit of a strange one this and I can't work out what's happening.
When I launch my app (a game) the music starts playing. I have a button which turns the music on and off. The settings are saved to shared prefs so they are retained.
All works well, you can press the home key, re-invoke the app, leave it in the background while doing other things etc. However, if the app is left in the background for a while (say, overnight), and then re-invoked. Everything works apart from the music.
You can go into the main menu, hit the 'music on/off' button multiple times, but get nothing.
The only way to start the music is to kill the app (or exit correctly, ie, press the 'back' key from the main menu) and then relaunch it so everything is re-created from scratch.
I've confirmed that the 'music' object is still valid and the 'music on/off' button presses are being registered.
Has anyone has similar issues with Media Player? I can't work out what I am (or am not doing) to cause this.
Code
This is my media player class:
public class MusicMan implements MediaPlayer.OnPreparedListener {
MediaPlayer musicPlayer;
MusicMan(Context myContext){
musicPlayer = MediaPlayer.create(myContext, R.raw.music);
musicPlayer.setVolume(.6f, .6f);
}
public void listener(){};
public void start(){
musicPlayer.setLooping(true);
musicPlayer.start();
}
public void stop(){
musicPlayer.stop();
}
public void pause(){
musicPlayer.pause();
}
public int getPos(){
return musicPlayer.getCurrentPosition();
}
public void skipTo(int position){
musicPlayer.seekTo(position);
}
#Override
public void onPrepared(MediaPlayer arg0) {
}
}
And then I simply crate an object like so:
MusicMan music = new MusicMan(view.getContext());
And then I just start and stop the music using the methods in the MusicMan class:
music.start();
You need to use
musicPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
Use this where you set new MediaPlayer player. It sets the wake lock to MediaPlayer and don't let CPU go sleep till you yourself didn't kill or stop application.

Categories

Resources