Initial Contribution
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
package com.example.android.apis.media;
|
||||
|
||||
import com.example.android.apis.R;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.Button;
|
||||
|
||||
public class MediaPlayerDemo extends Activity {
|
||||
private Button mlocalvideo;
|
||||
private Button mresourcesvideo;
|
||||
private Button mstreamvideo;
|
||||
private Button mlocalaudio;
|
||||
private Button mresourcesaudio;
|
||||
private Button mstreamaudio;
|
||||
private static final String MEDIA = "media";
|
||||
private static final int LOCAL_AUDIO = 1;
|
||||
private static final int STREAM_AUDIO = 2;
|
||||
private static final int RESOURCES_AUDIO = 3;
|
||||
private static final int LOCAL_VIDEO = 4;
|
||||
private static final int STREAM_VIDEO = 5;
|
||||
private static final int RESOURCES_VIDEO = 6;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle icicle) {
|
||||
// TODO Auto-generated method stub
|
||||
super.onCreate(icicle);
|
||||
setContentView(R.layout.mediaplayer_1);
|
||||
mlocalaudio = (Button) findViewById(R.id.localaudio);
|
||||
mlocalaudio.setOnClickListener(mLocalAudioListener);
|
||||
mresourcesaudio = (Button) findViewById(R.id.resourcesaudio);
|
||||
mresourcesaudio.setOnClickListener(mResourcesAudioListener);
|
||||
|
||||
mlocalvideo = (Button) findViewById(R.id.localvideo);
|
||||
mlocalvideo.setOnClickListener(mLocalVideoListener);
|
||||
mstreamvideo = (Button) findViewById(R.id.streamvideo);
|
||||
mstreamvideo.setOnClickListener(mStreamVideoListener);
|
||||
}
|
||||
|
||||
private OnClickListener mLocalAudioListener = new OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
Intent intent =
|
||||
new Intent(MediaPlayerDemo.this.getApplication(),
|
||||
MediaPlayerDemo_Audio.class);
|
||||
intent.putExtra(MEDIA, LOCAL_AUDIO);
|
||||
startActivity(intent);
|
||||
|
||||
}
|
||||
};
|
||||
private OnClickListener mResourcesAudioListener = new OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
Intent intent =
|
||||
new Intent(MediaPlayerDemo.this.getApplication(),
|
||||
MediaPlayerDemo_Audio.class);
|
||||
intent.putExtra(MEDIA, RESOURCES_AUDIO);
|
||||
startActivity(intent);
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
private OnClickListener mLocalVideoListener = new OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
Intent intent =
|
||||
new Intent(MediaPlayerDemo.this,
|
||||
MediaPlayerDemo_Video.class);
|
||||
intent.putExtra(MEDIA, LOCAL_VIDEO);
|
||||
startActivity(intent);
|
||||
|
||||
}
|
||||
};
|
||||
private OnClickListener mStreamVideoListener = new OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
Intent intent =
|
||||
new Intent(MediaPlayerDemo.this,
|
||||
MediaPlayerDemo_Video.class);
|
||||
intent.putExtra(MEDIA, STREAM_VIDEO);
|
||||
startActivity(intent);
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.example.android.apis.media;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.media.MediaPlayer;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.example.android.apis.R;
|
||||
|
||||
public class MediaPlayerDemo_Audio extends Activity {
|
||||
|
||||
private static final String TAG = "MediaPlayerDemo";
|
||||
private MediaPlayer mMediaPlayer;
|
||||
private static final String MEDIA = "media";
|
||||
private static final int LOCAL_AUDIO = 1;
|
||||
private static final int STREAM_AUDIO = 2;
|
||||
private static final int RESOURCES_AUDIO = 3;
|
||||
private static final int LOCAL_VIDEO = 4;
|
||||
private static final int STREAM_VIDEO = 5;
|
||||
private String path;
|
||||
|
||||
private TextView tx;
|
||||
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
tx = new TextView(this);
|
||||
setContentView(tx);
|
||||
Bundle extras = getIntent().getExtras();
|
||||
playAudio(extras.getInt(MEDIA));
|
||||
}
|
||||
|
||||
private void playAudio(Integer media) {
|
||||
try {
|
||||
switch (media) {
|
||||
case LOCAL_AUDIO:
|
||||
/**
|
||||
* TODO: Set the path variable to a local audio file path.
|
||||
*/
|
||||
path = "";
|
||||
if (path == "") {
|
||||
// Tell the user to provide an audio file URL.
|
||||
Toast
|
||||
.makeText(
|
||||
MediaPlayerDemo_Audio.this,
|
||||
"Please edit MediaPlayer_Audio Activity, "
|
||||
+ "and set the path variable to your audio file path."
|
||||
+ " Your audio file must be stored on sdcard.",
|
||||
Toast.LENGTH_LONG).show();
|
||||
|
||||
}
|
||||
mMediaPlayer = new MediaPlayer();
|
||||
mMediaPlayer.setDataSource(path);
|
||||
mMediaPlayer.prepare();
|
||||
mMediaPlayer.start();
|
||||
break;
|
||||
case RESOURCES_AUDIO:
|
||||
/**
|
||||
* TODO: Upload a audio file to res/raw folder and provide
|
||||
* its resid in MediaPlayer.create() method.
|
||||
*/
|
||||
mMediaPlayer = MediaPlayer.create(this, R.raw.test_cbr);
|
||||
mMediaPlayer.start();
|
||||
|
||||
}
|
||||
tx.setText("Playing audio...");
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "error: " + e.getMessage(), e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
// TODO Auto-generated method stub
|
||||
if (mMediaPlayer != null) {
|
||||
mMediaPlayer.release();
|
||||
mMediaPlayer = null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
package com.example.android.apis.media;
|
||||
|
||||
import com.example.android.apis.R;
|
||||
import com.example.android.apis.app.AlarmController;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.graphics.PixelFormat;
|
||||
import android.media.AudioManager;
|
||||
import android.media.MediaPlayer;
|
||||
import android.media.MediaPlayer.OnBufferingUpdateListener;
|
||||
import android.media.MediaPlayer.OnCompletionListener;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.SurfaceView;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.Button;
|
||||
import android.widget.Toast;
|
||||
|
||||
|
||||
public class MediaPlayerDemo_Video extends Activity implements
|
||||
OnBufferingUpdateListener, OnCompletionListener,
|
||||
MediaPlayer.OnPreparedListener, SurfaceHolder.Callback {
|
||||
|
||||
private static final String TAG = "MediaPlayerDemo";
|
||||
private int mVideoWidth;
|
||||
private int mVideoHeight;
|
||||
private MediaPlayer mMediaPlayer;
|
||||
private SurfaceView mPreview;
|
||||
private SurfaceHolder holder;
|
||||
private String path;
|
||||
private Bundle extras;
|
||||
private static final String MEDIA = "media";
|
||||
private static final int LOCAL_AUDIO = 1;
|
||||
private static final int STREAM_AUDIO = 2;
|
||||
private static final int RESOURCES_AUDIO = 3;
|
||||
private static final int LOCAL_VIDEO = 4;
|
||||
private static final int STREAM_VIDEO = 5;
|
||||
|
||||
/**
|
||||
*
|
||||
* Called when the activity is first created.
|
||||
*/
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
setContentView(R.layout.mediaplayer_2);
|
||||
mPreview = (SurfaceView) findViewById(R.id.surface);
|
||||
holder = mPreview.getHolder();
|
||||
holder.addCallback(this);
|
||||
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
|
||||
extras = getIntent().getExtras();
|
||||
|
||||
}
|
||||
|
||||
private void playVideo(Integer Media) {
|
||||
try {
|
||||
|
||||
switch (Media) {
|
||||
case LOCAL_VIDEO:
|
||||
/*
|
||||
* TODO: Set the path variable to a local media file path.
|
||||
*/
|
||||
path = "";
|
||||
if (path == "") {
|
||||
// Tell the user to provide a media file URL.
|
||||
Toast
|
||||
.makeText(
|
||||
MediaPlayerDemo_Video.this,
|
||||
"Please edit MediaPlayerDemo_Video Activity, "
|
||||
+ "and set the path variable to your media file path."
|
||||
+ " Your media file must be stored on sdcard.",
|
||||
Toast.LENGTH_LONG).show();
|
||||
|
||||
}
|
||||
break;
|
||||
case STREAM_VIDEO:
|
||||
/*
|
||||
* TODO: Set path variable to progressive streamable mp4 or
|
||||
* 3gpp format URL. Http protocol should be used.
|
||||
* Mediaplayer can only play "progressive streamable
|
||||
* contents" which basically means: 1. the movie atom has to
|
||||
* precede all the media data atoms. 2. The clip has to be
|
||||
* reasonably interleaved.
|
||||
*
|
||||
*/
|
||||
path = "";
|
||||
if (path == "") {
|
||||
// Tell the user to provide a media file URL.
|
||||
Toast
|
||||
.makeText(
|
||||
MediaPlayerDemo_Video.this,
|
||||
"Please edit MediaPlayerDemo_Video Activity,"
|
||||
+ " and set the path variable to your media file URL.",
|
||||
Toast.LENGTH_LONG).show();
|
||||
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Create a new media player and set the listeners
|
||||
mMediaPlayer = new MediaPlayer();
|
||||
mMediaPlayer.setDataSource(path);
|
||||
mMediaPlayer.setDisplay(holder);
|
||||
mMediaPlayer.prepare();
|
||||
mMediaPlayer.setOnBufferingUpdateListener(this);
|
||||
mMediaPlayer.setOnCompletionListener(this);
|
||||
mMediaPlayer.setOnPreparedListener(this);
|
||||
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "error: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public void onBufferingUpdate(MediaPlayer arg0, int percent) {
|
||||
Log.d(TAG, "onBufferingUpdate percent:" + percent);
|
||||
|
||||
}
|
||||
|
||||
public void onCompletion(MediaPlayer arg0) {
|
||||
Log.d(TAG, "onCompletion called");
|
||||
}
|
||||
|
||||
public void onPrepared(MediaPlayer mediaplayer) {
|
||||
Log.d(TAG, "onPrepared called");
|
||||
mVideoWidth = mMediaPlayer.getVideoWidth();
|
||||
mVideoHeight = mMediaPlayer.getVideoHeight();
|
||||
if (mVideoWidth != 0 && mVideoHeight != 0) {
|
||||
holder.setFixedSize(mVideoWidth, mVideoHeight);
|
||||
mMediaPlayer.start();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
|
||||
Log.d(TAG, "surfaceChanged called");
|
||||
|
||||
}
|
||||
|
||||
public void surfaceDestroyed(SurfaceHolder surfaceholder) {
|
||||
Log.d(TAG, "surfaceDestroyed called");
|
||||
}
|
||||
|
||||
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
// TODO Auto-generated method stub
|
||||
Log.d(TAG, "surfaceCreated called");
|
||||
playVideo(extras.getInt(MEDIA));
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
// TODO Auto-generated method stub
|
||||
if (mMediaPlayer != null) {
|
||||
mMediaPlayer.release();
|
||||
mMediaPlayer = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.example.android.apis.media;
|
||||
|
||||
import com.example.android.apis.R;
|
||||
import android.app.Activity;
|
||||
import android.graphics.PixelFormat;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.widget.MediaController;
|
||||
import android.widget.Toast;
|
||||
import android.widget.VideoView;
|
||||
|
||||
public class VideoViewDemo extends Activity {
|
||||
|
||||
/**
|
||||
* TODO: Set the path variable to a streaming video URL or a local media
|
||||
* file path.
|
||||
*/
|
||||
private String path = "";
|
||||
private VideoView mVideoView;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
setContentView(R.layout.videoview);
|
||||
mVideoView = (VideoView) findViewById(R.id.surface_view);
|
||||
|
||||
if (path == "") {
|
||||
// Tell the user to provide a media file URL/path.
|
||||
Toast.makeText(
|
||||
VideoViewDemo.this,
|
||||
"Please edit VideoViewDemo Activity, and set path"
|
||||
+ " variable to your media file URL/path",
|
||||
Toast.LENGTH_LONG).show();
|
||||
|
||||
} else {
|
||||
|
||||
/*
|
||||
* Alternatively,for streaming media you can use
|
||||
* mVideoView.setVideoURI(Uri.parse(URLstring));
|
||||
*/
|
||||
mVideoView.setVideoPath(path);
|
||||
mVideoView.setMediaController(new MediaController(this));
|
||||
mVideoView.requestFocus();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user