为什么Android媒体播放器在Samsung Galaxy Tab上无法正常工作?

2lpgd968  于 2023-05-12  发布在  Android
关注(0)|答案(1)|浏览(156)

我写了一个应用程序,当你移动你的手机像一个自行车比赛,然后它会检测到运动,并会选择一个随机的声音gare,然后播放它。它在HTC上运行良好。
但是当我尝试在三星Galaxy Tab上运行它时,它无法正常工作。它开始播放声音,但当我尝试移动它,然后它播放gare声音,然后只是沉默?不播放Idle bike声音?我发现有一个错误(38,0)。我尽了最大的努力想找到解决办法,但失败了。
我在res/raw/目录下使用.mp3回合。接下来我可以尝试什么?

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

ws51t4hk1#

经过长时间的挣扎我发现我是由于忘记了
. println();
我将代码更新为

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

相关问题