Search in sources :

Example 31 with MediaPlayer

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();
        }
    });
}
Also used : MediaController(android.widget.MediaController) OnPreparedListener(android.media.MediaPlayer.OnPreparedListener) MediaPlayer(android.media.MediaPlayer)

Example 32 with MediaPlayer

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);
    }
}
Also used : Bitmap(android.graphics.Bitmap) VideoView(android.widget.VideoView) TextView(android.widget.TextView) ImageView(android.widget.ImageView) IOException(java.io.IOException) Uri(android.net.Uri) MediaPlayer(android.media.MediaPlayer)

Example 33 with MediaPlayer

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;
    }
}
Also used : Intent(android.content.Intent) IOException(java.io.IOException) MediaPlayer(android.media.MediaPlayer)

Example 34 with MediaPlayer

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();
}
Also used : IOException(java.io.IOException) MediaPlayer(android.media.MediaPlayer)

Example 35 with MediaPlayer

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;
}
Also used : MediaPlayer(android.media.MediaPlayer)

Aggregations

MediaPlayer (android.media.MediaPlayer)370 IOException (java.io.IOException)180 LargeTest (android.test.suitebuilder.annotation.LargeTest)60 AudioEffect (android.media.audiofx.AudioEffect)54 AudioManager (android.media.AudioManager)50 AssetFileDescriptor (android.content.res.AssetFileDescriptor)28 EnergyProbe (com.android.mediaframeworktest.functional.EnergyProbe)24 MediaRecorder (android.media.MediaRecorder)18 Uri (android.net.Uri)16 SurfaceHolder (android.view.SurfaceHolder)16 CamcorderProfile (android.media.CamcorderProfile)12 File (java.io.File)12 VideoView (android.widget.VideoView)11 Intent (android.content.Intent)8 OnPreparedListener (android.media.MediaPlayer.OnPreparedListener)7 Surface (android.view.Surface)7 View (android.view.View)7 MediaController (android.widget.MediaController)7 MediaFormat (android.media.MediaFormat)6 UtteranceProgressDispatcher (android.speech.tts.TextToSpeechService.UtteranceProgressDispatcher)6