I have tried so many answer but there is no success. I want to play video after it loaded from remote URL. What is frustrating is Toast is showing but
video doesn't play.
Code
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
Log.i(TAG,"onPrepared");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
videoView.start();
Toast.makeText(mContext,"start video",Toast.LENGTH_LONG).show();
}
},100);
}
});
xml
<VideoView
android:id="#+id/video_view"
android:layout_width="410dp"
android:layout_height="307.50dp"
/>
Related
I want to hide ImageView Thumbnail after Remote Video ready to play, means when onPrepared execute but imageView.setVisibility(View.GONE) doesn't works at all.
I have seen this answers one, two and I think cause is SurfaceView or VideoView. as per answers
I have tried using both MediaPlayer and VideoView
code using MediaPlayer and SurfaceView
mMediaPlayer = new MediaPlayer();
holder.surfaceView.setDrawingCacheEnabled(true);
try {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(mContext, Uri.parse(video_url));
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
((Activity)mContext).runOnUiThread(new Runnable() {
#Override
public void run() {
holder.imgThumbnail.getParent().requestTransparentRegion(holder.imgThumbnail);
holder.imgThumbnail.setVisibility(View.GONE);
holder.imgThumbnail.getParent().requestTransparentRegion(holder.imgThumbnail);
}
});
Toast.makeText(mContext,"onPrepared",Toast.LENGTH_LONG).show();
}
});
} catch (IllegalArgumentException | SecurityException | IllegalStateException | IOException e) {
e.printStackTrace();
}
mMediaPlayer.prepareAsync();
XML of SurfaceView
<RelativeLayout
android:layout_width="410dp"
android:id="#+id/v_view"
android:visibility="visible"
android:layout_height="307.50dp">
<SurfaceView
android:id="#+id/video_view"
android:layout_width="410dp"
android:layout_height="307.50dp"
/>
<ImageView
android:id="#+id/imgThumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:scaleType="centerCrop" />
</RelativeLayout>
code using VideoView
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
Log.i(TAG,"onPrepared");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
videoView.start();
Toast.makeText(mContext,"start video",Toast.LENGTH_LONG).show();
((Activity)mContext).runOnUiThread(new Runnable() {
#Override
public void run() {
holder.imgThumbnail.getParent().
requestTransparentRegion(holder.imgThumbnail);
holder.imgThumbnail.setVisibility(View.GONE);
holder.imgThumbnail.getParent().
requestTransparentRegion(holder.imgThumbnail);
}
});
}
},100);
}
});
XML of VideoView
<RelativeLayout
android:layout_width="410dp"
android:id="#+id/v_view"
android:visibility="visible"
android:layout_height="307.50dp">
<VideoView
android:id="#+id/video_view"
android:layout_width="410dp"
android:layout_height="307.50dp"
/>
<ImageView
android:id="#+id/imgThumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:background="#color/back_orange"
android:scaleType="centerCrop" />
</RelativeLayout>
Please help! I'm stuck
i am making a phone dialer app that play the sound when you press on button the app is working great but after a while i get a random crash because on prepared listener i do not know what is happening and this is the error
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setOnPreparedListener(android.media.MediaPlayer$OnPreparedListener)' on a null object reference
at com.alper.pola.andoid.phonedailer.MainActivity$1.onTouch(MainActivity.java:258)
and this is how i cast the media player
if (mLastButton == one) {
MediaPlayer mediaPlayer ;
mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.one);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp1.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mp1.release();
}
});
}
if (mLastButton == two) {
MediaPlayer mediaPlayer ;
mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.two);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp2.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mp2.release();
}
});
}
if (mLastButton == three) {
MediaPlayer mediaPlayer ;
mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.three);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp3.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mp3.release();
}
});
}
if (mLastButton == four) {
MediaPlayer mediaPlayer ;
mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.four);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp4.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mp4.release();
}
});
}
the app is working fine and the same button when i click on it is working but after while the button stop working and i get this crash with every button it start with working fine but after i type a few numbers the app crash
You are using a very inefficient way while dealing with MediaPlayer. Still I'll help you a workaround with this code. Use mediaPlayer.prepare() to initiate the call. Checkout this code:
if (mLastButton == one) {
mp1 = MediaPlayer.create(MainActivity.this,R.raw.one);
mp1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp1.start();
});
mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mp1.release();
}
});
mp1.prepare();
}
Do similarly for others. There are much better way to do this but as with your code, this is shortest.
I am trying to find out how to use the android's MediaPlayer method setNextMediaPlayer which should smoothly transition from one player (song) to another. But do not know how to use the method since there is a lack of documentation.
This is what i do and it does not work:
final MediaPlayer mp1 = new MediaPlayer();
final MediaPlayer mp2 = new MediaPlayer();
try {
mp1.setDataSource("http://song1.MP3");
mp2.setDataSource("http://song2.mp3");
mp1.prepareAsync();
mp1.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp1.start();
}
});
mp1.setNextMediaPlayer(mp2);
mp1.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp2.prepareAsync();
mp2.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp2.start();
}
});
}
});
} catch (IllegalArgumentException e) {}
So the first song plays. But after it finishes the second does not start.
Got it to work.
mp1.setDataSource(song1);
mp1.prepareAsync();
mp1.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp1.start();
}
});
//mp1.start();
//mp1.seekTo(1000*100);
mp2.setDataSource(song2);
mp2.prepare();
mp1.setNextMediaPlayer(mp2);
The only problem is that the second mediaplayer cannot be called as async (i.e. mp2.prepareAsync()). I do not know why, but if you try it like that it does not start, which is a bad thing :/.
MediaPlayer setNextMediaPlayer gives a lot of problem what i do is this:
in the onCompletion() reset() the mediaplayer, setDataSource() and prepareAsync().
Something like this
mp1.setDataSource("http://song1.MP3");
mp1.prepareAsync();
mp1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp1.start();
}
});
mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp1.reset();
//ADD FLAG FOR STOP
try {
mp1.setDataSource("http://song2.mp3");
} catch (IOException e) {
e.printStackTrace();
}
mp1.prepareAsync();
}
});
Then in the onCompletion you need some flag to check if the last song was played to stop, because the code as i posted will loop in the last song.
There is a problem in the following media player that plays the audio stream when you press play if the stream is not available there is no any response from the program, How can I solve this issue, try to make progress dialog, but the solution does not work, maybe I did something wrong, but when you click on the progress bar appears first and then immediately disappears without waiting to start playback of music.
setDataSource filed in advance a broken link, nothing has changed.
Code media player with progress dialog:
private class ProgressTask extends AsyncTask <Void,Void,Void>{
#Override
protected void onPreExecute(){
progressbar.setVisibility(View.VISIBLE);
}
#Override
protected Void doInBackground(Void... arg0) {
preload();
return null;
}
#Override
protected void onPostExecute(Void result) {
MPplay();
progressbar.setVisibility(View.GONE);
}
}
public void preload() {
releaseMP();
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
if(pdaStream.isChecked()){
Main_stream = "http://145.255.233.204:58000/radio_pda";
}else{
Main_stream = "http://145.255.233.204:58000/radio";
}
mediaPlayer.setDataSource(Main_stream);
} catch (IOException e) {
e.printStackTrace();
}
Log.d(LOG_TAG, "prepareAsync");
mediaPlayer.prepareAsync();;
}
public void MPplay(){
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
}
If you want to see when a video is ready for playback, use this callback. http://developer.android.com/reference/android/media/MediaPlayer.OnPreparedListener.html
onPrepared(MediaPlayer mp)
//Called when the media file is ready for playback.
If you would like to see when a video is 100% buffered, use this callback: http://developer.android.com/reference/android/media/MediaPlayer.OnBufferingUpdateListener.html
public abstract void onBufferingUpdate (MediaPlayer mp, int percent)
I am trying to build an android app that will loop a video!
The problem is that it never loops! It plays the video only once!
During debugging i realized that the "myVideoView.setOnCompletionListener" is being executed but the video doesn't play!
I also try "mp.reset()" inside the CompletionListener.
Maybe i am missing something in a different file, such as the Manifest?
Any thoughts?
Here is my code:
final VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
myVideoView.setVideoURI(Uri.parse(SrcPath));
myVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer arg0) {
myVideoView.requestFocus();
myVideoView.start();
}
});
myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
myVideoView.setVideoURI(Uri.parse(SrcPath));
}
});
Try onPreparedListener instead of onCompletionListener:
myVideoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
Hope this helps.
Try this.
myVideoView = (VideoView) findViewById(R.id.videoView1);
myVideoView.setVideoPath(video_path);
myVideoView.setMediaController(new MediaController(this));
myVideoView.start();
myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
myVideoView.start();
}
});
By that after completion of your video start again...
Try this.
videoView.setVideoPath(tempFileMessageUri.toString());
ctlr = new MediaController(context);
ctlr.setMediaPlayer(videoView);
videoView.setMediaController(ctlr);
videoView.requestFocus();
videoView.start();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
videoView.start();
}
});
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
videoView.start();
}
});