本文整理汇总了Java中android.media.MediaPlayer.prepare方法的典型用法代码示例。如果您正苦于以下问题:Java MediaPlayer.prepare方法的具体用法?Java MediaPlayer.prepare怎么用?Java MediaPlayer.prepare使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.media.MediaPlayer
的用法示例。
在下文中一共展示了MediaPlayer.prepare方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildMediaPlayer
import android.media.MediaPlayer; //导入方法依赖的package包/类
private MediaPlayer buildMediaPlayer(Context activity) {
MediaPlayer mediaPlayer = new MediaPlayer();
try (AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep)) {
mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setLooping(false);
mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
mediaPlayer.prepare();
return mediaPlayer;
} catch (IOException ioe) {
Log.w(TAG, ioe);
mediaPlayer.release();
return null;
}
}
示例2: buildMediaPlayer
import android.media.MediaPlayer; //导入方法依赖的package包/类
private MediaPlayer buildMediaPlayer(Context activity) {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnErrorListener(this);
try {
AssetFileDescriptor file = activity.getResources()
.openRawResourceFd(R.raw.beep);
try {
mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
} finally {
file.close();
}
mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
mediaPlayer.prepare();
return mediaPlayer;
} catch (IOException ioe) {
Log.w(TAG, ioe);
mediaPlayer.release();
return null;
}
}
示例3: buildMediaPlayer
import android.media.MediaPlayer; //导入方法依赖的package包/类
private MediaPlayer buildMediaPlayer(Context activity) {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnErrorListener(this);
AssetFileDescriptor file = activity.getResources().openRawResourceFd(keepass2android.plugin.qr.R.raw.beep);
try {
mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
file.close();
mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
mediaPlayer.prepare();
} catch (IOException ioe) {
Log.w(TAG, ioe);
mediaPlayer = null;
}
return mediaPlayer;
}
示例4: buildMediaPlayer
import android.media.MediaPlayer; //导入方法依赖的package包/类
private MediaPlayer buildMediaPlayer(Context activity) {
MediaPlayer mediaPlayer = new MediaPlayer();
try {
AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep);
try {
mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
} finally {
file.close();
}
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setLooping(false);
mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
mediaPlayer.prepare();
return mediaPlayer;
} catch (IOException ioe) {
Log.w(TAG, ioe);
mediaPlayer.release();
return null;
}
}
示例5: buildMediaPlayer
import android.media.MediaPlayer; //导入方法依赖的package包/类
private MediaPlayer buildMediaPlayer(Context activity) {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnErrorListener(this);
try {
AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep);
try {
mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
} finally {
file.close();
}
mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
mediaPlayer.prepare();
return mediaPlayer;
} catch (IOException ioe) {
Log.w(TAG, ioe);
mediaPlayer.release();
return null;
}
}
示例6: startPlaying
import android.media.MediaPlayer; //导入方法依赖的package包/类
public void startPlaying() {
mPlayer = new MediaPlayer();
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Log.d("VOICE", "Ended");
playingRecord = false;
if (listener != null) {
listener.onCompleted();
}
}
});
try {
mPlayer.setDataSource(mFileFullName);
mPlayer.prepare();
mPlayer.start();
} catch (Exception e) {
Log.e(TAG, "prepare() failed");
}
}
示例7: buildMediaPlayer
import android.media.MediaPlayer; //导入方法依赖的package包/类
private MediaPlayer buildMediaPlayer(Context activity) {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnErrorListener(this);
AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep);
try {
mediaPlayer.setDataSource(file.getFileDescriptor(),
file.getStartOffset(), file.getLength());
file.close();
mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
mediaPlayer.prepare();
} catch (IOException ioe) {
Log.w(TAG, ioe);
mediaPlayer = null;
}
return mediaPlayer;
}
示例8: onCreate
import android.media.MediaPlayer; //导入方法依赖的package包/类
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mediaPlayer = new MediaPlayer();
filepath = getArguments().getString(FILEPATH);
imageUrl = getArguments().getString(IMAGEURL);
playState = false;
try {
mediaPlayer.setDataSource(filepath);
mediaPlayer.prepare();
} catch (IOException e) {
Log.i("nib", filepath);
ToastUtil.showShort(R.string.unable_to_play);
}
}
示例9: setDataSourceImpl
import android.media.MediaPlayer; //导入方法依赖的package包/类
/**
* @param player The {@link MediaPlayer} to use
* @param path The path of the file, or the http/rtsp URL of the stream
* you want to play
* @return True if the <code>player</code> has been prepared and is
* ready to play, false otherwise
*/
private boolean setDataSourceImpl(@NonNull final MediaPlayer player, @NonNull final String path) {
if (context == null) {
return false;
}
try {
player.reset();
player.setOnPreparedListener(null);
if (path.startsWith("content://")) {
player.setDataSource(context, Uri.parse(path));
} else {
player.setDataSource(path);
}
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.prepare();
} catch (Exception e) {
return false;
}
player.setOnCompletionListener(this);
player.setOnErrorListener(this);
final Intent intent = new Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION);
intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, getAudioSessionId());
intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, context.getPackageName());
intent.putExtra(AudioEffect.EXTRA_CONTENT_TYPE, AudioEffect.CONTENT_TYPE_MUSIC);
context.sendBroadcast(intent);
return true;
}
示例10: startPlaying
import android.media.MediaPlayer; //导入方法依赖的package包/类
private void startPlaying() {
//Reset time
startTime = SystemClock.uptimeMillis();
TransitionManager.beginDelayedTransition(mContainer);
mFab.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.icon_check_fab_mini));
mFab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(getContext(), R.color.fab_accept_green)));
mRecIcon.setVisibility(View.INVISIBLE);
mPlayIcon.setVisibility(View.VISIBLE);
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mAudioFilePath);
mPlayer.prepare();
mPlayer.start();
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
handleDismissDialog();
}
});
} catch (IOException e) {
Log.e(TAG, "prepare() failed");
}
}
示例11: startPlay
import android.media.MediaPlayer; //导入方法依赖的package包/类
public void startPlay(String audioPath, TextView timeView) {
if (!mIsPlaying) {
if (!StringUtils.isEmpty(audioPath)) {
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(audioPath);
mPlayer.prepare();
if (timeView != null) {
int len = (mPlayer.getDuration() + 500) / 1000;
timeView.setText(len + "s");
}
mPlayer.start();
if (listener != null) {
listener.starPlay();
}
mIsPlaying = true;
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlay();
}
});
} catch (Exception e) {
e.printStackTrace();
}
} else {
AppContext.showToastShort(R.string.record_sound_not_found);
}
} else {
stopPlay();
} // end playing
}
示例12: startPlayer
import android.media.MediaPlayer; //导入方法依赖的package包/类
private void startPlayer() {
mPlayer = new MediaPlayer();
mPlayer.setOnErrorListener(mErrorListener);
try {
isAlarmRinging = true;
// add vibration to alarm alert if it is set
//if (App.getState().settings().vibrate()) {
mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
mHandler.post(mVibrationRunnable);
//}
// Player setup is here
String ringtone;// = App.getState().settings().ringtone();
//if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
// && ringtone.startsWith("content://media/external/")
// && checkSelfPermission(
// Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
ringtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM).toString();
//}
mPlayer.setDataSource(this, Uri.parse(ringtone));
mPlayer.setLooping(true);
mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mPlayer.setVolume(mVolumeLevel, mVolumeLevel);
mPlayer.prepare();
mPlayer.start();
//if (App.getState().settings().ramping()) {
// mHandler.postDelayed(mVolumeRunnable, VOLUME_INCREASE_DELAY);
//} else {
mPlayer.setVolume(MAX_VOLUME, MAX_VOLUME);
//}
} catch (Exception e) {
isAlarmRinging = false;
if (mPlayer.isPlaying()) {
mPlayer.stop();
}
stopSelf();
}
}
示例13: createMusicFromResource
import android.media.MediaPlayer; //导入方法依赖的package包/类
public static Music createMusicFromResource(final MusicManager pMusicManager, final Context pContext, final int pMusicResID) throws IOException {
final MediaPlayer mediaPlayer = MediaPlayer.create(pContext, pMusicResID);
mediaPlayer.prepare();
final Music music = new Music(pMusicManager, mediaPlayer);
pMusicManager.add(music);
return music;
}
示例14: playVoice
import android.media.MediaPlayer; //导入方法依赖的package包/类
public void playVoice(String filePath) {
if (!(new File(filePath).exists())) {
return;
}
playMsgId = message.getMsgId();
AudioManager audioManager = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
mediaPlayer = new MediaPlayer();
if (EaseUI.getInstance().getSettingsProvider().isSpeakerOpened()) {
audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.setSpeakerphoneOn(true);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
} else {
audioManager.setSpeakerphoneOn(false);// 关闭扬声器
// 把声音设定成Earpiece(听筒)出来,设定为正在通话中
audioManager.setMode(AudioManager.MODE_IN_CALL);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
}
try {
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mediaPlayer.release();
mediaPlayer = null;
stopPlayVoice(); // stop animation
}
});
isPlaying = true;
currentPlayListener = this;
mediaPlayer.start();
showAnimation();
// 如果是接收的消息
if (message.direct() == EMMessage.Direct.RECEIVE) {
if (!message.isAcked() && chatType == ChatType.Chat) {
// 告知对方已读这条消息
EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
}
if (!message.isListened() && iv_read_status != null && iv_read_status.getVisibility() == View.VISIBLE) {
// 隐藏自己未播放这条语音消息的标志
iv_read_status.setVisibility(View.INVISIBLE);
message.setListened(true);
EMClient.getInstance().chatManager().setVoiceMessageListened(message);
}
}
} catch (Exception e) {
System.out.println();
}
}
示例15: convert
import android.media.MediaPlayer; //导入方法依赖的package包/类
@WorkerThread
@NonNull
public AlbumFile convert(String filePath) {
File file = new File(filePath);
String name = file.getName();
AlbumFile albumFile = new AlbumFile();
albumFile.setPath(filePath);
albumFile.setName(name);
String title = name;
if (name.contains("."))
title = name.split("\\.")[0];
albumFile.setTitle(title);
File parentFile = file.getParentFile();
albumFile.setBucketName(parentFile.getName());
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(filePath));
albumFile.setMimeType(mimeType);
long nowTime = System.currentTimeMillis();
albumFile.setAddDate(nowTime);
albumFile.setModifyDate(nowTime);
albumFile.setSize(file.length());
int mediaType = AlbumFile.TYPE_INVALID;
if (!TextUtils.isEmpty(mimeType)) {
if (mimeType.contains("video"))
mediaType = AlbumFile.TYPE_VIDEO;
if (mimeType.contains("image"))
mediaType = AlbumFile.TYPE_IMAGE;
}
albumFile.setMediaType(mediaType);
// Filter.
if (mSizeFilter != null && mSizeFilter.filter(file.length())) {
albumFile.setEnable(false);
}
if (mMimeFilter != null && mMimeFilter.filter(mimeType)) {
albumFile.setEnable(false);
}
if (mediaType == AlbumFile.TYPE_VIDEO) {
MediaPlayer player = new MediaPlayer();
try {
player.setDataSource(filePath);
player.prepare();
albumFile.setDuration(player.getDuration());
albumFile.setWidth(player.getVideoWidth());
albumFile.setHeight(player.getVideoHeight());
} catch (Exception ignored) {
} finally {
player.release();
}
if (mDurationFilter != null && mDurationFilter.filter(albumFile.getDuration())) {
albumFile.setEnable(false);
}
}
return albumFile;
}