我有一个应用程序,当你像自行车比赛那样移动你的手机时,它会检测到动作并会选择一个随机的gare然后播放它。它在HTC上运作良好。但是当我尝试在三星Galaxy Tab上运行它时,它无法正常工作。它在开始时播放声音,但是当我尝试移动它时,它会播放gare声音,然后只是沉默?不玩空闲自行车的声音?我发现有一个erroe(38,0)。我尽力找到解决方案,但失败了。
我在res / raw /目录中使用.mp3 round。 任何人都可以提供帮助吗?
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 ) ;
}
}
答案 0 :(得分:0)
经过长时间的斗争,我发现我忘记了
PhoneBike.player.prepare();
我将代码更新为
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 ( ) ;
}
}