I'm trying to play a video that I have on the sdcard of my device using Textureview, which according to the documentation, is the best solution to further transform the video, the problem is that when I put the path of an Internet video me working correctly , and when I put my route sdcard me the video is black, but the sound if it works, is very rare. I attached the code if you can help me, I've been searching like crazy but I find it can be happening, thanks in advance. regards
public class tres extends Activity implements TextureView.SurfaceTextureListener {
private MediaPlayer mp;
private TextureView textureview;
private String Path = "video.mp4";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tres);
textureview = (TextureView) findViewById(R.id.vista_video);
textureview.setSurfaceTextureListener(this);
}
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width,
int height) {
// TODO Auto-generated method stub
Surface s = new Surface(surface);
try {
mp= new MediaPlayer();
mp.setDataSource(Environment.getExternalStorageDir ectory()+"/" + Path);
mp.setSurface(s);
mp.prepare();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
im having exact the same issue.
current workarround is a running webserver app on the same device and setting the DataSource of my mediaPlayer instance to localhost.
but thats not the solution im hoping to find.
Related
I'd like to switch the video method in MediaPlayer to TextureView's Surface at some point during playback. But all I get is a black screen (the sound is playing). I made sure the size of the TextureView is ok, so it's not the case.
If I set the Surface of TextureView before calling prepare on MediaPlayer, than the video is shown.
So it seems that the order of calling prepare and applying the surface is crucial.
What's interesting, in case of SurfaceView everything works fine (no matter when I set the holder of SurfaceView: before or after the preparing is done).
Is there a way to set TextureView to MediaPlayer during playback? Calling MediaPlayer.release() and recreating it again while having to take care of progress would be very inconvenient.
#Override
public void onSurfaceTextureAvailable(final SurfaceTexture surface, final int width, final int height) {
s = new Surface(surface);
Thread play = new Thread(new Runnable() {
#Override
public void run() {
try {
player = new MediaPlayer();
player.setSurface(s);
player.setDataSource(vidAddress);
player.prepare();
player.setOnPreparedListener(MainActivity.this);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnCompletionListener(MainActivity.this);
player.setScreenOnWhilePlaying(true);
Log.e("mediaplayer", "true");
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
play.start();
}
And your onPrepared listener like this
public void onPrepared(MediaPlayer mp) {
if (!player.isPlaying()) {
player.start();
}
}
setSurfaceTextureListener like this at onCreate
mTextureView.setSurfaceTextureListener(MainActivity.this);
I hope this will help :)
I am using a TextureView to play a video in a ListView. The TextureView itself works perfectly, however, if I press the home button and reenter the application a few times, the TextureView turns black (though the audio continues to play). If I exit and reenter again, the TextureView turns white (or maybe transparent, as white is the colour of my background).
Here is my code:
holder.instagramTextureView
.setSurfaceTextureListener(new SurfaceTextureListener() {
#Override
public void onSurfaceTextureUpdated(
SurfaceTexture surface) {
// TODO Auto-generated method stub
}
#Override
public void onSurfaceTextureSizeChanged(
SurfaceTexture surface, int width, int height) {
// TODO Auto-generated method stub
}
#Override
public boolean onSurfaceTextureDestroyed(
SurfaceTexture surface) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onSurfaceTextureAvailable(
SurfaceTexture surface, int width, int height) {
final Surface s = new Surface(surface);
try {
holder.mMediaPlayer = new MediaPlayer();
holder.mMediaPlayer.setDataSource(post
.getMedias().get(0)
.getMediaUrlVideomp4StandardRes());
holder.mMediaPlayer.setSurface(s);
holder.mMediaPlayer.prepare();
holder.instagramVideoVolume = 0f;
holder.mMediaPlayer.setVolume(
holder.instagramVideoVolume,
holder.instagramVideoVolume);
//holder.mMediaPlayer.setOnBufferingUpdateListener(this);
holder.mMediaPlayer
.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(
MediaPlayer mp) {
// To play:
mp.reset();
try {
mp.setDataSource(post
.getMedias()
.get(0)
.getMediaUrlVideomp4StandardRes());
mp.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch
// block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch
// block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch
// block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch
// block
e.printStackTrace();
}
// <--- Here comes a call to
// "To Resize" which is shown
// right above this code
mp.start();
}
});
// holder.mMediaPlayer.setOnPreparedListener(this);
//holder.mMediaPlayer.setOnVideoSizeChangedListener(this);
holder.mMediaPlayer
.setAudioStreamType(AudioManager.STREAM_MUSIC);
holder.mMediaPlayer.start();
holder.instagramTextureView
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (holder.instagramVideoVolume == 1) {
holder.instagramVideoVolume = 0f;
holder.mMediaPlayer
.setVolume(
holder.instagramVideoVolume,
holder.instagramVideoVolume);
} else {
holder.instagramVideoVolume = 1f;
holder.mMediaPlayer
.setVolume(
holder.instagramVideoVolume,
holder.instagramVideoVolume);
}
}
});
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Problem was making a new mediaplayer each time the surface is available. Not quite sure how this worked, but this was the problem. Instead, I create the mediaplayer outside of this listener.
From my research, textureView.getSurfaceTexture() returns null onResume(). What I did was reattach a listener to it in onResume().
TextureView textureView;
/*....
Usual stuff
*/
public void onPause(){
super.onPause();
//... destroy or disable image producer
}
public void onResume(){
super.onResume();
textureView.setSurfaceTextureListener(/*another listener*/);
}
Then, perform your usual things on onSurfaceTextureAvailable()
The black screen and audio playing happened with me when I did not release the MediaPlayer object.
I have added two buttons to my app, one to play and one to start. As soon as i start the app the songs starts playing automatically. when i press on stops it stops but when i press on play it doesnt starts again
To stop I have used mediaplayer.stop() which is working fine
To start I have used
mediaplayer.start()
which is not working
According to flow diagram its given we need to use
prepare()
and then
OnPreparedListener.onPrepared()
and then
start()
I don't know how to use these functions. Please help me
Don't call stop() for stopping the mediaplayer use reset() instead. I dont have good reason for that. But, it'll work for sure.
// for stopping it call below statement
if(mediaPlayer.isPlaying())
mediaPlayer.reset();
//for playing it again
mediaPlayer.prepare();
mediaPlayer.start();
For starting media player again use following code.
if (mediaplayer != null) {
mediaplayer.start();
}
and one more thing, instead of using
mediaplayer.stop()
use
mediaplayer.pause();
so it will pause the current song instead of stop.
Just check the condition whether it is null or not.
Try following code on Play Button
mp.reset();
mp.setDataSource(song path);
mp.prepare();
mp.start();
mp.setDataSource(song path); not necessary if you playing same sound again
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.singleitemview);
btnsound=(Button)findViewById(R.id.play_sound);
btnstopsound=(Button)findViewById(R.id.stop_sound);
mp=new MediaPlayer();
btnsound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
mp.setDataSource(Sound);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
mp.setLooping(true);
}
});
btnstopsound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.setLooping(false);
mp.stop();
}
});
}
I want to play an RTSP audio stream in Android media player. I was trying to directly bind to an RTSP link in media player. Sometimes it's playing fine, but now it didn't play and showed a media player error:
MediaPlayer(808): error (1, -2147483648).
Where is the problem?
Here is my some part of the code:
public class PlayRadio extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
setDataSource(rtspUrl);
Log.e("inside doinbackground....", path);
return null;
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
Toast.makeText(getApplicationContext(), "starting..", Toast.LENGTH_LONG).show();
}
});
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
}
private void setDataSource(String path2) {
// TODO Auto-generated method stub
player=new MediaPlayer();
try {
Uri a =Uri.parse("rtsp://stream.rtm.swiftserve.com/live/rtm/rtm-ch010");
player.setDataSource(getApplicationContext(),a);
player.prepareAsync();
}
catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Log.e("tempPath", tempPath);
}
This error MediaPlayer(808): error (1, -2147483648) simply tells that your the video file you are playing is not supported by your device.
Generally RTSP links uses .sdp format which is currently not supported by Android devices.
Please check here Supported Media Formats
I try to play a video for a splash Activity when my Android app starts and I have the problem that onSurfaceTextureAvailable interface method never get called.
Here is the code:
public class HomeActivity extends Activity implements TextureView.SurfaceTextureListener,
MediaPlayer.OnBufferingUpdateListener, OnCompletionListener,
MediaPlayer.OnPreparedListener, MediaPlayer.OnVideoSizeChangedListener {
private MediaPlayer videoMediaPlayer;
private TextureView videoPreview;
private Bundle extras;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
videoPreview = new TextureView(this);
videoPreview.setSurfaceTextureListener(this);
extras = getIntent().getExtras();
setContentView(videoPreview);
}
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Surface s = new Surface(surface);
Log.d("HOME", "onSurfaceTextureAvailable");
try {
videoMediaPlayer= new MediaPlayer();
Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.intro_video);
videoMediaPlayer.setDataSource(getApplicationContext(), video);
videoMediaPlayer.setSurface(s);
videoMediaPlayer.prepare();
videoMediaPlayer.setOnBufferingUpdateListener(this);
videoMediaPlayer.setOnCompletionListener(this);
videoMediaPlayer.setOnPreparedListener(this);
videoMediaPlayer.setOnVideoSizeChangedListener(this);
videoMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
videoMediaPlayer.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Other stuff
The code is from stackoverflow similar question which it seems that he got it working like this.
Any advice?
to onSurfaceTextureAvailable() be called, hardware acceleration must be enabled. you can do this in AndroidManifast.xml file
<activity
android:name="com.example.HomeActivity"
android:hardwareAccelerated="true" >
</activity>