本文整理汇总了Java中android.media.MediaPlayer.setDataSource方法的典型用法代码示例。如果您正苦于以下问题:Java MediaPlayer.setDataSource方法的具体用法?Java MediaPlayer.setDataSource怎么用?Java MediaPlayer.setDataSource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.media.MediaPlayer
的用法示例。
在下文中一共展示了MediaPlayer.setDataSource方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initializeMediaPlayer
import android.media.MediaPlayer; //导入方法依赖的package包/类
private void initializeMediaPlayer() {
_logger.Debug("initializeMediaPlayer");
_radioPlayer = new MediaPlayer();
try {
_radioPlayer.setDataSource(_radioStream.GetUrl());
_logger.Debug(String.format(Locale.getDefault(), "Set DataSource to %s", _radioStream.GetUrl()));
} catch (Exception e) {
_logger.Error(e.toString());
Toasty.error(_context, "An error appeared settings url for radio player!", Toast.LENGTH_LONG).show();
}
_radioPlayer.setOnBufferingUpdateListener((mediaPlayer, percent) -> _logger.Info(String.format(Locale.getDefault(), "Buffered to %d%%", percent)));
_radioPlayer.setOnPreparedListener(mediaPlayer -> {
_logger.Debug("onPreparedListener...");
_radioPlayer.start();
});
}
示例2: prepareMediaPlayerFromPoint
import android.media.MediaPlayer; //导入方法依赖的package包/类
private void prepareMediaPlayerFromPoint(int progress) {
//set mediaPlayer to start from middle of the audio file
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(item.getFilePath());
mMediaPlayer.prepare();
mSeekBar.setMax(mMediaPlayer.getDuration());
mMediaPlayer.seekTo(progress);
mMediaPlayer.setOnCompletionListener(mp -> stopPlaying());
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
//keep screen on while playing audio
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
示例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);
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;
}
}
示例4: 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;
}
示例5: onPlayClick
import android.media.MediaPlayer; //导入方法依赖的package包/类
public void onPlayClick(View v) {
if (!mPlaying) {
try {
mPlayer = new MediaPlayer();
AssetFileDescriptor fd = getResources().openRawResourceFd(R.raw.space);
mPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
mPlayer.setOnCompletionListener(mOnCompletionListener);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.prepare();
mPlayer.start();
mPlaying = true;
setState(STATE_PLAYING);
} catch (IOException e) {
Log.e(TAG, "error trying to play a sound");
}
} else {
mPlayer.setOnCompletionListener(null);
mPlayer.stop();
mPlayer.release();
mPlayer = null;
mPlaying = false;
setState(STATE_IDLE);
}
}
示例6: 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;
}
}
示例7: 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");
}
}
示例8: 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 {
int rid = activity.getResources().getIdentifier("beep", "raw", activity.getPackageName());
if (rid == 0) {
return null;
}
AssetFileDescriptor file = activity.getResources().openRawResourceFd(rid);
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;
}
}
示例9: onPlaybackClick
import android.media.MediaPlayer; //导入方法依赖的package包/类
public void onPlaybackClick(View v) {
File f = new File(getExternalFilesDir(null), "recording.mp4");
if (!mPlaying) {
try {
mPlayer = new MediaPlayer();
mPlayer.setDataSource(f.getAbsolutePath());
mPlayer.setOnCompletionListener(mOnCompletionListener);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.prepare();
mPlayer.start();
mPlaying = true;
setState(STATE_PLAYING_BACK);
} catch (IOException e) {
Log.e(TAG, "error trying to playback recording");
}
} else {
mPlayer.setOnCompletionListener(null);
mPlayer.stop();
mPlayer.release();
mPlayer = null;
mPlaying = false;
setState(STATE_IDLE);
}
}
示例10: createMusicFromFile
import android.media.MediaPlayer; //导入方法依赖的package包/类
public static Music createMusicFromFile(final MusicManager pMusicManager, final File pFile) throws IOException {
final MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(new FileInputStream(pFile).getFD());
mediaPlayer.prepare();
final Music music = new Music(pMusicManager, mediaPlayer);
pMusicManager.add(music);
return music;
}
示例11: playSound
import android.media.MediaPlayer; //导入方法依赖的package包/类
private void playSound(Context context, Uri alert) {
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(context, alert);
final AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
} catch (IOException e) {
System.out.println("OOPS");
}
}
示例12: initializeMediaPlayer
import android.media.MediaPlayer; //导入方法依赖的package包/类
private void initializeMediaPlayer() {
_radioPlayer = new MediaPlayer();
try {
_radioPlayer.setDataSource(_radioStream.GetUrl());
} catch (Exception e) {
Logger.getInstance().Error(TAG, e.getMessage());
Toasty.error(_context, "An error appeared settings url for radio player!", Toast.LENGTH_LONG).show();
}
_radioPlayer.setOnBufferingUpdateListener((mediaPlayer, percent) -> Logger.getInstance().Information(TAG, String.format(Locale.getDefault(), "Buffered to %d%%", percent)));
_radioPlayer.setOnPreparedListener(mediaPlayer -> _radioPlayer.start());
}
示例13: startPlayMusic
import android.media.MediaPlayer; //导入方法依赖的package包/类
/**
* 开始播放音乐
* @param path 音乐绝对路径
*/
private void startPlayMusic(String path){
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setOnPreparedListener(preparedListener);
mMediaPlayer.setDataSource(path);
mMediaPlayer.prepareAsync();
}catch (Exception e) {
LogUtils.error(e.toString());
}
}
示例14: 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);
}
}
示例15: initAudioPlayer
import android.media.MediaPlayer; //导入方法依赖的package包/类
private void initAudioPlayer() throws IOException {
audioPlay = new MediaPlayer();
// audioPlay.setOnPreparedListener(prepareListener);
audioPlay.setOnCompletionListener(endAudioListener);
audioPlay.setAudioStreamType(AudioManager.STREAM_MUSIC);
audioPlay.setDataSource(audioFilePath);
audioPlay.prepare();
}