Playing 6 videos in 6 Textureview - android

I would really appreciate if you can help me with this problem:
How can i play 6 videos in 6 different Textureviews using 6 different MediaPlayers.
I'm using the next code(which comes from Playing video on TextureView)
public class MediaPlayerDemo_Video extends Activity implements TextureView.SurfaceTextureListener {
private MediaPlayer mMediaPlayer;
private TextureView mPreview;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
mPreview = new TextureView(this);
mPreview.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
mPreview.setSurfaceTextureListener(this);
extras = getIntent().getExtras();
setContentView(mPreview);
}
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Surface s = new Surface(surface);
try {
mMediaPlayer= new MediaPlayer();
mMediaPlayer.setDataSource("http://daily3gp.com/vids/747.3gp");
mMediaPlayer.setSurface(s);
mMediaPlayer.prepare();
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnVideoSizeChangedListener(this);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.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();
}
}

Related

Applying TextureView to MediaPlayer after MediaPlayer.prepare causes black screen

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 :)

TextureView playing video turns black after onPause

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.

playing sdcard video on textureview

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.

Error - Class Equalizer - Methods getBand() and getBandFreqRange(short band)

I don't know more what I can do to solve these problems. I am
implementing a MediaPlayer + Equalizer app. My MediaPlayer works fine,
however, I am having problem with my equalizer: some its methods don't
work.
public class MainActivity extends Activity {
private MediaPlayer mediaPlayer;
private Equalizer equalizer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource("/sdcard/titanium.mp3");
mediaPlayer.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();
}
mediaPlayer.start();
equalizer = new Equalizer(0, 0);
equalizer.setEnabled(true);
int minEQLevel = equalizer.getBandLevelRange()[0];
int maxEQLevel = equalizer.getBandLevelRange()[1];
Log.i("kkk", "Valor de minEQLevel" + minEQLevel);
Log.i("kkk", "Valor de maxEQLevel" + maxEQLevel);
short numberOfBand = equalizer.getNumberOfBands();
Log.i("kkk", "NĂºmero de bandas = "+numberOfBand);
/******From here to down the methods don't work *****/
try {
short band = equalizer.getBand(Integer.valueOf(50000));
Log.i("kkk", "getBand() = "+band);
short band = 0;
//int menor = equalizer.getBandFreqRange(band)[0];
//int maior = equalizer.getBandFreqRange(band)[1];
//Log.i("kkk", "Menor = "+menor);
//Log.i("kkk", "Maior = "+maior);
} catch (IllegalArgumentException e) {
Log.i("kkk", "Bad parameter value");
} catch (UnsupportedOperationException e) {
Log.i("kkk", "get parameter() rejected");
} catch (IllegalStateException e) {
Log.i("kkk", "get parameter() called in wrong state");
}
}
}
The follow methods work normally:
getBandLevelRange();
getNumberOfBands();
The follow methods don't work. I get the message error: Bad parameter
value.
getBand();
getBandFreqRange(); this last is commented, but when it isn't also don't work.
In the getBand() method I already tried several parameters values like: 50000, 1000000. Frequency supported at example of the library AudioFx.
Why don't these methods work??? Did I forgive to setup anything???
tks.

TextureView onSurfaceTextureAvailable never called

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>

Categories

Resources