0

我已经阅读了几个关于在 android 中播放音乐的示例,并且我使用异步线程开发了它。一切似乎都很好,但是当我进入活动时,声音没有播放。但是,如果我以简单的方式(在评论之间)这样做,声音就会播放。

package net.sgoliver.android.listviewopt;

import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Detector extends Activity implements SensorEventListener, OnPreparedListener,      OnCompletionListener {
  private SensorManager mSensorManager;
  private Sensor mAccelerometer;
  private MediaPlayer mediaPlayer;

  TextView title,tv,tv1,tv2, txtNombre;      
  LinearLayout layout;


  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detector);

    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    //get layout
    layout = (LinearLayout)findViewById(R.id.linear);

    //get textviews
    title=(TextView)findViewById(R.id.name);   
    tv=(TextView)findViewById(R.id.xval);
    tv1=(TextView)findViewById(R.id.yval);
    tv2=(TextView)findViewById(R.id.zval);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnPreparedListener(this);
    mediaPlayer.setOnCompletionListener(this);

    try {
        mediaPlayer.setDataSource(getResources().getResourceName(R.raw.song));
    } 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 (NotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mediaPlayer.setLooping(true);

    mediaPlayer.prepareAsync();


    /** This way it works perfect 
    MediaPlayer mediaPlayer;                 
    mediaPlayer = MediaPlayer.create(this,R.raw.song);
    mediaPlayer.setLooping(true);        
    mediaPlayer.setVolume(100,100);          
    mediaPlayer.start(); **/

  //Localizar los controles
    txtNombre  = (TextView)findViewById(R.id.nombre);

    //Recuperamos la información pasada en el intent
    Bundle bundle = this.getIntent().getExtras();

    //Construimos el mensaje a mostrar
    txtNombre.setText("" + bundle.getString("NOMBRE"));
    txtNombre.setVisibility(View.GONE);
  }

  @Override
  public final void onAccuracyChanged(Sensor sensor, int accuracy)
  {
    // Do something here if sensor accuracy changes.
  }

  @Override
  public final void onSensorChanged(SensorEvent event) 
  {
    // Many sensors return 3 values, one for each axis.
    float x =  event.values[0];
    float y =  event.values[1];
    float z =  event.values[2];


    //display values using TextView
    title.setText(R.string.app_name);
    tv.setText("X axis" +"\t\t"+x);
    tv1.setText("Y axis" + "\t\t" +y);
    tv2.setText("Z axis" +"\t\t" +z);

    if(y<3.0){
        txtNombre.setVisibility(View.VISIBLE);
    }else{
        txtNombre.setVisibility(View.INVISIBLE);
    }

  }

  @Override
  protected void onResume()
  {
    super.onResume();
    mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
  }

  @Override
  protected void onPause() {
    super.onPause();
    mSensorManager.unregisterListener(this);
  }

  @Override
    public void onPrepared(MediaPlayer mp) {
        mp.start();//Cuando acaba de cargar inicia la reproducción
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        mp = null;//Para que cuando pulses de nuevo el botón play, no cree otro reproductor, eliminamos el actual
    }


}

提前致谢。

4

0 回答 0