TextureView onSurfaceTextureAvailable never called - android

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>

Related

Playing 6 videos in 6 Textureview

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();
}
}

cannot play the selected music after clicking the "ok" button of Mediastore

In this app, it can play the default music well, but when I try to use MediaStore to select another music to play, something wrong here. In the page of mediastore, it can play the selected music well, but when I click the "ok" button to confirm my choice, it cannot play the music anymore. I do not know why this happened and I am very glad to have your help!
The following is the code
public class MUSIC extends AppCompatActivity implements SensorEventListener,MediaPlayer.OnPreparedListener,GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,LocationListener {
private Button JoggingButton,BikingButton;
private SensorManager mSensormanager;
private Sensor accelerometer;
private final float NOISE = (float) 2.0;
private boolean initialization;
private float mLastX, mLastY, mLastZ;
private static final String TAG = "sensor";
private MediaPlayer mediaPlayer1;
private MediaPlayer mediaPlayer2;
private double measurement;
private double speed;
private boolean jogging=false,biking=false;
private Uri urijogging,uribiking;
private LocationManager locationManager;
private GoogleApiClient googleApiClient;
private LocationRequest locationRequest;
private final static int MY_PERMISSION_REQUEST_FINE=100;
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST=9000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music);
mediaPlayer1 = new MediaPlayer();
mediaPlayer2 = new MediaPlayer();
JoggingButton=(Button)findViewById(R.id.button3);
BikingButton=(Button)findViewById(R.id.button4);
if(accelerometer!=null){
if(urijogging==null){
Prepareplay(mediaPlayer1,Uri.parse("android.resource://"+getPackageName()+ "/raw/jogging1"));
}else{
Prepareplay(mediaPlayer1,urijogging);
}
if(uribiking==null){
Prepareplay(mediaPlayer2,Uri.parse("android.resource://"+getPackageName()+ "/raw/biking1"));
}else{
Prepareplay(mediaPlayer2,uribiking);
}
mediaPlayer1.start();
mediaPlayer2.start();
JoggingButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mediaPlayer1.release();
mediaPlayer1=new MediaPlayer();
Intent intent=new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 10);
}
});
BikingButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mediaPlayer2.release();
mediaPlayer2=new MediaPlayer();
Intent intent=new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 11);
}
});
Toast.makeText(getApplicationContext(), "Current speed:"+String.valueOf(speed),Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),"Sorry,there are no accelerometers on your device",Toast.LENGTH_LONG).show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK && requestCode == 10){
urijogging=data.getData();
Toast.makeText(getApplicationContext(),String.valueOf(urijogging),Toast.LENGTH_LONG).show();
Joggingplay(this,urijogging);
}
if(resultCode == RESULT_OK && requestCode == 11){
uribiking=data.getData();
Toast.makeText(getApplicationContext(),String.valueOf(uribiking),Toast.LENGTH_LONG).show();
Bikingplay(this,uribiking);
}
}
private void Joggingplay(Context context, Uri uri) {
try {
mediaPlayer1.setDataSource(context, uri);
mediaPlayer1.setOnPreparedListener(this);
mediaPlayer1.prepareAsync();
onPrepared(mediaPlayer1);
mediaPlayer1.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) {
e.printStackTrace();
}
}
private void Bikingplay(Context context, Uri uri) {
try {
mediaPlayer2.setDataSource(context, uri);
mediaPlayer2.setOnPreparedListener(this);
mediaPlayer2.prepareAsync();
onPrepared(mediaPlayer2);
mediaPlayer2.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) {
e.printStackTrace();
}
}
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
mediaPlayer.pause();
}
public void Prepareplay(MediaPlayer player, Uri uri){
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
player.setDataSource(getApplicationContext(), uri);
} catch (IOException e) {
e.printStackTrace();
}
try {
player.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The following is the logcat
05-16 16:42:04.243 14390-14390/com.example.a12086.app_sensor E/MediaPlayer: pause called in state 1, mPlayer(0x0)
05-16 16:42:04.244 14390-14390/com.example.a12086.app_sensor E/MediaPlayer: error (-38, 0)
I searched this problem online, and I already used mediaPlayer.prepareAsync() and onPrepared(mediaPlayer), but it still do not work. And the selected music can only be played on the MediaStore page, I am confused about that...
try to Use: mdiaPlayer.reset();
mdiaPlayer.prepare();
mdiaPlayer.start(); instead of
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.prepareAsync();
onPrepared(mediaPlayer);
mediaPlayer.start();

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.

Categories

Resources