i have a problem when i try to play a video in format hd with class MediaPlayer in android (it gives me black display but it plays sound)... it doesn't work with hd video but it works with not hd videos... i would wanto to know what you think about this and if there is a way to fix the problem.
this is my code...
SingletonMedia singletonMedia = SingletonMedia.getIstance();
final MediaPlayer player = new MediaPlayer();
//String uri = singletonMedia.listaMedia.get(currentMedia).url;
//MediaPlayer player = MediaPlayer.create(getActivity(), Uri.parse(uri));
SurfaceHolder mHolder;
SurfaceView mPreview;
mPreview = (SurfaceView) getActivity().findViewById(R.id.surfaceView);
//mPreview.setVisibility(View.VISIBLE);
mHolder = mPreview.getHolder();
try {
Log.w(null, singletonMedia.listaMedia.get(currentMedia).url);
player.setDataSource(singletonMedia.listaMedia.get(currentMedia).url);
}
catch (Exception e) {
Log.w(null, "Entrato nell'eccezione del setDataSource nel Fragment: "+e.getMessage());
}
player.setDisplay(mHolder);
try {
player.prepare();
} catch (Exception e) {
Log.w(null, "Entrato nell'eccezione del prepare nel Fragment: " + e.getMessage());
}
player.start();
thanks in advance for help.
is there a way to play hd videos in android?
You should find out the encoding format of your hd video. Android os does not support too many formats. MediaPlayer can play video with format h264 even if video is hd. If encoding foramt is h264, you should try another device.
Or you should make sure your play operation be started after SurfaceView.surfaceCreated.
Related
Iam trying to develop an android based camera application which requires recording videos on live camera preview with overlay images. Is there any way on android using which we can record videos with overlay images without using any third party library .
Currently iam using an additional Surfaceview as an overlay on live camera screen and MediaRecorder class for recording videos.Here is my code :
recorder = new MediaRecorder();
recorder.setCamera(mCam);
String filename="";
String path="";
path= Environment.getExternalStorageDirectory().getAbsolutePath().toString();
Date date=new Date();
filename="/rec"+date.toString().replace(" ", "_").replace(":", "_")+".mp4";
File file=new File(path,filename);
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
recorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setVideoSize(mPreviewSize.width, mPreviewSize.height);
recorder.setVideoEncodingBitRate(10000000);
recorder.setVideoFrameRate(30);
try {
recorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
The above code crashes the application during the start of recording process.
Is there any other way in OpenGL ES 2.0 apart from MediaRecorder for recording videos with overlay.
Please advice.
How can i play audio from a specified time ?
I have a prerecorded audio and i want to play it from a specific time to a specific time.
I have tried seekTo function in MediaPlayer but it doesnt seem to work.
mPlayer = new MediaPlayer();
try{
mPlayer.setDataSource(PATH_FILE + selectedAudio);
mPlayer.prepare();
mPlayer.seekTo(2);
mPlayer.start();
}catch(IOException ioe){
System.out.println("Error in startPlaying method");
}
Thanks in advance
Iam trying to built an android application for playing online radio. The code is working in emulator properly. but when installed in phone it does not works.
MediaPlayer mediaPlayer = new MediaPlayer();
String url = "http://5293.live.streamtheworld.com:3690/JACK2_LOWAAC_SC";
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();
Is there any problem in the link format? I tried to play same link in html5 its working fine on desktop but the same website when opened in phone the link is not working. Also are there any issues or any components in html5 which do not work in smart phones bu work on Desktop ?
I need to play the live stream not the static file like mp3. You can take some URL form www.listenlive.eu/uk.html and try to play.The URL in my code is form this site only. download the VLC file and open it with any text editor and you will get url.
use mediaPlayer.prepareAsync(); instead of mediaPlayer.prepare(); since your are streaming from web and also add permission for internet
<uses-permission android:name="android.permission.INTERNET"/>
Also
String url = "http://5293.live.streamtheworld.com:3690/JACK2_LOWAAC_SC";
doesn't work for me instead i use
final String url = "http://vprmix.streamguys.net/vprmix64.mp3";
private boolean isPLAYING = false;
public void streamAudio(String url) {
if (!isPLAYING) {
isPLAYING = true;
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(url);
mediaPlayer.prepareAsync();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
Log.e("mediaPlayer", "prepare() failed");
}
} else {
isPLAYING = false;
stopPlaying();
}
}
private void stopPlaying() {
mediaPlayer.release();
mediaPlayer = null;
}
I need to play a live stream on devices with 2.x and greater versions. This states that it's impossible to play live streams on devices with Android 2.x.
What're my options here ? Especially I'm interested in streaming audio - what format should i pick and in conjunction with which protocol ?
P.S. I've tried Vitamio - don't want to make customers download third party libraries.
UPD
How come I can play this stream "http://188.138.112.71:9018/" ?
try this example for RTSP streaming (the url should support RTSP) for video change the code to support just audio
public class MultimediaActivity extends Activity {
private static final String RTSP = "rtsp://url here";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.multimedia);
//***VideoView to video element inside Multimedia.xml file
VideoView videoView = (VideoView) findViewById(R.id.video);
Log.v("Video", "***Video to Play:: " + RTSP);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
Uri video = Uri.parse(RTSP);
videoView.setMediaController(mc);
videoView.setVideoURI(video);
videoView.start();
}
}
EDIT:
Live Audio streaming using MediaPlayer in Android
Live Audio streaming in android, from 1.6 sdk onwards is become so easy. In setDataSource() API directly pass the url and audio will play without any issues.
The complete code snippet is,
public class AudioStream extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String url = "http://www.songblasts.com/songs/hindi/t/three-idiots/01-Aal_Izz_Well-(SongsBlasts.Com).mp3";
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(url);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepare();
mp.start();
} catch (Exception e) {
Log.i("Exception", "Exception in streaming mediaplayer e = " + e);
}
}
}
You can use RTSP protocol which is supported by Android native media player.
player = new MediaPlayer();
player.reset();
player.setDataSource(intent.getStringExtra("Path"));
player.prepare();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
Where path would be your rtsp audio streaming url.
I'm trying to record audio using MediaRecorder on the Droid X2, and I'm running into issues. The MediaRecorder seems to prepare and start recording just fine, but when I stop recording and try to listen to the file that is produced the playback immediately stops. Even if I try to open the audio file in the standard media player app, it immediately stops.
Here's the code I'm using to record:
mCurrentRecordingFilePath = mContext.getExternalFilesDir(null).getAbsolutePath() + File.separator + fileName;
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mRecorder.setOutputFile(mCurrentRecordingFilePath);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
// mRecorder.setAudioEncodingBitRate(16);
// mRecorder.setAudioSamplingRate(44100);
try {
mRecorder.prepare();
mIsPrepared = true;
} catch (IOException e) {
Log.e("Test", "MediaRecorder prepare() failed");
e.printStackTrace();
}
if (mRecorder != null && mIsPrepared) {
mRecorder.start();
mIsRecording = true;
Log.i("Test", "Started audio capture: " + mCurrentRecordingFilePath);
}
I commented out the setAudioEncodingBitRate() call because it was causing the prepare() to fail, and just to be on the safe side I also commented out setAudioSamplingRate(). I have tried every combination of output format and audio encoder that is available and the result is always the same. I get no exceptions, and a file is created that is not empty, but it will not play back properly.
Not sure if it will help diagnose, but here's the code I use to stop recording:
if (mRecorder != null && mIsRecording) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
mIsPrepared = false;
mIsRecording = false;
Log.i("Test", "Stopped audio capture");
}
The recording code works fine on a Galaxy Nexus, Nexus S, EVO 4G, and Galaxy SII. Any idea why my audio file would be bad on the Droid X2?
Found the answer here: MediaPlayer cant play audio files from program data folder?
Problem wasn't the recording, it was the way I was setting the MediaPlayer's datasource. Though I have no idea why the stock media player app wouldn't play the file... maybe it's a permission thing. Here's the code I was using to play the file:
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(fileName);
mPlayer.prepare();
mPlayer.start();
Here's the code that will work if the file is stored in the external files directory:
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource((new FileInputStream(fileName)).getFD());
mPlayer.prepare();
mPlayer.start();