本文整理汇总了Java中android.media.MediaPlayer类的典型用法代码示例。如果您正苦于以下问题:Java MediaPlayer类的具体用法?Java MediaPlayer怎么用?Java MediaPlayer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MediaPlayer类属于android.media包,在下文中一共展示了MediaPlayer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: errToStr
import android.media.MediaPlayer; //导入依赖的package包/类
private String errToStr(int framework_err, int impl_err) {
String msg = null;
if (framework_err == MediaPlayer.MEDIA_ERROR_IO) {
msg = "IO Error";
} else if (framework_err == MediaPlayer.MEDIA_ERROR_MALFORMED) {
msg = "Bitstream unsupported";
} else if (framework_err == MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK) {
msg = "Invalid progressive playback";
} else if (framework_err == MediaPlayer.MEDIA_ERROR_TIMED_OUT) {
msg = "Operation time out";
} else if (framework_err == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
msg = "MediaPlayer died";
} else if (framework_err == MediaPlayer.MEDIA_ERROR_UNSUPPORTED) {
msg = "File spec is not supported in the media framework";
} else if (framework_err == MediaPlayer.MEDIA_ERROR_UNKNOWN) {
msg = "Unknown error";
}
return msg;
}
示例2: soundShake
import android.media.MediaPlayer; //导入依赖的package包/类
private void soundShake() {
try {
this.mPlayer = MediaPlayer.create(RedPacketSdkManager.getInstance().getApplicationContext(), R.raw.shake_sound);
if (this.mPlayer != null) {
this.mPlayer.start();
this.mPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
RedPacketDialog.this.mPlayer.release();
}
});
}
} catch (Exception e) {
e.printStackTrace();
if (this.mPlayer != null) {
this.mPlayer.release();
}
}
}
示例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: onInfo
import android.media.MediaPlayer; //导入依赖的package包/类
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
switch (what) {
case MediaPlayer.MEDIA_INFO_BUFFERING_START:
mEventEmitter.receiveEvent(getId(), Events.EVENT_STALLED.toString(), Arguments.createMap());
break;
case MediaPlayer.MEDIA_INFO_BUFFERING_END:
mEventEmitter.receiveEvent(getId(), Events.EVENT_RESUME.toString(), Arguments.createMap());
break;
case MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
mEventEmitter.receiveEvent(getId(), Events.EVENT_READY_FOR_DISPLAY.toString(), Arguments.createMap());
break;
default:
}
return false;
}
示例5: onPrepared
import android.media.MediaPlayer; //导入依赖的package包/类
@Override
public void onPrepared(MediaPlayer mp) {
if(mHasVideo) {
mediaPlayer.start();
mediaPlayer.setVolume(0,0);
setVideoSize();
vidSurface.bringToFront();
vidSurface.requestFocus();
MediaController mController = new MediaController(context);
mController.setMediaPlayer(this);
mController.setAnchorView(vidSurface);
// mController.show(0);
vidSurface.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
}
}
示例6: initializeMedia
import android.media.MediaPlayer; //导入依赖的package包/类
private void initializeMedia() {
mediaEight[0] = MediaPlayer.create(getApplicationContext(), R.raw.eight_1);
mediaEight[1] = MediaPlayer.create(getApplicationContext(), R.raw.eight_2);
mediaEight[2] = MediaPlayer.create(getApplicationContext(), R.raw.eight_3);
mediaEight[3] = MediaPlayer.create(getApplicationContext(), R.raw.eight_4);
mediaEight[4] = MediaPlayer.create(getApplicationContext(), R.raw.eight_5);
mediaEight[5] = MediaPlayer.create(getApplicationContext(), R.raw.eight_7);
mediaEight[6] = MediaPlayer.create(getApplicationContext(), R.raw.eight_8);
mediaEight[0].setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaEight[1].setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaEight[2].setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaEight[3].setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaEight[4].setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaEight[5].setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaEight[6].setAudioStreamType(AudioManager.STREAM_MUSIC);
}
示例7: playRecord
import android.media.MediaPlayer; //导入依赖的package包/类
private void playRecord() throws IOException {
File dir = new File(Environment.getExternalStorageDirectory() + File.separator + "AAAAAAAAA");
if (dir.exists()) {
File[] files = dir.listFiles();
if (files.length > 0) {
mediaPlayer.setDataSource(files[0].getAbsolutePath());
mediaPlayer.prepare();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
}
}
}
示例8: AndroidMediaPlayer
import android.media.MediaPlayer; //导入依赖的package包/类
public AndroidMediaPlayer() {
synchronized (mInitLock) {
mInternalMediaPlayer = new MediaPlayer();
}
mInternalMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mInternalListenerAdapter = new AndroidMediaPlayerListenerHolder(this);
attachInternalListeners();
}
示例9: playSoundFile
import android.media.MediaPlayer; //导入依赖的package包/类
@ReactMethod
public void playSoundFile(String name, String type) throws IOException {
if (this.mediaPlayer == null) {
int soundResID = getReactApplicationContext().getResources().getIdentifier(name, "raw", getReactApplicationContext().getPackageName());
this.mediaPlayer = MediaPlayer.create(getCurrentActivity(), soundResID);
this.mediaPlayer.setOnCompletionListener(
new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
WritableMap params = Arguments.createMap();
params.putBoolean("success", true);
sendEvent(getReactApplicationContext(), "FinishedPlaying", params);
}
});
} else {
Uri uri = Uri.parse("android.resource://" + getReactApplicationContext().getPackageName() + "/raw/" + name);
this.mediaPlayer.reset();
this.mediaPlayer.setDataSource(getCurrentActivity(), uri);
this.mediaPlayer.prepare();
}
this.mediaPlayer.start();
}
示例10: chimeSound
import android.media.MediaPlayer; //导入依赖的package包/类
public static void chimeSound() {
((Activity) MainMenu.getAppContext()).runOnUiThread(new Runnable() {
public void run() {
final MediaPlayer mp1 = MediaPlayer.create(MainMenu.getAppContext(), R.raw.chime);
mp1.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp1.release();
}
});
mp1.start();
}
});
}
示例11: onCreate
import android.media.MediaPlayer; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initializing button
btn = findViewById(R.id.play);
//initializing mediaPlayer object
mediaPlayer = new MediaPlayer();
//setting streaming type
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//setting clickListener on button
btn.setOnClickListener(pausePlay);
}
示例12: onInfo
import android.media.MediaPlayer; //导入依赖的package包/类
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
switch (what) {
case MediaPlayer.MEDIA_INFO_BAD_INTERLEAVING:
// 音频和视频数据不正确
break;
case MediaPlayer.MEDIA_INFO_BUFFERING_START:
if (!isFinishing())
mSurfaceVideoView.pause();
break;
case MediaPlayer.MEDIA_INFO_BUFFERING_END:
if (!isFinishing())
mSurfaceVideoView.start();
break;
case MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
if (DeviceUtils.hasJellyBean()) {
mSurfaceVideoView.setBackground(null);
} else {
mSurfaceVideoView.setBackgroundDrawable(null);
}
break;
}
return false;
}
示例13: onSurfaceCreated
import android.media.MediaPlayer; //导入依赖的package包/类
@Override
public void onSurfaceCreated(SurfaceHolder holder) {
super.onSurfaceCreated(holder);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setSurface(holder.getSurface());
try {
AssetManager assets = getApplicationContext().getAssets();
AssetFileDescriptor descriptor = assets.openFd("text1.mp4");
mMediaPlayer.setDataSource(descriptor.getFileDescriptor(),descriptor.getStartOffset(),descriptor.getLength());
mMediaPlayer.setLooping(true);
mMediaPlayer.setVolume(1.0f,1.0f);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
示例14: playAudio
import android.media.MediaPlayer; //导入依赖的package包/类
/**
* 播放语音文件
*
* @param path
* @param finishCallback
*/
public synchronized void playAudio(String path, Runnable finishCallback) {
if (onceStart) {
mediaPlayer.reset();
}
tryRunFinishCallback();
audioPath = path;
LCIMAudioHelper.this.finishCallback = finishCallback;
try {
mediaPlayer.setDataSource(path);
mediaPlayer.prepare();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
tryRunFinishCallback();
}
});
mediaPlayer.start();
onceStart = true;
} catch (IOException e) {
}
}
示例15: 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;
}
}