I created a simple application which will stream video from an URL, it works fine when i point to some local URL like,
http://192.168.X.XX:XXX/Tutorial/TestVideo.mp4
But when try to point some external URL like,
http://122.183.X.XX/Tutorial/TestVideo.mp4
the app is not working. In both the URLs i am having the same video file. When i work with the external URL i am getting the following logcat info
08-17 17:34:28.772: INFO/Prefetcher(34): max duration reached, size = 960201 bytes
Please find my code below:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
preview=(VideoView)findViewById(R.id.surface);
preview.setEnabled(true);
preview.bringToFront();
MediaController mc=new MediaController(playerActivity.this);
mc.setMediaPlayer(player_interface);
mc.show(50);
preview.setMediaController(mc );
holder=preview.getHolder();
holder.setFixedSize(400, 400);
b=(Button)findViewById(R.id.cmd_play);
pb=(ProgressBar)findViewById(R.id.progress);
b.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
mp.setDisplay(holder);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnBufferingUpdateListener(playerActivity.this);
mp.setOnPreparedListener(playerActivity.this);
mp.prepare();
mp.start();
mp.seekTo(20000);
}
catch(Exception e)
{
}
}
I don't know what is the problem, please some body help me if you know the solution for this.
Thanks and Regards,
Rajapandian
If this is occurring on the emulator try this solution and try it on an actual phone. If this isn't the problem, you'll need to post the code you are using to load the video.
Related
Currently I'm trying to simply stream a Video from some link this way:
try {
videoView = (VideoView) findViewById(R.id.videoView);
MediaController vidControl = new MediaController(this);
vidControl.setAnchorView(videoView);
videoView.setMediaController(vidControl);
videoView.setVideoPath("http://some.link/some_video.mp4");
loadingProgress.show();
videoView.requestFocus();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
loadingProgress.dismiss();
videoView.start();
}
});
} catch (Exception e) {
e.printStackTrace();
}
Everything is going OK, but I want to make that URL (i.e. http://some.link/some_video.mp4) secure. I mean user won't be able to download this video.
The problem is: before loading this video, the logcat is
displaying log like this:
my.app.package.name W/MediaPlayer: Couldn't open http://some.link/some_video.mp4: java.io.FileNotFoundException: No content provider: http://some.link/some_video.mp4
I have noticed:
Even the code is not going into CATCH block.
Made SIGNED App, but that LOG is still appearing.
Those logs are built into the framework MediaPlayer class. The only way I know of to remove it is to take the MediaPlayer code from AOSP, copy it to a class in your app, remove all the log statements, and use that instead of the Android MediaPlayer class.
so this is my first question here on stackoverflow, so take it easy on me ^^
Since some days I got into android developing. As a first little project I want to develop a soundboard app. Sadly I can't get the damn MediaPlayer to work. Either it gives no error and I can't hear a thing (volume maxed) or I get an error when I want to prepare the MediaPlayer.
I call the playSound(View) event, when clicking on a button. Inside the function, I'd like to play a mp3 file from the apps resources. Here is my current code following that you'll find a link where you can see, how I added the mp3 file to the apps resources. I googled a lot and tried as much as I understand right now but I can't get it to work.. Do you have any ideas what might be wrong with the code?
public void playSound(View v)
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setCancelable(true);
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(MainActivity.this, Uri.parse("android.resource://mypackagename/res/raw/einemetallischebitterkeit"));
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
}
});
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
builder.setMessage(e.toString());
AlertDialog dialog = builder.create();
dialog.show();
}
}
mp3 file in app resources
You should use like this
InputStream mymp3 = getResources().openRawResource(R.raw.einemetallischebitterkeit);
There are so many posts on the same topic but no one is giving proper reason for this error, Could some one help me to solving this error.
My code is working fine for SDCARD videos. whenever I try to access it from url it throws this error.
W/MediaPlayer: Couldn't open file on client side; trying server side: java.io.FileNotFoundException: No content provider: http://download.itcuties.com/teaser/itcuties-teaser-480.mp4
E/MediaPlayer: error (1, -2147483648)
I tried several formats and several urls for everything i am getting the same error.
Internet permissions given.
Code:
public class MainActivity extends AppCompatActivity {
VideoView video1;
String url ="http://download.itcuties.com/teaser/itcuties-teaser-480.mp4";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
video1 = (VideoView) findViewById(R.id.video1);
video1.setVideoURI(Uri.parse(url));
video1.setMediaController(new MediaController(this));
video1.requestFocus();
Thread view1=new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_DISPLAY);
video1.start();
}
});
}
}
You just need to start your view1 thread. Your code loads the video but doesn't play it. Adding this will solve the issue.
view1.start();
I have mp4 and wmv movie files and need to play only sound not video screen. How to do it?? Could I get sound track from movie file??
I know if I hide the SurfaceView from screen, I can only hear sound.. but I have tried a lot but.. it is impossible.
If you have any solution to extract sound track from movie file, please let me know..
Thanks in advance.
If you are using the mediaplayer API in your app then don't call the API public void setDisplay (SurfaceHolder sh). This will ensure that only the audio is played out even if the video content is present..
More info in android doc here
Use following code, Optimize following code as per your requirement. It play video from youtube.
try {
final MediaPlayer m = new MediaPlayer();
Thread t = new Thread(new Runnable() {
public void run() {
try {
m.setDataSource("rtsp://v8.cache3.c.youtube.com/CjgLENy73wIaLwlQP1m32SiSYxMYJCAkFEIJbXYtZ29vZ2xlSARSB3JlbGF0ZWRggqG7w9aS2-1MDA==/0/0/0/video.3gp");
m.prepare();
m.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.start();
} catch (Exception e) {
e.printStackTrace();
}
I have a list of songs that I'm streaming using the MediaPlayer. Some of the songs consistently work and others consistently do not work. I can't see a difference between these files, and they seem to play fine in itunes and such.
When the songs fail it is throwing an IllegalStateException on the mediaPlayer.prepare() line. The IllegalStateException that is thrown has no useful info in it, (detailMessage is null, stackState is null)
Here is my code
try {
mediaPlayer.setDataSource(media.url);
setPlayerState(PlayerState.PREPARING);
mediaPlayer.prepare();
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "bad stream");
}
Here is a url to the file that does NOT work:
skdy.bryceb.dev.mediarain.com/song.m4a
Here is one that DOES work:
skdy.bryceb.dev.mediarain.com/song2.m4a
Any ideas why this works on some songs and fails on others?
Thanks MisterSquonk I'm sure that way would work.
In my particular case after beating my head against the wall for a while I realized that on some songs, I was getting to the buffered amount before the player state was getting set to prepared. So I added a check to make sure that the MediaPlayer was in the "PREPARED" state and then it worked great:
// Media prepared listener
mediaPlayer.setOnPreparedListener(
new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
setPlayerState(PlayerState.PREPARED);
}
});
// Media buffer listener
mediaPlayer.setOnBufferingUpdateListener(
new MediaPlayer.OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
// Sometimes the song will finish playing before the 100% loaded in has been
// dispatched, which result in the song playing again, so check to see if the
// song has completed first
if(getPlayerState() == PlayerState.COMPLETED)
return;
if(getPlayerState() == PlayerState.PAUSED)
return;
// If the music isn't already playing, and the buffer has been reached
if(!mediaPlayer.isPlaying() && percent > PERCENT_BUFFER) {
if(getPlayerState() == PlayerState.PREPARED)
{
mediaPlayer.start();
setPlayerState(PlayerState.PLAYING);
}
//if it isn't prepared, then we'll wait till the next buffering
//update
return;
}
}
});
OK, I hacked together a minimal Mediaplayer implementation in a 'sandbox' app/activity I always keep spare for testing.
I might be wrong but if you're streaming these songs over the net, you'll need to prefix the url with http://.
I tried the urls with Winamp and Chrome verbatim (no protocol prefix string) and they worked fine although it's likely both of those applications will use some form of intelligence to work out how to connect/stream.
If I tried that in my mediaPlayer code, I get the same exception as you but if I prefix the urls with http:// the songs play fine.
Example...
// Activity scope
Button button;
CheckBox checkBox;
String url = "";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//button declared in my activity
button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
if (!checkBox.isChecked())
url = getString(R.string.url_song1);
else
url = getString(R.string.url_song2);
mediaPlayer = new MediaPlayer();
}
#Override
public void onClick(View arg0) {
try {
Log.i(TAG, "onClick() entered...");
mediaPlayer.setDataSource(url);
Log.i(TAG, "Preparing mediaplayer...");
mediaPlayer.prepare();
Log.i(TAG, "Starting mediaplayer...");
mediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "bad stream");
}
}
If I copy the songs to my SD card both play fine and as long as the internet url strings have an 'http://' prefix then they also work.