-1

我想使用 MediaPlayer android,使用此代码,但出现错误,有办法修复此代码,或者谁能给我一个更好的例子?

try {

    MediaPlayer player = new MediaPlayer();
    player.setDataSource("/LFP/res/raw/Allen.mp3");
    player.prepare();
    player.start();

} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}          
4

3 回答 3

0
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import android.media.MediaPlayer.OnPreparedListener;
import android.view.MotionEvent;
import android.widget.MediaController;
import android.widget.TextView;

import java.io.IOException;

public class AudioPlayer extends Activity implements OnPreparedListener,                
MediaController.MediaPlayerControl{
private static final String TAG = "AudioPlayer";

public static final String AUDIO_FILE_NAME = "audioFileName";

private MediaPlayer mediaPlayer;
private MediaController mediaController;
private String audioFile;

private Handler handler = new Handler();

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audio_player);

audioFile = this.getIntent().getStringExtra(AUDIO_FILE_NAME);
((TextView)findViewById(R.id.now_playing_text)).setText(audioFile);

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

mediaController = new MediaController(this);

try {
  mediaPlayer.setDataSource(audioFile);
  mediaPlayer.prepare();
  mediaPlayer.start();
} catch (IOException e) {
  Log.e(TAG, "Could not open file " + audioFile + " for playback.", e);
}

}

@Override
protected void onStop() {
super.onStop();
mediaController.hide();
mediaPlayer.stop();
mediaPlayer.release();
}

  @Override
  public boolean onTouchEvent(MotionEvent event) {

  mediaController.show();
  return false;
}

public void start() {
mediaPlayer.start();
}

public void pause() {
mediaPlayer.pause();
}

public int getDuration() {
return mediaPlayer.getDuration();
}

public int getCurrentPosition() {
return mediaPlayer.getCurrentPosition();
}

public void seekTo(int i) {
mediaPlayer.seekTo(i);
}

public boolean isPlaying() {
return mediaPlayer.isPlaying();
}

public int getBufferPercentage() {
return 0;
}

public boolean canPause() {
return true;
}

public boolean canSeekBackward() {
return true;
}

public boolean canSeekForward() {
return true;
}

  public void onPrepared(MediaPlayer mediaPlayer) {
  Log.d(TAG, "onPrepared");
  mediaController.setMediaPlayer(this);
  mediaController.setAnchorView(findViewById(R.id.main_audio_view));

   handler.post(new Runnable() {
     public void run() {
     mediaController.setEnabled(true);
     mediaController.show();
     }
   });
  }
 }

这对我来说很好。希望它也对你有用。

于 2013-05-30T09:21:33.887 回答
0

利用

mediaplayer.prepareAsync() 
and 
mediaplayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.start();
                }
            });

取而代之的是

于 2016-12-14T10:33:32.103 回答
0

在这里,我将音频文件保存在资产中。以下代码有效,

MediaPlayer mp= new MediaPlayer();
        AssetFileDescriptor afd = null;
        try {
            afd =getActivity().getBaseContext().getAssets().openFd("myAudio.mp3");
            mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            afd.close();
            mp.prepare();
            mp.start();

            mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    mp.release();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
于 2017-10-31T12:48:50.787 回答