use of android.media.MediaPlayer in project AndroidSDK-RecipeBook by gabu.
the class Recipe067 method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mVideoView = (VideoView) findViewById(R.id.video_view);
// MediaControllerを作って
mMediaController = new MediaController(this);
// MediaControllerセット
mVideoView.setMediaController(mMediaController);
// VideoViewで動画を再生する準備ができた時に
// 呼び出されるリスナー
mVideoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
// 3,4秒でMediaControllerが消えちゃうので
// 常に表示させておきたいんだけど
// showメソッドが期待通りに動作してくれない
// durationは正しく取得できてる。
int duration = mVideoView.getDuration();
mMediaController.show(duration);
// リファレンスを信じて0にしてもダメ
// mMediaController.show(0);
// まぁ気にせず再生スタート!
mVideoView.start();
}
});
}
use of android.media.MediaPlayer in project AndroidSDK-RecipeBook by gabu.
the class Recipe086 method onActivityResult.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
// 正しい結果が得られなかった場合の処理
return;
}
if (requestCode == REQUEST_CODE_1) {
// このbitmapが撮影した画像データです。
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageBitmap(bitmap);
} else if (requestCode == REQUEST_CODE_2) {
// dataがnullなので、以下のように取得しないで
// Uri uri = data.getData();
// インテントにセットしたUri mPictureUriを使う
// エラーが発生していなければ、
// このmPictureUriに撮影した写真データのUriが入っている。
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageURI(mPictureUri);
} else if (requestCode == REQUEST_CODE_3) {
// 撮影された動画のUriを取得
Uri uri = data.getData();
// VideoViewを取得
VideoView v = (VideoView) findViewById(R.id.VideoView01);
// VideoViewにはUriがセットできます。
v.setVideoURI(uri);
// 再生します。
v.start();
} else if (requestCode == REQUEST_CODE_4) {
// 録音された音声のUriを取得
Uri uri = data.getData();
// メディアプレーヤーで再生する例
MediaPlayer mp = new MediaPlayer();
try {
// Uriをセット
mp.setDataSource(getApplicationContext(), uri);
// 準備
mp.prepare();
// 再生!
mp.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else if (requestCode == REQUEST_CODE_5) {
String resultStr = "";
// 認識結果のリストを取得
// 似ている言葉など、複数の結果がある場合もある。
List<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
for (String result : results) {
resultStr += (result + "\n");
}
TextView textView = (TextView) findViewById(R.id.text);
textView.setText(resultStr);
}
}
use of android.media.MediaPlayer in project XobotOS by xamarin.
the class VideoView method openVideo.
private void openVideo() {
if (mUri == null || mSurfaceHolder == null) {
// not ready for playback just yet, will try again later
return;
}
// Tell the music playback service to pause
// TODO: these constants need to be published somewhere in the framework.
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "pause");
mContext.sendBroadcast(i);
// we shouldn't clear the target state, because somebody might have
// called start() previously
release(false);
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnPreparedListener(mPreparedListener);
mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
mDuration = -1;
mMediaPlayer.setOnCompletionListener(mCompletionListener);
mMediaPlayer.setOnErrorListener(mErrorListener);
mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
mCurrentBufferPercentage = 0;
mMediaPlayer.setDataSource(mContext, mUri, mHeaders);
mMediaPlayer.setDisplay(mSurfaceHolder);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setScreenOnWhilePlaying(true);
mMediaPlayer.prepareAsync();
// we don't set the target state here either, but preserve the
// target state that was there before.
mCurrentState = STATE_PREPARING;
attachMediaController();
} catch (IOException ex) {
Log.w(TAG, "Unable to open content: " + mUri, ex);
mCurrentState = STATE_ERROR;
mTargetState = STATE_ERROR;
mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
return;
} catch (IllegalArgumentException ex) {
Log.w(TAG, "Unable to open content: " + mUri, ex);
mCurrentState = STATE_ERROR;
mTargetState = STATE_ERROR;
mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
return;
}
}
use of android.media.MediaPlayer in project XobotOS by xamarin.
the class Ringtone method openMediaPlayer.
private void openMediaPlayer() throws IOException {
if (mAudio != null) {
mAudio.release();
}
mAudio = new MediaPlayer();
if (mUri != null) {
mAudio.setDataSource(mContext, mUri);
} else if (mFileDescriptor != null) {
mAudio.setDataSource(mFileDescriptor);
} else if (mAssetFileDescriptor != null) {
// a full file.
if (mAssetFileDescriptor.getDeclaredLength() < 0) {
mAudio.setDataSource(mAssetFileDescriptor.getFileDescriptor());
} else {
mAudio.setDataSource(mAssetFileDescriptor.getFileDescriptor(), mAssetFileDescriptor.getStartOffset(), mAssetFileDescriptor.getDeclaredLength());
}
} else {
throw new IOException("No data source set.");
}
mAudio.setAudioStreamType(mStreamType);
mAudio.prepare();
}
use of android.media.MediaPlayer in project platform_frameworks_base by android.
the class VideoView method getAudioSessionId.
@Override
public int getAudioSessionId() {
if (mAudioSession == 0) {
MediaPlayer foo = new MediaPlayer();
mAudioSession = foo.getAudioSessionId();
foo.release();
}
return mAudioSession;
}
Aggregations