How to play mpg/3gp 5 sec video at start of application? - android

I am developing an application in which i want to play a short 5 seconds video at the startup. which is the best format 3gp, mpg or something else? i have generated a title activity. I wanted to play the video before title. Help please!!! Below is the code of my title activity.
public class Title extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.title);
setTitle("M.I.S.T");
this.setTitleColor(Color.BLUE);
View title = getWindow().findViewById(android.R.id.title);
View titleBar = (View) title.getParent();
titleBar.setBackgroundColor(Color.YELLOW);
Thread timer = new Thread(){
public void run(){
try{
sleep(3000);
}catch (InterruptedException e){
e.printStackTrace();
}finally{
Intent open= new Intent("com.congestion6.asad.MENU");
startActivity(open);
}
}
};
timer.start();
}
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}

mpeg can be compressing the video over a range of different formats/algorithms/codecs and some are supported some are not. 3gp is just one and it is supported (although a very poor format).
Try encoding a video yourself that you'll see all different options. Usually mp4 on H264 works flawlessly on mobiles.

Related

how to make a videoview NOT started automatically after prepared

I have created a videoview with a standard media controller on it. I was able to play the video by calling start() method in setOnPreparedListener, so the video will play automatically when it finished preparing it self.
However, what i want to do is to make the video stand-by (NOT playing automatically), so the user need to tap/click/touch the videoview to start the video.
I've done some googling, and i also tried to setOnTouchListener on my videoview and calling the start() method there. But the result is unexpected (and confusing as well), a pop up dialog appears and said "The video cant be played".
This is the complete code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
txtTitle = (TextView)findViewById(R.id.txtTitle);
player = (VideoView)findViewById(R.id.player);
Bundle video = getIntent().getExtras();
if(video != null)
{
id = video.getString("id");
title = video.getString("title");
rtsp = video.getString("rtsp");
}
txtTitle.setText(title);
pDialog = new ProgressDialog(this);
pDialog.setTitle("Please Wait...");
pDialog.setMessage("Buffering...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(this);
mediacontroller.setAnchorView(player);
// Get the URL from String VideoURL
Uri uri = Uri.parse(rtsp);
player.setMediaController(mediacontroller);
player.setVideoURI(uri);
player.setBackgroundColor(Color.WHITE);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
player.requestFocus();
player.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer arg0) {
// TODO Auto-generated method stub
pDialog.dismiss();
player.setBackgroundColor(Color.TRANSPARENT);
//the video will be played if i call the start() method here
}
});
player.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
//"The video cant be played" pop up dialog appeared, video wont start
player.start();
return false;
}
});
}
I got confuse because it can be played when i put the start() method in setOnPreparedListener but it couldn't when i put it in setOnTouchListener.
I don't know if this is related to the problem or not, but im buffering a youtube video (RTSP link) on my videoview.
Any help is appreciated, Thanks.
This should be a comment but my reputation is not high enough. I do not see you calling prepare() method so probably you are trying to invoke start() when player is in initialized state. You might try to set the onTouchListener of player object in the onPrepared() callback.

Video don't play from same time when change screen orientation

I like this site very much because I can find many useful solutions help me very much especially I'm beginner in android programming and this is my first question so I hope find helping from this great community.
The Problem:
I play video in my app and make it playing in full screen in landscape when user orient screen so I want to make video restart playing from the same point by using seekTo() and getCurrentPosition() - but the problem that video play many seconds after the position I mean that video seek to same point in time but when start play it add many seconds.
My Code:
//initialise everything
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
v = (VideoView) findViewById(R.id.videoView1);
v.setVideoPath(dir + videoName + ".3gp");
v.setMediaController(new MediaController(this));
if (savedInstanceState == null){
v.start();
}
else {
playingTime = savedInstanceState.getInt("restartTime", 0);
v.seekTo(playingTime);
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
if (file.exists()){
playingTime = v.getCurrentPosition();
v.stopPlayback();
outState.putInt("restartTime", playingTime);
}
}
I try many solution such as this
How to keep playing video while changing to landscape mode android
I'm using Galaxy Nexus to test my app .. I would be grateful for any hint.
Sorry for my bad English and thanks very much.
In your activity, keep everything normal. It boils down to saving the position when activity is recreated. Here is one approach:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//initialise everything
//finally
if(savedInstanceState!=null) {
int seekValue = savedInstanceState.getInt("where");
//now seekTo(seekValue)
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
int seekValue; //set this to the current position;
outState.putInt("where", seekValue);
super.onSaveInstanceState(outState);
}
}

Stream and Play .m4a stream (Itunes preview Url) in android

I want to stream and play itunes preview urls like http://a2.mzstatic.com/us/r1000/044/Music/e9/40/ec/mzm.evyxvimp.aac.p.m4a. I tried to use AAC Decoder Android Library. By which I can stream and play AAC stream urls like http://http.yourmuze.com:8000/play/paradise/l.aac. But it dnt stream m4a urls(Logcat says java.io.FileNotFoundException: http://a2.mzstatic.com/us/r1000/044/Music/e9/40/ec/mzm.evyxvimp.aac.p.m4a).
How I can stream the .m4a links?
My Code:
public class AACPlayerActivity extends Activity implements OnClickListener,PlayerCallback{
private Button btnPlay;
private ProgressBar progress;
private Handler uiHandler;
private MultiPlayer multiPlayer;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnPlay = (Button)findViewById(R.id.playButton);
progress = (ProgressBar)findViewById(R.id.progress);
progress.setVisibility(View.INVISIBLE);
btnPlay.setOnClickListener(this);
uiHandler = new Handler();
}
public void playerException(Throwable arg0) {
// TODO Auto-generated method stub
}
public void playerMetadata(String arg0, String arg1) {
// TODO Auto-generated method stub
}
public void playerPCMFeedBuffer(boolean arg0, final int audioBufferSizeMs, final int audioBufferCapacityMs) {
// TODO Auto-generated method stub
uiHandler.post(new Runnable() {
public void run() {
// TODO Auto-generated method stub
progress.setProgress( audioBufferSizeMs * progress.getMax() / audioBufferCapacityMs );
}
});
}
public void playerStarted() {
uiHandler.post(new Runnable() {
public void run() {
// TODO Auto-generated method stub
progress.setProgress( 0 );
progress.setVisibility( View.VISIBLE );
}
});
}
public void playerStopped(int arg0) {
// TODO Auto-generated method stub
uiHandler.post(new Runnable() {
public void run() {
// TODO Auto-generated method stub
progress.setVisibility( View.INVISIBLE );
}
});
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(btnPlay.getText().equals("Play")){
start();
btnPlay.setText("stop");
}else{
stop();
}
}
private void start() {
stop();
// we cannot do it in playerStarted() - it is too late:
multiPlayer = new MultiPlayer(this,MultiPlayer.DEFAULT_AUDIO_BUFFER_CAPACITY_MS, MultiPlayer.DEFAULT_DECODE_BUFFER_CAPACITY_MS);
multiPlayer.playAsync("http://a2.mzstatic.com/us/r1000/044/Music/e9/40/ec/mzm.evyxvimp.aac.p.m4a");
}
private void stop() {
if (multiPlayer != null) {
multiPlayer.stop();
multiPlayer = null;
}
}
Updates
I try to use ServeStream it says the url could not be opened.
I try to use Vitamio SDK it also fails to play the itunes preview Urls.
Is possible to play itunes preview urls in android?
Take a look at ServeStream:
HTTP media server browser and stream player for Android.
Features:
Supports Android 2.2+, 3.0+ (No support for < 2.2)
Plays Android supported media files
Additional support for m3u, m3u8, pls and asx playlists
Supports multitasking/playing audio in the background
Repeat and shuffle modes
Alarm clock support
Home screen widget
Utilizes HTML parsing to allow navigation of HTTP media servers that serve HTML pages
M4A and AAC are supported by Android. I would recommend to read this page: http://developer.android.com/guide/appendix/media-formats.html
Just couple of thing which you should be aware of
a) Older devices may have no particular coded or format support (especially, if you go below OS 2.2)
b) Some cheap devices may have quite strange things integrated in audio/video stack. I saw it on couple of cheap Chinese devices.
So, you can try to use MediaPlayer API to play it:
http://developer.android.com/reference/android/media/MediaPlayer.html
You can take a look at example here:
http://blog.endpoint.com/2011/03/api-gaps-android-mediaplayer-example.html
It has nothing to do with media formats etc. Apple simply prevents non-Apple clients to access previews. You can spoof User-Agent header and access to previews.
Read my relevant blog post here: http://ahmetalpbalkan.com/blog/how-to-bypass-itunes-music-previews-protection/ Thats why you get 404 while playing music on Android. this should answer your question.

In eclipse how to have a open url passed in

I am very new to programming but through determination have found a way to make an application that allows a media url to be played through my app the issue is im trying to make it so that its open to more that 1 specific url for that specific song Im trying to make it so that a playlist that is already set on a site can be chosen through this player if anyone is up to helping me learn and or taking the time to work with me through email or messaging please let me know the code i have so far for this app is as follows
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
String url = "http://beatswith.us/uploads/Mac%20Miller%20-%20Paper%20Route%20feat.%20Kev%20Da%20Hustla.mp3"; // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // might take long! (for buffering, etc)
mediaPlayer.start();
}
};
So if I'm reading your question right, it sounds like you currently have just one song your app will play but instead you want to be able to choose from a list of songs to play..
What you probably would want to do as a next step is create an array of songs and then use ListView to allow the user to pick which song they want to play. It looks like this may be your first time programming. I suggest you look up some tuturials showing how a list view works and then make an ArrayAdapter to bind to your ListView.. The ArrayAdapter has an array (think of an array as being a variable that has more than 1 thing in it, in your case Strings which represent URL's to songs..
Here is some code to get you started:
public class SongListActivity extends ListActivity {
static final String[] songList = new String[] {
"http://beatswith.us/uploads/Mac%20Miller%20-%20Paper%20Route%20feat.%20Kev%20Da%20Hustla.mp3",
"http://beatswith.us/another1.mp3",
"http://beatswith.us/another2.mp3"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
ArrayAdapter songAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, songList);
setListAdapter(songAdapter);
}
#Override
protected void onListItemClick(ListView lv, View v,int pos, long id) {
String url = (String) lv.getAdapter().getItem(pos);
playSong(url);
}
private void playSong(String url) {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
mediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
here is more documentation that should help:
http://developer.android.com/reference/android/app/ListActivity.html
You will need to update the array with valid URL's to songs.
-- Update --
I'm a bit confused still by the exact question.. I'm not sure what exactly you want to do.. If there is a playlist on a website (perhaps in xml format) you could download that xml file and then make a ListAdapter that you would use instead of an ArrayAdapter.. I mean really this gets much more complicated because in reality you probably want to show the name of the song so that means you need to parse the ID3 information from the mp3 -- which is relatively easy to do but it is definitely much more involved than what your current code does.. My example should give you a bit of an idea on how to play a list of songs and should get you a bit more programming experience before you move on to more complicated things.

How to use the microphone on Android

I have just started to develop my first Android app, and I am having a hard time figuring out how to start the microphone and have it listen, which is a main feature of my app.
I've searched the Android docs and I can't find much info on this.
Thanks in advance.
Maybe this can help (actually from the Android docs):
Audio Capture
Create a new instance of android.media.MediaRecorder.
Set the audio source using MediaRecorder.setAudioSource(). You will probably want to use MediaRecorder.AudioSource.MIC.
Set output file format using MediaRecorder.setOutputFormat().
Set output file name using MediaRecorder.setOutputFile().
Set the audio encoder using MediaRecorder.setAudioEncoder().
Call MediaRecorder.prepare() on the MediaRecorder instance.
To start audio capture, call MediaRecorder.start().
To stop audio capture, call MediaRecorder.stop().
When you are done with the MediaRecorder instance, call MediaRecorder.release() on it. Calling MediaRecorder.release() is always recommended to free the resource immediately.
or:
Android Audio Recording Tutorial
You can use custom recorder:
final static int RQS_RECORDING = 1;
Uri savedUri;
Button buttonRecord;
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
buttonRecord = (Button) findViewById(R.id.record);
buttonRecord.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(
MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(intent, RQS_RECORDING);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == RQS_RECORDING) {
savedUri = data.getData();
Toast.makeText(MainActivity.this,
"Saved: " + savedUri.getPath(), Toast.LENGTH_LONG).show();
}
}

Categories

Resources