-1

我使用这些行在服务和活动中使用媒体播放器播放一些音频文件,但我的设备上没有声音,可能是什么原因?我应该怎么做才能理解什么是错的并最终解决这个问题?

    MediaPlayer mp = MediaPlayer.create(this, R.raw.alert);
    mp.start();
4

3 回答 3

1

查看MediaPlayer 文档中的状态图。

创建后MediaPlayer它处于空闲状态。如您所见,您需要在调用 start() 之前对其进行初始化和准备。

于 2012-08-20T07:24:28.167 回答
1
Intent viewMediaIntent = new Intent();   
viewMediaIntent.setAction(android.content.Intent.ACTION_VIEW);   
File file = new File(objectFilePath);   
viewMediaIntent.setDataAndType(Uri.fromFile(file), "audio/*");   
viewMediaIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(viewMediaIntent);  

http://developer.android.com/reference/android/media/MediaPlayer.html

于 2012-08-20T07:17:20.940 回答
0

检查以下代码,它对我来说很好。我希望它也对你有用.....不要忘记在 android Manifest File 中添加音频播放权限

import android.app.Activity;
 import android.media.MediaPlayer;
  import android.media.MediaPlayer.OnBufferingUpdateListener;
  import android.media.MediaPlayer.OnCompletionListener;
  import android.os.Bundle;
  import android.os.Handler;
    import android.util.Log;
  import android.view.MotionEvent;
    import android.view.View;
     import android.view.View.OnClickListener;
     import android.view.View.OnTouchListener;
     import android.widget.Button;
     import android.widget.SeekBar;

                                                public class StreamAudioFromUrlSampleActivity extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{
                    private Button btn_play,
                btn_pause,
                btn_stop;
private SeekBar seekBar;
private MediaPlayer mediaPlayer;
private int lengthOfAudio;
private final String URL = "http://android.erkutaras.com/media/audio.mp3";
private final Handler handler = new Handler();
private final Runnable r = new Runnable() { 
    @Override
    public void run() {
        updateSeekProgress();                   
    }
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    init();
}

private void init() {
    btn_play = (Button)findViewById(R.id.btn_play);
    btn_play.setOnClickListener(this);
    btn_pause = (Button)findViewById(R.id.btn_pause);
    btn_pause.setOnClickListener(this);
    btn_pause.setEnabled(false);
    btn_stop = (Button)findViewById(R.id.btn_stop);
    btn_stop.setOnClickListener(this);
    btn_stop.setEnabled(false);

    seekBar = (SeekBar)findViewById(R.id.seekBar);
    seekBar.setOnTouchListener(this);

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

}

@Override
public void onBufferingUpdate(MediaPlayer mediaPlayer, int percent) {
    seekBar.setSecondaryProgress(percent);
}

@Override
public void onCompletion(MediaPlayer mp) {
    btn_play.setEnabled(true);
    btn_pause.setEnabled(false);
    btn_stop.setEnabled(false);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (mediaPlayer.isPlaying()) {
        SeekBar tmpSeekBar = (SeekBar)v;
        mediaPlayer.seekTo((lengthOfAudio / 100) * tmpSeekBar.getProgress() );
    }
    return false;
}

@Override
public void onClick(View view) {

    try {
        mediaPlayer.setDataSource(URL);
        mediaPlayer.prepare();
        lengthOfAudio = mediaPlayer.getDuration();
    } catch (Exception e) {
        //Log.e("Error", e.getMessage());
    }

    switch (view.getId()) {
    case R.id.btn_play:
        playAudio();
        break;
    case R.id.btn_pause:
        pauseAudio();
        break;
    case R.id.btn_stop:
        stopAudio();
        break;
    default:
        break;
    }

    updateSeekProgress();
}

private void updateSeekProgress() {
    if (mediaPlayer.isPlaying()) {
        seekBar.setProgress((int)(((float)mediaPlayer.getCurrentPosition() / lengthOfAudio) * 100));
        handler.postDelayed(r, 1000);
    }
}   

private void stopAudio() {
    mediaPlayer.stop();
    btn_play.setEnabled(true);
    btn_pause.setEnabled(false);
    btn_stop.setEnabled(false);
    seekBar.setProgress(0);
}

private void pauseAudio() {
    mediaPlayer.pause();
    btn_play.setEnabled(true);
    btn_pause.setEnabled(false);
}

private void playAudio() {
    mediaPlayer.start();
    btn_play.setEnabled(false);
    btn_pause.setEnabled(true);
    btn_stop.setEnabled(true);
}

}

于 2012-08-20T07:38:03.137 回答