I am trying to create a RTSP player for Android, but I am getting the error Video Can't be played. I don't know whats the mistake I am making, this is simply not working, I tried all methods, I am giving the code below
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends Activity {
VideoView myVideoView;
ProgressDialog progDailog;
AudioManager audio;
MediaController mediaController;
String unStringUrl="rtsp://his.dvrdns.org:8554/channel/2";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myVideoView = (VideoView)findViewById(R.id.videoplayer);
progDailog = ProgressDialog.show(MainActivity.this, null, "Video loading...", true);
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mediaController = new MediaController(this);
myVideoView.setMediaController(mediaController);
myVideoView.setVideoURI(Uri.parse(unStringUrl));
myVideoView.requestFocus();
myVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer arg0) {
// called too soon with rtsp in 4.1
if(progDailog != null) {
progDailog.dismiss();
}
myVideoView.start();
}
});
myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
/*Intent intent = new Intent(MyVideoView.this, lastActivity);
intent.putExtra("cleTitre", activityTitle);
intent.putExtra("cleSegment", activityCat);
startActivity(intent);*/
}
});
myVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
if(progDailog != null) {
progDailog.dismiss();
}
return false;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
You can try including internet -Permisison line in (following line) in your app's Manifest File.In my case it worked.
uses-permission android:name="android.permission.INTERNET"
Hope this helps.
Related
I'm using Android Studio.I tried all the codes that I've seen on the internet but my app doesn't work. I have a raw folder which contains the music that I'll be use but still I don't know what is the error in my code.
Here's my code:
package com.example.aloja.babysteps;
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
/**
* Created by Aloja on 3/27/2017.
*/
public class A extends Activity {
Button btnBack3,btnPlay;
ImageView ivApple;
MediaPlayer apple;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
btnBack3 = (Button) findViewById(R.id.btnBack3);
btnPlay =(Button) findViewById(R.id.btnPlay);
ivApple = (ImageView) findViewById(R.id.ivApple);
ivApple.setImageResource(R.drawable.apple);
apple= MediaPlayer.create(this, R.raw.apple);
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
apple.start();
}
});
}
protected void onPause(){
super.onPause();
apple.stop();
apple.release();
}
}
P.S Sorry for my english. Hope you understand what I'm trying to ask
Try this code hope it will help you.
int resID=getResources().getIdentifier("filename", "raw", getPackageName());
MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.e("MediaPlayer", "what=="+what);
Log.e("MediaPlayer", "extra=="+extra);
return false;
}
});
mediaPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
#Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
Log.e("MediaPlayer", "what=="+what);
Log.e("MediaPlayer", "extra=="+extra);
return false;
}
});
mediaPlayer.start();
I test your code on MX5 and it works well.
Did you enable your phone's music volume ?
(In MX5, you can see tone, media, notification and system volume.)
I am trying to play an audio file from the internal storage.
The code I used is..
package com.abhi.firstapp.firstapp;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.net.URI;
public class MainActivity extends AppCompatActivity {
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
File f= new File("/sdcard/a.mp3");
if(f.exists())
{
Toast toast= Toast.makeText(this, "file exists", Toast.LENGTH_LONG);
toast.show();
Log.d("uri","1");
Uri uri= Uri.fromFile(f);
Log.d("uri", "2");
mp= new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
Log.d("uri", "3");
try {
mp.setDataSource("/sdcard/a.mp3");
} catch (IOException e) {
e.printStackTrace();
}
//mp.setDataSource(getBaseContext(), uri);
Log.d("uri", "4");
try {
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
Log.d("uri", "IOException");
}
mp.start();
}
else {
Toast toast1 = Toast.makeText(this, "file does not exist", Toast.LENGTH_LONG);
toast1.show();
}
//MediaPlayer mp= MediaPlayer.create(getBaseContext(), uri);
//mp.start();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
By using the log, I can determine that this code is running till the mp.prepare(mediaplayer prepare). And on this step, it gives the error Illegal State Exception
Caused by: java.lang.IllegalStateException
at android.media.MediaPlayer.prepare(Native Method)
Please Help!
There are a couple of things you might want to change.
First: mp.prepare() will block your main thread, which is forbidden and will result in an exception where Android will close your app. To prevent this, mp.prepareAsync was designed. Use that method instead and implement both an onPreparedListener and an onErrorListener.
Second: you should provide a datasource before you call prepare().
You could do this for example this way:
public class MainActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
MediaPlayer mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(streamURL);
} catch (IOException e) {
// Error, do something
}
mp.prepareAsync();
...
}
#Override
public void onPrepared(MediaPlayer player) {
mediaPlayer.start();
}
...
}
I had the same problem. It can be raised because MediaPlayer is already prepared. When you create a new MediaPlayer by MediaPlayer mp = MediaPlayer.create(getContext(),someUri); mp prepares automatically so you should not prepare it by yourself.
I am trying to a make an app that has just one button . When you press it plays a sound and when you LONG press it , It gives you the option to share . The app is working without any errors .. but the share option just wont open . And the button does nothing when you long press it .
package com.example.buttonclicksound;
import com.example.buttonclicksound.MainActivity;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
protected static final String TAG = "MainActivity";
Button button1;
MediaPlayer mPlayer;
AnimationDrawable lightsAnimation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//No title bar is set for the activity
requestWindowFeature(Window.FEATURE_NO_TITLE);
//Full screen is set for the Window
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
ImageView lights = (ImageView) findViewById(R.id.imageView1);
lightsAnimation = (AnimationDrawable) lights.getDrawable();
button1 = (Button) findViewById(R.id.button1);
mPlayer = MediaPlayer.create(MainActivity.this, R.raw.splash);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
mPlayer.start();
mPlayer.setLooping(false);
} catch (Exception e) {
Log.e("ButtonListenerActivity", "error: " + e.getMessage(),
e);
}
}
});
button1.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
shareIt();
return true;
}
private void shareIt() {
//sharing implementation here
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("audio/mPlayer");
}
});
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
lightsAnimation.start();
}
protected void onDestroy() {
super.onDestroy();
// TODO Auto-generated method stub
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Its extremly frustrating .. been working so hard at this .. and the Daam thing just wont work .
after some further work .. I am now stuck at this .. the send via box opens up .. i select email .. but all i get is a blank email
private void shareIt(MediaPlayer mPlayer) {
//sharing implementation here
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("audio/mp3");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT,"Ringtone File:"+getResources().getResourceEntryName(mPlayer)+".mp3");
sharingIntent.putExtra(Intent.EXTRA_TEXT,"Ringtone File : "+getResources().getResourceEntryName(mPlayer)+".mp3");
sharingIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("android.resource://com.my.android.soundfiles/"+mPlayer));
sharingIntent.putExtra("sms_body","Ringtone File : "+getResources().getResourceEntryName(mPlayer)+".mp3");
startActivity(Intent.createChooser(sharingIntent, "Share Sound File"));
}
});
You're not doing anything with your sharingIntent, just creating it.
I'm new to android and I need it for a project I'm working on. I need it to play a sound by clicking a button. I followed online tutorials but my code does not play sounds at all and I'm getting an error on MediaPlayer Error(-19,0). I've tried a lot of the fixes I saw here and I can't seem to make it work. Any help? Here's the code
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AudioManager audioManager = (AudioManager) getSystemService(MainActivity.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);
Button buttonHello = (Button) findViewById(R.id.button1);
buttonHello.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer mp = Medi aPlayer.create(MainActivity.this, R.raw.button);
mp.start();
mp.setOnCompletionListener(new OnCompletion Listener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
};
});
}
});
} catch (Exception e) {
System.out.println("Error!");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
This links http://marakana.com/forums/android/examples/59.html has the great example to use MediaPlayer class to play song from raw folder.
To play song from files in the memory you can use the following code.
MediaPlayer mp = new MediaPlayer();
mp.reset();
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filename.getAbsolutePath());
Uri selectedImage=RingtoneDownload.this.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
try {
mp.setDataSource(RingtoneDownload.this,selectedImage);
mp.prepare();
mp.start();
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This code is working for me while set the source from file.
I'm doing a aplication to recorder the sound, but when I try to record the sound,the emulator is stopped because it ask me to insert an SD card. What I have to do? I couldn't do without an SD card ?
Thank you!
package proiektua.proiektua;
import java.io.FileDescriptor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
int peticion = 1;
Uri url1;
private int STATE_CONFIGURE;
private int mState = STATE_CONFIGURE;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void grabar(View v) {
Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(intent, peticion);}
public void setOutputFile(FileDescriptor fd) {
switch (mState) {
case STATE_RECORD:
throw new RuntimeException("setOutputFile cannot be called while recording!");
case STATE_RELEASED:
throw new RuntimeException("setOutputFile called on an already released recorder!");
default:
break;
}
}
public void reproducir(View v) {
MediaPlayer mediaPlayer = MediaPlayer.create(this, url1);
mediaPlayer.start();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == peticion) {
url1 = data.getData();
}
}
}
You have the option to create an SDCard through the mksdcard command and associate it with the amulator.
please look at this function:
public void setOutputFile (FileDescriptor fd)
Added in API level 3
Pass in the file descriptor of the file to be written. Call this after setOutputFormat() but before prepare().
You can decide where to store the file.
also look at this example.
http://androidfreakers.blogspot.co.il/2011/09/using-mediarecorder.html