Aoa. I'm facing a problem with androird surfaceView.. I'm trying to animate a ball by changing its x,y coordinates. But ball is disappearing... Please see the code and identify the issue,
regards.`
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(tile1, axisX, axisY,null); //LINE ONE objects
canvas.drawBitmap(tile2, axisX+78, axisY,null);
canvas.drawBitmap(tile3, axisX+156, axisY,null);
canvas.drawBitmap(tile4, axisX+234, axisY,null);
canvas.drawBitmap(movingStick, (mWidth-movingStick.getWidth())/2, mHeight-200,null);
canvas.drawBitmap(mBall, balCurrPosX,balCurrPosY ,null);
}
//thread code
Thread t=new Thread(new Runnable() {
private boolean moveUp = true;
#Override
public void run() {
// TODO Auto-generated method stub
try{
while (true) {
if (balCurrPosY >= mHeight){
moveUp= false;
}else if(balCurrPosX<=0){
moveUp = true;
}
if(moveUp)
balCurrPosY--;
else
balCurrPosY++;
Thread.sleep(2000);
postInvalidate();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});`
You forgot to start your new Thread at the end of your code :
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
Related
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.
Hi have a progressBar that i run. but when the user exits the app via the home button and then re-enters it by holding down the home button and selected the app. The progressBar setProgress flickers between an old value and a new one almost as if its remembering the previous state. I have created a function in my on pause/onResume methods to pause the timer save the values and on resume get those values and start the timer and progress bar again. The confusing thing is it works the onPause stuff works perfectly if the onPause function is run and the user stays in the app for example presses the back button. Does anyone know where im going wrong or how to correctly implement a progressBar using a CountDownTimer?
heres my code
#Override
protected void onResume() {
// TODO Auto-generated method stub
Log.v("Game","ONRESUME");
loadData();
layoutChecks();
loadViews();
try {
if(!myData.get("completed").equals("3")){
hintCheck();
getHints();
startTimer();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onResume();
}
#Override
protected void onPause() {
Log.v("Game","ONPAUSE");
try {
if(!myData.get("completed").equals("3")){
pauseTimer();
updateData();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onPause();
}
public void loadViews(){
progressBar = new ProgressBar(this);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressD = progressBar.getProgressDrawable();
}
public void prepareProgressBar(){
progressD.setLevel(0);
progressBar.setProgress(0);
progressD.setLevel(points*100);
progressBar.setProgress(points);
}
public void startTimer(){
try {
points = Integer.valueOf(myData.getJSONObject("Data").getString("points"));
int timer = points;
hasTimerCancelled = false;
prepareProgressBar();
counter = new MyCount(timer/2 * 1000,1000);
counter.start();
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public void pauseTimer(){
try {
counter.cancel();
hasTimerCancelled = true;
JSONObject Data = myData.getJSONObject("Data");
Data.put("points", String.valueOf(points));
myData.put("Data", Data);
updateData();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class MyCount extends CountDownTimer{
public long totaltime;
public MyCount(long totalTime, long countDownInterval) {
super(totalTime, countDownInterval);
totaltime = totalTime;
}
#Override
public void onFinish() {
counter.cancel();
points = 0;
progressBar.setProgress(0);
}
#Override
public void onTick(long millisUntilFinished) {
if(!hasTimerCancelled){
if(points > 0){
points = points - 1;
}
progressBar.setProgress((int) totaltime);
}
}
}
I am trying to run audios one after another with a time gap of 5 seconds. However I dont really see that happening. My third audio in the list is played and others are skipped. This means while debugging only music3 is being played and others are not. I would love alternate methods of doing this. Moreover I used prepare method just like that. Makes no difference while running though.
for(int j=0;j<3;j++)
{
if(j==0)
{
xnp = MediaPlayer.create(this,R.raw.ticktock);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
try {
xnp.prepare();
xnp.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 5000);
xnp.stop();
}
if(j==1)
{
xnp = MediaPlayer.create(this,R.raw.music2);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
try {
xnp.prepare();
xnp.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 5000);
xnp.stop();
}
else if (j==2)
{
xnp = MediaPlayer.create(this,R.raw.music3);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
try {
xnp.prepare();
xnp.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 5000);
xnp.stop();
intenter();
}
}
Your current implementation does not work because the media playback is not done on the main thread, so you are immediately playing all 3 media resources, so only the third one is heard.
I think you might want to call MediaPlayer#setOnCompletionListener on your MediaPlayer object. In the completion listener, you can then postDelayed onto your Handler to queue up the next resource to play.
I have a media player service that plays a music in background, and this service is called from an activity. Now when i ex. exit from that activity, and get back to it again, i want to see what is the status, what song is played etc.
Here is the code from my service:
public class MediaPlayerService extends Service implements
OnCompletionListener, OnClickListener {
MediaPlayer mediaPlayer;
String url;
int mediaPos, mediaMax, position;
boolean isShufle;
WeakReference<SeekBar> seekbar;
WeakReference<TextView> reciter, songTitle, songPos;
private WeakReference<ImageView> play, forward, backward;
ArrayList<Songs> songs;
Handler handler;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
// mediaPlayer = MediaPlayer.create(this,url);// raw/s.mp3
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
private void play(final int position) throws IllegalArgumentException,
SecurityException, IllegalStateException, IOException {
/*
* dialog = ProgressDialog.show(AlbumDetails.this, "",
* getString(com.darkovski.quran.R.string.buffering), true);
* dialog.setCancelable(true); dialog.show();
*/
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(songs.get(position).getLink());
// prepare track
mediaPlayer.prepare();
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
play.get().setImageResource(R.drawable.pause);
// play.setImageResource(R.drawable.pause);
// this is new
mediaPos = mp.getCurrentPosition();
mediaMax = mp.getDuration();
reciter.get().setText(songs.get(position).getRecitorName());
songTitle.get().setText(songs.get(position).getTitle());
songPos.get().setText((position + 1) + " of 114");
seekbar.get().setMax(mediaMax);
seekbar.get().setProgress(mediaPos);
handler.removeCallbacks(moveSeekBarThread);
handler.postDelayed(moveSeekBarThread, 100);
mp.start();
// dialog.dismiss();
}
});
// when truck finishes
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// if(shuffle)
try {
if (isShufle) {
play(new Random().nextInt(songs.size()));
} else {
if (position == songs.size())
play(0);
else
play(position + 1);
}
AlbumDetails.position += 1;
} 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();
}
}
});
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
url = intent.getStringExtra("url");
handler = new Handler();
position = intent.getIntExtra("position", -1);
forward = new WeakReference<ImageView>(AlbumDetails.forward);
backward = new WeakReference<ImageView>(AlbumDetails.backward);
play = new WeakReference<ImageView>(AlbumDetails.play);
play.get().setOnClickListener(this);
forward.get().setOnClickListener(this);
backward.get().setOnClickListener(this);
seekbar = new WeakReference<SeekBar>(AlbumDetails.seekbar);
reciter = new WeakReference<TextView>(AlbumDetails.reciter);
songTitle = new WeakReference<TextView>(AlbumDetails.songTitle);
songPos = new WeakReference<TextView>(AlbumDetails.songPos);
songs = AlbumDetails.songs;
seekbar.get().setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (fromUser && mediaPlayer.isPlaying()) {
mediaPlayer.seekTo(progress);
}
}
});
/*
* try { mediaPlayer.setDataSource(url); mediaPlayer.prepare();
* mediaPlayer.setOnCompletionListener(this); } 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(); } if (!mediaPlayer.isPlaying()) {
* mediaPlayer.start(); }
*/
try {
play(position);
} 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();
}
return START_STICKY;
}
public void onDestroy() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
}
public void onCompletion(MediaPlayer _mediaPlayer) {
stopSelf();
}
private Runnable moveSeekBarThread = new Runnable() {
public void run() {
if (mediaPlayer.isPlaying()) {
int mediaPos_new = mediaPlayer.getCurrentPosition();
int mediaMax_new = mediaPlayer.getDuration();
seekbar.get().setMax(mediaMax_new);
seekbar.get().setProgress(mediaPos_new);
handler.postDelayed(this, 100);
}
}
};
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.playeer_back:
try {
if (mediaPlayer != null)
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
}
if (position - 1 < 0) {
play(songs.size());
position = songs.size();
} else
play(position - 1);
} catch (IllegalArgumentException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (SecurityException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IllegalStateException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
break;
case R.id.playeer_forward:
try {
if (mediaPlayer != null)
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
}
play(position + 1);
position += 1;
} 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();
}
break;
case R.id.playeer_play:
if (mediaPlayer == null) {
try {
play(position);
} catch (IllegalArgumentException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SecurityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
play.get().setImageResource(R.drawable.pause);
} else if (mediaPlayer != null) {
mediaPlayer.start();
play.get().setImageResource(R.drawable.play_play);
}
// playFunction();
break;
}
}
}
And my activity code - onClick:
case R.id.playeer_play:
playbackServiceIntent
.putExtra("url", songs.get(position).getLink());
playbackServiceIntent
.putExtra("position", position);
startService(playbackServiceIntent);
break;
There are different methods to realize it.
Using LocalBroadcastManager
Using an application to track the status of your service, i.e Song name, length, ...
Using SharedPreferences
IMHO, I think using a LocalBroadcastManager is more cleaner.
- Send a broadcast message to the service
- When the service receives the message, let it broadcast its status.
I made a script which detects if there's internet connection:
public static boolean isOnline() {
try {
InetAddress.getByName("google.hu").isReachable(3);
return true;
} catch (UnknownHostException e){
return false;
} catch (IOException e){
return false;
}
}
If there's no internet the app will warn the user and the quit! But how to delay super.onBackPressed for 20 seconds? :)
this.toast1 = new Toast(getApplicationContext());
toast1.setGravity(Gravity.CENTER, 0, 0);
toast1.setDuration(20000);
toast1.setView(layout);
toast1.show();
super.onBackPressed();
Thread delayThread= new Thread(new Runnable()
{
#Override
public void run()
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
activityInstance.this.runOnUiThread(new Runnable()
{
#Override
public void run()
{
super.onBackPressed();
}
});
}
});
delayThread.start();
The easiest is to create a Handler in onCreate()and use postDelayed() with a Runnable that calls super.onBackPressed()