video playback Issue using Cameraview - android

I am capturing a video using Cameraview, once the recording is finished, the app will playback the video in fullscreen. However after I record the first video, I am only seeing a black screen. If I ress back and record another video everything works and the playback is successful. The errors I am having are:
E/MediaPlayer: error (1, -2147483648)
E/MediaPlayer: isPlaying: called in state MEDIA_PLAYER_STATE_ERROR
here is the code sample:
public class VideoPreviewActivity extends AppCompatActivity {
FullscreenVideoLayout videoLayout;
CustomReusingDialog mCustomReusingDialog;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_preview);
videoLayout = (FullscreenVideoLayout) findViewById(R.id.videoview);
videoLayout.setActivity(this);
final Uri videoUri = getIntent().getParcelableExtra("video");
try {
videoLayout.setVideoURI(videoUri);
} catch (IOException e) {
e.printStackTrace();
}
videoLayout.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
playVideo();
}
});
videoLayout.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mCustomReusingDialog =CustomReusingDialog.newInstance(new CustomReusingDialog.Builder(VideoPreviewActivity.this, new CustomReusingDialog.OnDialogShowListener() {
#Override
public void onGetDialog(final Dialog mDialog, View mView) {
TextView uploadText =(TextView)mView.findViewById(R.id.uploadText);
TextView deleteText =(TextView)mView.findViewById(R.id.deleteText);
uploadText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mCustomReusingDialog.dismiss();
if (LoginManager.isNetworkAvailable(getApplicationContext()))
new UploadManager(VideoPreviewActivity.this,new File(videoUri.getPath())).execute();
else
LoginManager.showMessage(getApplicationContext(),"Please Check your Network Settings!!");
}
});
deleteText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mCustomReusingDialog.dismiss();
UploadManager.deleteFile(VideoPreviewActivity.this,new File(videoUri.getPath()));
}
});
}
}).setDialogLayout(R.layout.dialog_upload).setCancelAble(true));
mCustomReusingDialog.show(getSupportFragmentManager(),"");
}
});
}
void playVideo() {
if (videoLayout.isPlaying()) return;
videoLayout.start();
}
#Override
protected void onPause() {
super.onPause();
}
}}

Related

Mediaplayer Background Audio not work properly Using URL And Song Not Play Second Time

This is my second activity and when open this activity threw button activity take load and open after some time and some times UI Freeze and some time black screen open for some seconds
public class AartiActivity extends AppCompatActivity {
final MediaPlayer player1 = new MediaPlayer();
private Button btn_playarti, btn_pausearti, btn_stoparti;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aarti);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
btn_playarti = (Button) findViewById(R.id.btn_playarti);
btn_pausearti = (Button) findViewById(R.id.btn_pausearti);
btn_stoparti = (Button) findViewById(R.id.btn_stoparti);
try {
player1.setDataSource(STRING_URL);
player1.prepare();
} catch (IOException e) {
e.printStackTrace();
}
btn_playarti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer player) {
player.stop();
}
});
player1.start();
}
});
btn_pausearti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (player1.isPlaying()) {
player1.pause();
}
}
});
btn_stoparti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player1.stop();
}
});
}
#Override
protected void onStop() {
super.onStop();
player1.stop();
}
}

How to play only one sound at a time?

I have two ImageViews with different sounds which plays sound when clicked on one of them. When click on the first ImageView I want the sound to play and before the first ImageView finish playing the sound, if the second ImageView is click, I want to restrict the second ImageView to play a sound because the first ImageView is still playing the sound. In fact, I want to restrict a sound to play when another sound is playing. But, I didn't get the desired result.
public class Transportation extends AppCompatActivity {
private MediaPlayer sound,sound1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transportation);
sound = MediaPlayer.create(this,R.raw.vatunabus);
final ImageView imageView= (ImageView)findViewById(R.id.vatunabussound);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(sound1.isPlaying()){
sound.stop();
sound.release();
}
else if(sound.isPlaying()){
sound.pause();
imageView.setBackgroundResource(R.mipmap.playicon);
}
else {
sound.start();
imageView.setBackgroundResource(R.mipmap.pauseicon);
}
}
});
sound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
imageView.setBackgroundResource(R.mipmap.playicon);
}
});
sound1 = MediaPlayer.create(this,R.raw.varowlsanabus);
final ImageView imageView1 = (ImageView)findViewById(R.id.firstbussound);
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(sound.isPlaying()){
sound1.stop();
sound1.release();
}
else if(sound1.isPlaying()){
sound1.pause();
imageView1.setBackgroundResource(R.mipmap.playicon);
}
else {
sound1.start();
imageView1.setBackgroundResource(R.mipmap.pauseicon);
}
}
});
sound1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
imageView1.setBackgroundResource(R.mipmap.playicon);
}
});
}
}
Here's an idea, let's create a boolean value to tell us if a sound is currently playing.
private MediaPlayer sound,sound1;
private boolean soundPlaying = false; //new boolean variable
Next, before playing a sound, we'll check to make sure it's still false:
public void onClick(View v) {
//Add a check that exits the function if a sound is playing
//Add this check in both onClick methods
if(soundPlaying){
return;
}
...
}
Also, after sound.start() and sound1.start(), let's set the boolean to true:
soundPlaying = true;
Finally, in both instances of the onCompletion methods, let's reset the boolean to false:
public void onCompletion(MediaPlayer mp) {
soundPlaying = false;
...
}
The final code will look like this:
public class Transportation extends AppCompatActivity {
private MediaPlayer sound,sound1;
private boolean soundPlaying = false; //new boolean variable
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transportation);
sound = MediaPlayer.create(this,R.raw.vatunabus);
final ImageView imageView= (ImageView)findViewById(R.id.vatunabussound);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Add a check that exits the function if a sound is playing
if(soundPlaying){
return;
}
if(sound1.isPlaying()){
sound.stop();
sound.release();
}
else if(sound.isPlaying()){
sound.pause();
imageView.setBackgroundResource(R.mipmap.playicon);
}
else {
sound.start();
imageView.setBackgroundResource(R.mipmap.pauseicon);
}
}
});
sound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
soundPlaying = false;
imageView.setBackgroundResource(R.mipmap.playicon);
}
});
sound1 = MediaPlayer.create(this,R.raw.varowlsanabus);
final ImageView imageView1 = (ImageView)findViewById(R.id.firstbussound);
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Add a check that exits the function if a sound is playing
if(soundPlaying){
return;
}
if(sound.isPlaying()){
sound1.stop();
sound1.release();
}
else if(sound1.isPlaying()){
sound1.pause();
imageView1.setBackgroundResource(R.mipmap.playicon);
}
else {
sound1.start();
imageView1.setBackgroundResource(R.mipmap.pauseicon);
}
}
});
sound1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
soundPlaying = false;
imageView1.setBackgroundResource(R.mipmap.playicon);
}
});
}
}
I hope this helps. Couldn't try it in live code though, so let me know how it goes.

Created simple media player but after clicking each button five or six time it stop playing

I have created a simple media player. After clicking each button five or six times, it stops playing sound. But the app is not crashing and not showing any error. The app is playing sound without any problem at the first time. Plus, in my function where I have created onClick method, can I apply the stop or pause function to stop the previous playing sound?
Here is the code:
public class MainActivity extends AppCompatActivity {
MediaPlayer mymediaplayer;
AudioManager myaudiomanager;
public void playone(View view)
{
mymediaplayer=MediaPlayer.create(this,R.raw.one);
mymediaplayer.start();
}
public void playtwo(View view)
{
mymediaplayer=MediaPlayer.create(this,R.raw.two);
mymediaplayer.start();
}
public void playthree(View view)
{
mymediaplayer=MediaPlayer.create(this,R.raw.three);
mymediaplayer.start();
}
public void playfour(View view)
{
mymediaplayer=MediaPlayer.create(this,R.raw.four);
mymediaplayer.start();
}
public void playfive(View view)
{
mymediaplayer=MediaPlayer.create(this,R.raw.five);
mymediaplayer.start();
}
public void playsix(View view)
{
mymediaplayer=MediaPlayer.create(this,R.raw.six);
mymediaplayer.start();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myaudiomanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
int max=myaudiomanager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int progress=myaudiomanager.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar volume=(SeekBar)findViewById(R.id.seekBar);
volume.setMax(max);
volume.setProgress(progress);
volume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
myaudiomanager.setStreamVolume(AudioManager.STREAM_MUSIC,i,0);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
You should release media player before starting to play new item. I did change playone as example:
public void playone(View view)
{
releaseMediaPlayer(mymediaplayer);//release previous player if there are any
mymediaplayer=MediaPlayer.create(this,R.raw.one);
mymediaplayer.start();
}
void releaseMediaPlayer(MediaPlayer mPlayer){
if(mPlayer!=null)
{
mPlayer.release();
}
}

Android - MediaPlayer Error

I'm developing an app that plays a background music. The music is managed by a service that runs in background. In the Main activity, in the onCreate() method, the service starts with:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Background music service.
Intent musicServiceIntent = new Intent(this, BackgroundSoundService.class);
startService(musicServiceIntent);
final BackgroundSoundService bss = new BackgroundSoundService();
buttonStart = (Button) v.findViewById(R.id.button_start);
buttonStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bss.onStart();
}
});
buttonPause = (Button) v.findViewById(R.id.button_pause);
buttonPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bss.onPause();
}
});
buttonStop = (Button) v.findViewById(R.id.button_stop);
buttonStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bss.onStop();
}
});
}
}
}
The service is the following code:
BackgroundSoundService.java
public class BackgroundSoundService extends Service {
private static final String TAG = null;
static MediaPlayer mMediaPlayer;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.background_music);
mMediaPlayer.setVolume(1.0f, 1.0f);
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
playMusic();
}
});
playMusic();
}
public void playMusic() {
mMediaPlayer.start();
}
/**
*
* #param arg0 -
* #return -
*/
public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}
/**
*
*/
public void onStart() {
Log.d("A", "onStart()");
if(!mMediaPlayer.isPlaying()) {
Log.d("A", "mMediaPlayer is not playing");
playMusic();
} else {
Log.d("A", "mMediaPlayer is playing");
}
}
/**
*
*/
public void onPause() {
Log.d("A", "onPause()");
if(mMediaPlayer.isPlaying()) {
Log.d("A", "mMediaPlayer is playing");
mMediaPlayer.pause();
} else {
Log.d("A", "mMediaPlayer is not playing");
}
}
/**
*
*/
public void onStop() {
Log.d("A", "onStop()");
if(mMediaPlayer.isPlaying()) {
Log.d("A", "mMediaPlayer is playing");
mMediaPlayer.stop();
} else {
Log.d("A", "mMediaPlayer is not playing");
}
}
/**
*
*/
#Override
public void onDestroy() {
Log.d("A", "onDestroy()");
mMediaPlayer.stop();
mMediaPlayer.release();
}
/**
*
*/
#Override
public void onLowMemory() {
}
}
Now, MainActivity has three buttons to control the music. SART Button, starts music, PAUSE Button, put the sound in pause and STOP Button, stops music. Each one executes the methos onStart(), on Pause() and onStop() in the BackgroundSoundService. The problem is if I press the START Button after the STOP Button, the log show me this error:
E/MediaPlayer: start called in state 0
E/MediaPlayer: error (-38, 0)
I try solve this implementing:
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
But did not work. Any suggestions? Thank you.
Posting MainActivity code helps us to give an exact solution without deviating you to random answers.
After .stop() , if you want it to start again you need to reset it.
public void onStop() {
Log.d("A", "onStop()");
if(mMediaPlayer.isPlaying()) {
Log.d("A", "mMediaPlayer is playing");
mMediaPlayer.stop();
mMediaPlayer.reset();
mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.background_music);
} else {
Log.d("A", "mMediaPlayer is not playing");
}
}
EDIT
boolean isPrepared = false;
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
isPrepared = true;
}
});
public void playMusic() {
if(isPrepared){
mMediaPlayer.start();
isPrepared = false;
}
else{
Toast.makeText(getApplicationContext(),"Not prepared",LENGTH.SHORT)
.show();
}
}
Instead of mediaPlayer.create(), call new mediaplayer
Call prepare() and start() inside playMusic()
OR
prepareAsync() inside playMusic() and have onPreparedListener() calls start()

How to prepare mediaplayer in android while another playing?

I using MediaPlayerto play video from http stream is any solution to prepare second media player with another http url while first one still playing?
I want to play two videos without black screen between them, when first one end second start immediately.
Here is my code:
public class VideoActivity extends Activity implements SurfaceHolder.Callback {
private MediaPlayer mediaPlayer;
private SurfaceHolder surfaceHolder;
private SurfaceView playerSurfaceView;
private String videoSrc = "http://192.168.1.101:8090/api/video/";
private String android_id;
private MediaPlayer next;
private static boolean firstCall = true;
private String secondVideoSrc = "http://192.168.1.101:8090/api/secondvideo/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
playerSurfaceView = (SurfaceView)findViewById(R.id.playersurface);
surfaceHolder = playerSurfaceView.getHolder();
surfaceHolder.addCallback(this);
GetDeviceId();
videoSrc += android_id;
secondVideoSrc += android_id;
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
prepareVideoPlayer();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
private void GetDeviceId() {
android_id = Settings.Secure.getString(getBaseContext().getContentResolver(),
Settings.Secure.ANDROID_ID);
}
private void prepareVideoPlayer() {
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDisplay(surfaceHolder);
mediaPlayer.setDataSource(this, Uri.parse(videoSrc));
mediaPlayer.setScreenOnWhilePlaying(true);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
if(firstCall) {
mediaPlayer.start();
firstCall = false;
}
new AsyncPrepareNext().execute();
}
});
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
next.start();
}
});
}
catch(Exception ex) {
}
}
private void prepareNext() {
try {
next = new MediaPlayer();
next.setDisplay(surfaceHolder);
next.setDataSource(this, Uri.parse(secondVideoSrc));
next.setScreenOnWhilePlaying(true);
next.prepareAsync();
next.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
}
});
next.setAudioStreamType(AudioManager.STREAM_MUSIC);
next.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
}
});
}
catch(Exception ex) {
}
}
private class AsyncPrepareNext extends AsyncTask<Void,Void,Void> {
#Override
protected Void doInBackground(Void... params) {
prepareNext();
return null;
}
}
}
When i run this first video starts for 2 or 3 seconds and then stop with some errors like media server died, audio flinger died, error (100,0), error (38,0)
You'll probably need to implement the OnPreparedListener() and OnCompletedListener() to accomplish this and you might need 2 media players for it as well.
#Override
public void onPrepared(MediaPlayer mp){
// Set Video Source for second video
mp.setDataSource(path);
// Prepare second video
mp.prepareAsync();
}
#Override
public void onCompletion(MediaPlayer mp){
// Start second video here
mp.start();
}
and set them like this:
mediaPlayer.setOnPreparedListener(new OnPreparedListener());
mediaPlayer.setOnCompletedListener(new OnCompletionListener());

Categories

Resources