record video from rtsp on TextureView - java

Hi I have the following code that simply displays an RTSP on a TextureView to then make a screenShot of the stream and save it as an image,
playButton.setOnClickListener(this);
playButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(String.valueOf(Uri.parse("rtsp://........./")));
} catch (IOException e) {
e.printStackTrace();
}
mMediaPlayer.setSurface(surface);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepareAsync();
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
playButton.setVisibility(View.INVISIBLE);
btnCapture.setVisibility(View.VISIBLE);
recordVideo.setVisibility(View.VISIBLE);
}
});
}
});
recordVideo.setOnClickListener(this);
recordVideo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
how can I record a portion of this video and save it as mp4? I made the start and stop buttons for recording the video Can I save the video between the start and stop?

Related

Media Player not playing sound from a live source but works for mp3

Currently working on an app in Android Studio to play music from a live audio source. I got the play and pause button to work for certain url's such as this, which may only work either because it's http. However, when doing it with this link, which is live audio from a college radio station, it doesn't play anything (the static when the station is not on can't be heard in the app).
My code is below:
public class RadioSelection extends AppCompatActivity {
ImageView fm;
ImageView digital;
MediaPlayer mediaPlayer;
ImageView playPause;
String stream = "http://wmuc.umd.edu:8000/wmuc-hq";
boolean prepared = false;
boolean started = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_selection);
mediaPlayer=new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
new PlayerTask().execute(stream);
playPause = findViewById(R.id.play_pause);
fm = findViewById(R.id.fm);
digital = findViewById(R.id.digital);
playPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (started) {
started = false;
mediaPlayer.pause();
Toast.makeText(RadioSelection.this, "Paused", Toast.LENGTH_SHORT).show();
} else {
started = true;
mediaPlayer.start();
Toast.makeText(RadioSelection.this, "Playing", Toast.LENGTH_SHORT).show();
}
}
});
fm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
digital.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
private class PlayerTask extends AsyncTask<String, Void, Boolean> {
#Override
protected Boolean doInBackground(String... strings) {
try {
mediaPlayer.setDataSource(strings[0]);
mediaPlayer.prepare();
prepared = true;
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("It's prepared!");
return prepared;
}
#Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
}
}
#Override
protected void onPause() {
super.onPause();
if (started) {
mediaPlayer.pause();
}
}
#Override
protected void onResume() {
super.onResume();
if (started) {
mediaPlayer.start();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if (prepared) {
mediaPlayer.release();
}
}
}
I feel like it has to do with the protocol but I'm not sure, and I don't really know of a way to test if it actually successfully connected to the audio source. I'd really appreciate any help.

How to play android radio with screen off background playable option

How to play android radio with screen off background playable option. My App working fine without backgournd playing option. But I want to give background playing option in my app that is when user closes the app, the radio should keep on playing, untill user stops it.
I am also disable screen off option in xml file with android:keepScreenOn="true" option but I want to remove this option and keep mobile screen off and play my app in background.
This is my code
Button BPlay;
String stream = "http://stream.zeno.fm/hmzuvfwn9k0uv";
MediaPlayer mediaPlayer;
boolean prepared = false;
boolean started = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio);
BPlay = (Button) findViewById(R.id.b_play);
BPlay.setEnabled(false);
BPlay.setText("Loading.....");
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
new PlayerTask().execute(stream);
BPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (started){
started = false;
mediaPlayer.pause();
BPlay.setText("PLAY");
} else {
started = true;
mediaPlayer.start();
BPlay.setText("PAUSE");
}
}
});
}
class PlayerTask extends AsyncTask<String, Void, Boolean> {
#Override
protected Boolean doInBackground(String... strings) {
try {
mediaPlayer.setDataSource(strings [0]);
mediaPlayer.prepare();
prepared = true;
} catch (IOException e) {
e.printStackTrace();
}
return prepared;
}
#Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
BPlay.setEnabled(true);
BPlay.setText("PLAY");
}
}
#Override
protected void onPause() {
super.onPause();
if (started){
mediaPlayer.pause();
}
}
#Override
protected void onResume() {
super.onResume();
if (started){
mediaPlayer.start();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if (prepared){
mediaPlayer.release();
}
}
}
In order to have you media playing while the app is in the background, you'll need a MediaSession background Service running in parallel.
TLTR, here's a basic tutorial from Google on how to do that.

JAVA error: "E/MediaPlayer: Error (-38,0)"

I'm having a problem with my radio, the radio works and everything, but the thing is that when I pressed Pause the radio stops, but, when i click on PLAY again, the radio starts again, but like if it where a song. Conclusion (sorry for my bad explanation)
when i use OnPause() this happens
weird example===
if I start playing 1:00pm , and then I paused 1:01pm, then wait a couple minutes, and play again, the radio will continue playing like if it where 1:01pm, and it's 1:05pm (awkward example)
when i changed to OnStop()
it gave me MediaPlayer code error(-38,0)
enter code here :::::: b_play = findViewById(R.id.b_play);
b_play.setEnabled(false);
b_play.setText("Cargando...");
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
new PlayerTask().execute(stream);
b_play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (started) {
started = false;
mediaPlayer.stop();
b_play.setText("Escuchar");
} else {
started = true;
mediaPlayer.start();
b_play.setText("Pausar");
}
}
#Override
protected Boolean doInBackground(String... strings) {
try {
mediaPlayer.setDataSource(strings[0]);
mediaPlayer.prepare();
prepared = true;
} catch (IOException e) {
e.printStackTrace();
}
return prepared;
}
#Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
mediaPlayer.start();
b_play.setEnabled(true);
b_play.setText("Escuchar");
}
}
#Override
protected void onPause() {
super.onStop();
if (started) {
mediaPlayer.stop();
}
}
#Override
protected void onResume() {
super.onResume();
if (started) {
mediaPlayer.start();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if (prepared) {
mediaPlayer.release();
}
}
If u could help me out, i'll do apprecciate, I'm sorry about my english and weird explanation (?
remove onPause with onStop
#Override
protected void onStop() {
super.onStop();
if (started) {
mediaPlayer.stop();
}
}
#Override
protected void onResume() {
super.onResume();
if (started) {
mediaPlayer.start();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if (prepared) {
mediaPlayer.release();
}
}

Media Player error in RecyclerView

i have a RecyclerView that displays a list of songs and i have successfully implemented on click listener with it and verified it with logs. Now i want to play the song upon click event. When i first click on a item the songs starts to play but if i then tap on any other item the app simply crashes
Here is my RecyclerViewClickListener
SongAdapter songAdapter = new SongAdapter(getContext(), songsList, new RecyclerViewClickListener() {
#Override
public void onClick(View view, int position) {
Toast.makeText(getContext(), "You clicked on Ssong "+songsList.get(position).getSongName(), Toast.LENGTH_SHORT)
.show();
songPath = songsList.get(position).getUrl();
Log.v("Song Path", songPath);
if(mp.isPlaying())
releaseMediaResources();
playSong();
}
});
recyclerView.setAdapter(songAdapter);
Here is my playSong method
private void playSong () {
if(!songPath.equals("")) {
try {
mp.setDataSource(songPath);
} catch (Exception e) {
Log.e("Home Fragment", "Error setting song Url", e);
}
mp.prepareAsync();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.reset();
mp.release();
}
});
}
}
And here is my releaseMediaResources
private void releaseMediaResources() {
mp.stop();
mp.reset();
mp.release();
}
These are the errors
java.lang.IllegalStateException
at android.media.MediaPlayer.isPlaying(Native Method)
at com.ashutosh.prototype4.HomeFragment.playSong(HomeFragment.java:159)
at com.ashutosh.prototype4.HomeFragment.releaseMediaResources(HomeFragment.java:192)
at com.ashutosh.prototype4.HomeFragment.access$100(HomeFragment.java:31)
at com.ashutosh.prototype4.HomeFragment$2.onClick(HomeFragment.java:94)
at com.ashutosh.prototype4.SongAdapter$1.onClick(SongAdapter.java:50)
According to me the main problem lies in setting up the media player because when i display only toast messages there's no issues at all but as soon as i implement media player to on click method the crash occurs
I will create every time the new MediaPlayer, but of course try to check, if there is not already existing instance for instance:
private MediaPlayer mediaPlayer;
private void playSong(String filePath) {
if (mediaPlayer != null && mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
mediaPlayer = MediaPlayer.create(getActivity(), Uri.parse(filePath));
mediaPlayer.setVolume(DEFAULT_VOLUME_MUSIC, DEFAULT_VOLUME_MUSIC);
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
}
});
}

How to play the next song in an array?

For an assignment I am trying to play songs from an array using media player. The first song in the array plays fine, but the next, and last track buttons are where my issue is. Could anyone suggest a way of playing the next/last song from my music array?
None relevant code not included.
// Arrays of sound files
private int[] audioFileArrayChill = {R.raw.vanilla_summer, R.raw.lifeline, R.raw.remember_the_mountain_bed};`
// Listen for the end of the track
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
for (int x = 1; x <= audioArray.length; x++) {
currentIndex++;
// Play next song in array
mp.selectTrack(audioArray[x]);
mp.start();
}
}
});
// Assigning onClickListener to last track button
lastTrack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer.reset();
mediaPlayer.selectTrack(audioArray[currentIndex - 1]);
try {
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
Log.e("Error", "ERROR");
}
}
});
// Assigning onClickListener to next track button
nextTrack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer.reset();
mediaPlayer.selectTrack(audioArray[currentIndex + 1]);
try {
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
Log.e("Error", "ERROR");
}
}
});
use below code for next button click
nextTrack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.stop();
mp.reset();
mp.setDataSource(audioArray[currentIndex + 1]);
mp.prepare();
mp.start();
}

Categories