当前位置: 首页>>代码示例>>Java>>正文


Java MediaPlayer.prepare方法代码示例

本文整理汇总了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;
    }
}
 
开发者ID:MRYangY,项目名称:YZxing,代码行数:17,代码来源:BeepManager.java

示例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;
  }
}
 
开发者ID:yun2win,项目名称:tvConnect_android,代码行数:23,代码来源:BeepManager.java

示例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;
}
 
开发者ID:PhilippC,项目名称:keepass2android,代码行数:19,代码来源:BeepManager.java

示例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;
  }
}
 
开发者ID:kkyflying,项目名称:CodeScaner,代码行数:22,代码来源:BeepManager.java

示例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;
    }
}
 
开发者ID:Hultron,项目名称:LifeHelper,代码行数:22,代码来源:BeepManager.java

示例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");
    }
}
 
开发者ID:WorldBank-Transport,项目名称:RoadLab-Pro,代码行数:21,代码来源:VoiceRecordPlayer.java

示例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;
}
 
开发者ID:dufangyu1990,项目名称:LeCatApp,代码行数:20,代码来源:BeepManager.java

示例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);
    }
}
 
开发者ID:hgs1217,项目名称:Paper-Melody,代码行数:16,代码来源:ListenFragment.java

示例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;
}
 
开发者ID:aliumujib,项目名称:Orin,代码行数:34,代码来源:MultiPlayer.java

示例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");
    }
}
 
开发者ID:abicelis,项目名称:Remindy,代码行数:29,代码来源:RecordAudioDialogFragment.java

示例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
}
 
开发者ID:hsj-xiaokang,项目名称:OSchina_resources_android,代码行数:33,代码来源:RecordButtonUtil.java

示例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();
  }
}
 
开发者ID:Arjun-sna,项目名称:LocationAware,代码行数:40,代码来源:LocationAwareService.java

示例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;
}
 
开发者ID:angcyo,项目名称:RLibrary,代码行数:10,代码来源:MusicFactory.java

示例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();
	}
}
 
开发者ID:Vicent9920,项目名称:FanChat,代码行数:57,代码来源:EaseChatRowVoicePlayClickListener.java

示例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;
}
 
开发者ID:WeiXinqiao,项目名称:Recognize-it,代码行数:60,代码来源:PathConversion.java


注:本文中的android.media.MediaPlayer.prepare方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。