为什么 Android 媒体播放器在 Samsung Galaxy Tab 上无法正常工作?
我写了一个应用程序,当你像自行车比赛一样移动手机时,它会检测到运动并选择随机的车站声音然后播放。在 HTC 上运行良好。
但当我尝试在 Samsung Galaxy Tab 上运行它时,它无法正常工作。它在开始时播放声音,但当我尝试移动它时,它会播放齿轮声音,然后就安静了?不播放闲置自行车声音?我发现有错误(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 ) ;
}
}
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 ) ;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过长时间的挣扎,我发现我是因为忘记了
我将代码更新为
After a long struggle I found I was due to forgetting of
I update code as