Android play Text To Speech with audio focus (java) - java

I don't understand how to play text to speech with audio focus (there is spotify running some music when I try my app),
I don't understand the audioFocusChangeListener.
Here is my code:
public class MainActivity extends AppCompatActivity {
AudioManager.OnAudioFocusChangeListener audioFocusChangeListener;
public void playTTSWithAudioFocus(String toSpeak) {
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.requestAudioFocus(audioFocusChangeListener,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request temporary focus.
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
textToSpeech.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null,null);
}
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playTTSWithAudioFocus("hello world");
}
}
Please help me.

Related

Is there any option to add user-agent in streaming media Exo Player?

I'm facing a problem with exomedia player, I want to add support for some types of streaming url s but exomedia won't accept them,I have tried to much time to add the option " User AGENT" but can't turn it on, I have added my code and if there is someone who can help to fix this ?
I have tried to add User -Agent ,but in this code The player runs smooth but not some type of urls
public class ActivityStreaming extends Activity implements OnPreparedListener {
private long exitTime = 0;
private VideoView videoView;
String url;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_streaming);
url = getIntent().getStringExtra("url");
videoView = (VideoView) findViewById(R.id.video_view);
videoView.setOnPreparedListener(this);
Uri videoUri = Uri.parse(url);
videoView.setVideoURI(videoUri);
}
#Override
public void onPrepared() {
//Starts the video playback as soon as it is ready
videoView.start();
}
public void onBackPressed() {
super.onBackPressed();
}
public void closeStreaming() {
if ((System.currentTimeMillis() - exitTime) > 2000) {
Toast.makeText(this, getString(R.string.press_again_to_close_streaming), Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else {
finish();
}
}
}

My android App stop playing sound on button tap after some time

Hello there, i have just created a basic android app which plays different musics on button tap..
The app just worked fine for the first couple of seconds but when i just keep on tapping and tapping , at some point it stops playing the music and just crashed...
I am unable to figure out what the problem is ..Please help me make it work..
Thanks.
Here is my code :-
public class MainActivity extends AppCompatActivity {
MediaPlayer mediaPlayer;
public void PlayMusic(View view)
{
int ID = view.getId();
String NameID = view.getResources().getResourceEntryName(ID);
int sound= getResources().getIdentifier(NameID,"raw","com.example.pickachu.mypatatap");
mediaPlayer = MediaPlayer.create(this,sound);
mediaPlayer.start();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
The sound is not playing after multiple taps because you must be getting IllegalStateException because you are not releasing the Mediaplayer object and Mediaplayer Lifecycles are not managed properly when you tap multiple times.
You can use setOnCompletionListener(MediaPlayer.OnCompletionListener listener) to release mediaPlayer after completion of sound as:
mediaPlayer = MediaPlayer.create(this,sound);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.reset();
mp.release();
mediaplayer = null;
}
});
mediaPlayer.start();
pass uri instead string
mediaPlayer= MediaPlayer.create(this, Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/Music/music.mp3"));
mediaPlayer.setLooping(true);
mpintro.start();

Using manager for controlling the volume

I am having problem with using audio manager for controlling volume
I am making a simple app to change ringer mode from the app but the app is crashing. The way to get instance of audio manager as directed by google is showing error. Is it possible to access and control ringer volume and mode from app?
public class MainActivity extends AppCompatActivity {
private boolean isPhoneSilent;
private AudioManager audioManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
Button toggle_Btn = findViewById(R.id.toggle_Btn);
int ringerMode = audioManager.getRingerMode();
if(ringerMode==AudioManager.RINGER_MODE_SILENT)
{
isPhoneSilent=true;
}
else
toggle_Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(isPhoneSilent)
{
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
isPhoneSilent=false;
}else
{
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
isPhoneSilent=true;
}
}
});
}

android - how to play audio and display textview on click a button

I am a beginner. So far I have created an app that counts. I created a button which displays the text but it should also play the whistle sound.
I displayed my java code below.
/**
* Displays the winning team.
*/
public void displayForWinner(String score) {
TextView scoreView = (TextView) findViewById(R.id.textView);
scoreView.setText(String.valueOf(score));
}
To play a short sound effect, create an empty MediaPlayer object:
MediaPlayer mMediaPlayer;
Next, assign a MediaPlayer to it using your context, and sound (Place your sound file under app/src/main/res/raw/) :
mMediaPlayer = MediaPlayer.create(MyActivity.this,R.raw.whistle_sound);
Finally, when you're ready to play your sound, call:
mMediaPlayer.start();
-EDIT-
To make the text change after your sound has completed, use this:
public class MyActivity extends Activity {
MediaPlayer mMediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
mMediaPlayer = MediaPlayer.create(MyActivity.this,R.raw.whistle_sound);
}
public void displayForWinner(String score) {
final String FinalScore = score;
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
TextView scoreView = (TextView) findViewById(R.id.textView);
scoreView.setText(String.valueOf(FinalScore));
}
});
}
}
Also, consider trimming your audio clip to reduce delay.

when video finished go back to activity

I have some videos using videoView. everything is running well, but when the video is finished nothing happens, the phone stay on dark screen waiting to pres play again or the blink back on the phone.
I want that when videos is finished, go back to the activity before. this is my code.
public class chapterone extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.videoplayer);
VideoView videoView =
(VideoView)this.findViewById(R.id.videoView);
MediaController mc = new MediaController(this);
videoView.setMediaController(mc);
videoView.setVideoURI(Uri.parse(
"http://video.com/episode/" +
"cotton1.mp4"));
/* videoView.setVideoPath(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MOVIES) +
"/movie.mp4");
*/
videoView.requestFocus();
videoView.start();
}
}
the URL is only an example.
videoView.setOnCompletionListener(completionListener);
OnCompletionListener completionListener=new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.stop();
chapterone.this.finish();
}
};
i hope can help i found this, but i am not sure if is correct uset it
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
#Override
public void onCompletion(MediaPlayer mp){
// invoke your activity here
Intent i = new Intent(chapterone.this, videoserie.class);
startActivity(i);
}
});

Categories