I have written a app, when you move your phone like a Bike race, then it will detect the motion and will choose a random voice of gare and then play it. It's working good on HTC.
But when I try to run it on Samsung Galaxy Tab, it is not working properly. It play sound at start but when I try move it, then it play gare sound and then just silent? Not play the Idle bike sound? I found that there is a error (38, 0). I try my best to find a solution but failed.
I am using .mp3 round in res/raw/
directory. What can I try next?
package com.my.phone.bike ;
import java.io.IOException ;
import java.util.Date ;
import java.util.Random ;
import android.app.Activity ;
import android.content.res.Configuration ;
import android.graphics.drawable.AnimationDrawable ;
import android.hardware.SensorListener ;
import android.hardware.SensorManager ;
import android.media.MediaPlayer ;
import android.media.MediaPlayer.OnCompletionListener ;
import android.os.Bundle ;
import android.util.DisplayMetrics ;
import android.widget.Button ;
public class PhoneBike extends Activity implements SensorListener {
private final int GAS_IDS[] = { R.raw.gas1 , R.raw.gas2 , R.raw.gas3 , R.raw.gas4 , R.raw.gas5 , R.raw.gas6 } ;
private static MediaPlayer player ;
final String tag = "MainAcivity" ;
SensorManager sm = null ;
Button btn ;
float start_z = - 1 ;
float ened_z = - 1 ;
private boolean is_started = false ;
private long time_started ;
private long time_ended ;
private long timeStemp = 750 ;
Date date ;
private Random random ;
AnimationDrawable frameAnimation ;
/** Called when the activity is first created. */
@ Override
public void onCreate ( Bundle icicle ) {
super.onCreate ( icicle ) ;
DisplayMetrics dpmat = new DisplayMetrics ( ) ;
getWindowManager ( ).getDefaultDisplay ( ).getMetrics ( dpmat ) ;
setContentView ( R.layout.main ) ;
sm = ( SensorManager ) getSystemService ( SENSOR_SERVICE ) ;
date = new Date ( ) ;
random = new Random ( ) ;
player = MediaPlayer.create ( getApplicationContext ( ) , R.raw.start ) ;
try {
player.prepare ( ) ;
} catch ( IllegalStateException e ) {
e.printStackTrace ( ) ;
} catch ( IOException e ) {
e.printStackTrace ( ) ;
}
player.start ( ) ;
player.setOnCompletionListener ( new test ( ) ) ;
}
public void onSensorChanged ( int sensor , float [ ] values ) {
synchronized ( this ) {
if ( sensor == SensorManager.SENSOR_ORIENTATION ) {
date = new Date ( ) ;
float temp = values [ 2 ] ;
if ( temp < 10 && temp > - 25 ) {
start_z = temp ;
is_started = true ;
time_started = date.getTime ( ) ;
}
if ( is_started == true ) {
if ( temp > 50 && temp < 90 ) {
ened_z = temp ;
time_ended = date.getTime ( ) ;
long times = time_ended - time_started ;
if ( times <= timeStemp ) {
int random_no = 0 + random.nextInt ( GAS_IDS.length - 1 ) ;
int ran_id = GAS_IDS [ random_no ] ;
final MediaPlayer temp_player = MediaPlayer.create ( getApplicationContext ( ) , ran_id ) ;
try {
temp_player.prepare ( ) ;
} catch ( IllegalStateException e ) {
e.printStackTrace ( ) ;
} catch ( IOException e ) {
e.printStackTrace ( ) ;
}
player.pause ( ) ;
temp_player.start ( ) ;
temp_player.setOnCompletionListener ( new OnCompletionListener ( ) {
@ Override
public void onCompletion ( MediaPlayer arg0 ) {
temp_player.stop ( ) ;
player.start ( ) ;
}
} ) ;
is_started = false ;
} else {
is_started = false ;
}
}
}
}
}
}
public void onAccuracyChanged ( int sensor , int accuracy ) {
}
@ Override
protected void onResume ( ) {
super.onResume ( ) ;
if ( PhoneBike.player != null ) {
PhoneBike.player.start ( ) ;
}
sm.registerListener ( this , SensorManager.SENSOR_ORIENTATION | SensorManager.SENSOR_ACCELEROMETER ,
SensorManager.SENSOR_DELAY_NORMAL ) ;
}
@ Override
protected void onStop ( ) {
sm.unregisterListener ( this ) ;
super.onStop ( ) ;
}
@ Override
protected void onPause ( ) {
if ( PhoneBike.player != null ) {
PhoneBike.player.pause ( ) ;
}
super.onPause ( ) ;
}
@ Override
protected void onDestroy ( ) {
if ( PhoneBike.player != null ) {
PhoneBike.player.stop ( ) ;
PhoneBike.player.release ( ) ;
PhoneBike.player = null ;
}
super.onDestroy ( ) ;
}
class test implements OnCompletionListener {
@ Override
public void onCompletion ( MediaPlayer player ) {
PhoneBike.player = MediaPlayer.create ( getApplicationContext ( ) , R.raw.idle ) ;
PhoneBike.player.setLooping ( true ) ;
PhoneBike.player.start ( ) ;
}
}
@ Override
public void onConfigurationChanged ( Configuration newConfig ) {
super.onConfigurationChanged ( newConfig ) ;
}
}
After a long struggle I found I was due to forgetting of
PhoneBike.player.prepare ( ) ;
I update code as
class Test implements OnCompletionListener {
@ Override
public void onCompletion ( MediaPlayer player ) {
PhoneBike.player = MediaPlayer.create ( getApplicationContext ( ) , R.raw.idle ) ;
PhoneBike.player.prepare ( ) ;
PhoneBike.player.setLooping ( true ) ;
PhoneBike.player.start ( ) ;
}
}