如何在android中播放声音

时间:2015-08-13 10:00:46

标签: android android-mediaplayer

我希望在应用程序启动时在我的应用程序中播放音乐。我尝试了很多代码,但没有任何代码可以帮助我,我没有收到任何错误。有人可以帮我解决这个问题。谢谢你。

public class Login extends Activity {

EditText edName, edPassword;
String userName,password;
MediaPlayer mp;


@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    mp = MediaPlayer.create(getApplicationContext(), R.raw.startsound);

    ActionBar actionBar = getActionBar();
    actionBar.hide();

    edName = (EditText) findViewById(R.id.editText1);
    edPassword = (EditText) findViewById(R.id.editText2);
    mp.start(); 
} 
public void SignInClick(View V) {

    userName = edName.getText().toString();
    password = edPassword.getText().toString();
    if (userName.equals("")) {
        Toast.makeText(Login.this, "Username is empty", Toast.LENGTH_LONG).show();}
    else if (password.equals("")) {
        Toast.makeText(Login.this, "Password is empty", Toast.LENGTH_LONG).show();
    }
  else  {
    Intent intent=new Intent(Login.this,Home.class); 
    startActivity(intent); 
    }       

}

1 个答案:

答案 0 :(得分:0)

希望这有帮助,并且应该是一个有效的例子,尽管我没有机会测试它。

public static Music theme;
theme = createMusic("menutheme.mp3");
theme.setLooping(true);
theme.setVolume(0.85f);
theme.play();




public Music createMusic(String filename) {
        try {
            AssetFileDescriptor assetDescriptor = assets.openFd(filename);
            return new AndroidMusic(assetDescriptor);
        } catch (IOException e) {
            throw new RuntimeException("Couldn't load music '" + filename + "'");
        }
    }


import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import com.matt.framework.Music;
import java.io.IOException;

public class AndroidMusic implements Music, OnCompletionListener, OnSeekCompleteListener, OnPreparedListener, OnVideoSizeChangedListener {
    MediaPlayer mediaPlayer;
    boolean isPrepared = false;

    public AndroidMusic(AssetFileDescriptor assetDescriptor) {
        mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(assetDescriptor.getFileDescriptor(),
                    assetDescriptor.getStartOffset(),
                    assetDescriptor.getLength());
            mediaPlayer.prepare();
            isPrepared = true;
            mediaPlayer.setOnCompletionListener(this);
            mediaPlayer.setOnSeekCompleteListener(this);
            mediaPlayer.setOnPreparedListener(this);
            mediaPlayer.setOnVideoSizeChangedListener(this);

        } catch (Exception e) {
            throw new RuntimeException("Couldn't load music");
        }
    }

    @Override
    public void dispose() {

        if (this.mediaPlayer.isPlaying()){
            this.mediaPlayer.stop();
        }
        this.mediaPlayer.release();
    }

    @Override
    public boolean isLooping() {
        return mediaPlayer.isLooping();
    }

    @Override
    public boolean isPlaying() {
        return this.mediaPlayer.isPlaying();
    }

    @Override
    public boolean isStopped() {
        return !isPrepared;
    }

    @Override
    public void pause() {
        if (this.mediaPlayer.isPlaying())
            mediaPlayer.pause();
    }



    @Override
    public void play() {
        if (this.mediaPlayer.isPlaying())
            return;

        try {
            synchronized (this) {
                if (!isPrepared)
                    mediaPlayer.prepare();
                mediaPlayer.start();
            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setLooping(boolean isLooping) {
        mediaPlayer.setLooping(isLooping);
    }

    @Override
    public void setVolume(float volume) {
        mediaPlayer.setVolume(volume, volume);
    }

    @Override
    public void stop() {
        if (this.mediaPlayer.isPlaying() == true){
            this.mediaPlayer.stop();

            synchronized (this) {
                isPrepared = false;
            }}
    }

    @Override
    public void onCompletion(MediaPlayer player) {
        synchronized (this) {
            isPrepared = false;
        }
    }

    @Override
    public void seekBegin() {
        mediaPlayer.seekTo(0);

    }


    @Override
    public void onPrepared(MediaPlayer player) {
        // TODO Auto-generated method stub
        synchronized (this) {
            isPrepared = true;
        }

    }

    @Override
    public void onSeekComplete(MediaPlayer player) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onVideoSizeChanged(MediaPlayer player, int width, int height) {
        // TODO Auto-generated method stub

    }
}