I'm trying to load an intent to play a video file on my phone's SD drive. I'm not sure what I'm doing wrong...
String movieurl = Environment.getExternalStorageDirectory() + "/Videos/Wildlife.wmv";
Intent intentToPlayVideo = new Intent(Intent.ACTION_VIEW);
intentToPlayVideo.setDataAndType(Uri.parse(movieurl), "video/*");
startActivity(intentToPlayVideo);
I get an error "File cannot be displayed of played".
Any thoughts?
Note:
I've also tried:
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(movieurl);
mp.prepare();
mp.start();
Which fails with exception: java.io.IOException: Prepare failed.: status=0x1
Figured it out...
Turns out that on the Droid X2, Environment.getExternalStorageDirectory() returns "/mnt/sdcard", which isn't actually the SD Card.
(Found this out by doing a File.listFiles())
The actual SD Card directory on the Droid X2 is "/mnt/sdcard-ext".
Thanks for the help!
Does the WMV file play by itself in the standard media player? I'd suspect if you continue to get errors that perhaps the file is just not playable.
Try like this:
FileInputStream fis = new FileInputStream(new File(movieurl));
MediaPlayer mp = new MediaPlayer();
mediaPlayer.setDataSource(fis.getFD());
fis.close();
mp.prepare();
mp.start();
Related
First I make a call to
get_fullPathToAudio();
to determine the path of where an audio file was downloaded.
public File get_fullPathToAudioAsFile()
{
File storageFile = this.getExternalFilesDir(null);
if (storageFile == null)
{
// no external storage so store on private path
storageFile = this.getFilesDir();
}
return storageFile;
}
public String get_fullPathToAudio() {
return get_fullPathToAudioAsFile() + File.separator + this.get_currentArticleAudioFileName();
}
String filePath = this.myApp.get_fullPathToAudio();
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare();
mediaPlayer.start();
All works well if I have an SD drive on my emulator but when I don't have an emulator the function
get_fullPathToAudioAsFile();
uses
getFilesDir();
instead of
getExternalFilesDir(null);
which returns a path to my private drive where my app is installed. I can see the file in
data/data/com.myapp/files/myfile.m4a
for example. Is there a different way to play an audio file if it's not on the SD drive?
The error messsage I get is
error(-1, -2147483648)
Could not open file null for playback
I solved it this way
FileInputStream fileInputStream = new FileInputStream(mFile);
mPlayer.setDataSource(fileInputStream.getFD());
mPlayer.prepare();
MediaPlayer error -2147483648 when playing file on internal storage
I have downloaded and stored the expansion files successfully.But it crashes when i try to play the mp3 inside..
ZipResourceFile expansionFile = APKExpansionSupport.getAPKExpansionZipFile(getApplicationContext(),
1, 0);
InputStream fileStream = expansionFile.getInputStream("mysong.mp3");
AssetFileDescriptor asd = expansionFile.getAssetFileDescriptor("mysong.mp3");
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(asd.getFileDescriptor(), asd.getStartOffset(), asd.getLength());
asd.close();
mediaPlayer.prepare();
mediaPlayer.start();
Here the logcat says both the inputstream and the file descriptor are null..Can anyone help me ?
Finaly it solved.. The file name should be with the full path..
AssetFileDescriptor asd = expansionFile.getAssetFileDescriptor("main.1.com.mypackage.app/mysong.mp3");
I am trying to play audio files stored in the assets directory, but before I even read a file I get the error "error occured java.io.FileNotFoundException: track.mp3". Here's how I read it:
AssetFileDescriptor afd = getAssets().openFd("track.mp3");
I read a lot of descriptions on the internet, but no success.
I think this code should work for you:
AssetFileDescriptor afd = getAssets().openFd("track.mp3");
MediaPlayer player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
player.prepare();
player.start();
Otherwise, check if the file is in the right folder and the name is "track.mp3".
Right-click your "track.mp3" file, with the "Refactor" > "Move" command, move your file to another directory (e.g. "res"). With this same command, move it back to your "assets" directory. For me, it works.
Android guidelines suggest for Audio use res/raw folder. So make sure that all audio are there and when you read you can use this method:
void playAudio(Context mContext, String sound){
new AudioPlayer().play(mContext, mContext.getResources().getIdentifier(
sound,"raw", mContext.getPackageName()));
}
And if you need something line audio player then take a look at MediaPlayer
Use this code
MediaPlayer mediaPlayer = new MediaPlayer();
AssetFileDescriptor afd;
try {
afd = getAssets().openFd("1.mp3");
mediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
Toast.makeText(LearnActivity.this, "Error Playing audio from Asset directory",Toast.LENGTH_SHORT).show();
}
Otherwise, check if the file is in the right folder and the name is "track.mp3".
Can someone please point to me what is wrong with my code?
try{
MediaPlayer p = new MediaPlayer();
p.setDataSource(getCacheDir() + "/temp.mp3");
p.prepare();
p.start();
}catch(Exception e){}
MediaPlayer p catches Exception whenever it calls prepare(). File temp.mp3 is already inside the cache folder and it is not corrupt or what so ever but it doesn't get played. Is it that MediaPlayer can't play files on cache?
I hope someone can give me an idea of what I have done wrong. Thanks in advance!
Probably cache dir is your private dir and media player can't access it. Try to play from SD card.
Use the Filedescriptor:
try{
MediaPlayer p = new MediaPlayer();
FileInputStream fis = new FileInputStream(getCacheDir() + "/temp.mp3");
p.setDataSource(fis.getFD());
p.prepare();
p.start();
}catch(Exception e){}
As I understand it, Android will only play AAC format audio if it's encoded as MPEG-4 or 3GPP.
I'm able to play AAC audio encoded as M4A when it's local to the app, but it fails when obtaining it from a server.
The following works, as the m4a file is held locally in the res/raw directory.
MediaPlayer mp = MediaPlayer.create(this, R.raw.*file*);
mp.start();
The following doesn't work. (But does with MP3's).
Uri uri = Uri.parse("http://*example.com*/blah.m4a");
MediaPlayer mp = MediaPlayer.create(this, uri);
mp.start();
Can anyone shed any light on why it fails when the m4a audio file is not local?
Here's (some of) the error...
ERROR/PlayerDriver(542): Command PLAYER_INIT completed with an error or info UNKNOWN PVMFStatus
ERROR/MediaPlayer(769): error (200, -32)
WARN/PlayerDriver(542): PVMFInfoErrorHandlingComplete
DEBUG/MediaPlayer(769): create failed:
DEBUG/MediaPlayer(769): java.io.IOException: Prepare failed.: status=0xC8
DEBUG/MediaPlayer(769): at android.media.MediaPlayer.prepare(Native Method)
DEBUG/MediaPlayer(769): at android.media.MediaPlayer.create(MediaPlayer.java:530)
DEBUG/MediaPlayer(769): at android.media.MediaPlayer.create(MediaPlayer.java:507)
...
I'm targeting SDK 1.6.
This work-around allows you to play M4A files from the net (and AAC files in other containers such as MP4 & 3GP). It simply downloads the file and plays from the cache.
private File mediaFile;
private void playAudio(String mediaUrl) {
try {
URLConnection cn = new URL(mediaUrl).openConnection();
InputStream is = cn.getInputStream();
// create file to store audio
mediaFile = new File(this.getCacheDir(),"mediafile");
FileOutputStream fos = new FileOutputStream(mediaFile);
byte buf[] = new byte[16 * 1024];
Log.i("FileOutputStream", "Download");
// write to file until complete
do {
int numread = is.read(buf);
if (numread <= 0)
break;
fos.write(buf, 0, numread);
} while (true);
fos.flush();
fos.close();
Log.i("FileOutputStream", "Saved");
MediaPlayer mp = new MediaPlayer();
// create listener to tidy up after playback complete
MediaPlayer.OnCompletionListener listener = new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
// free up media player
mp.release();
Log.i("MediaPlayer.OnCompletionListener", "MediaPlayer Released");
}
};
mp.setOnCompletionListener(listener);
FileInputStream fis = new FileInputStream(mediaFile);
// set mediaplayer data source to file descriptor of input stream
mp.setDataSource(fis.getFD());
mp.prepare();
Log.i("MediaPlayer", "Start Player");
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
I tried it too but I could not find out the solution!
At the last Google I/O I saw something that helped me a lot. It is Extending from MediaPlayer and improve a lot of things! Take a look.
EXOPLAYER CAN HELP YOU A LOT
Check this part of the example:
private static final int BUFFER_SEGMENT_SIZE = 64 * 1024;
private static final int BUFFER_SEGMENT_COUNT = 256;
...
// String with the url of the radio you want to play
String url = getRadioUrl();
Uri radioUri = Uri.parse(url);
// Settings for exoPlayer
Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE);
String userAgent = Util.getUserAgent(context, "ExoPlayerDemo");
DataSource dataSource = new DefaultUriDataSource(context, null, userAgent);
ExtractorSampleSource sampleSource = new ExtractorSampleSource(
radioUri, dataSource, allocator, BUFFER_SEGMENT_SIZE * BUFFER_SEGMENT_COUNT);
audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource);
// Prepare ExoPlayer
exoPlayer.prepare(audioRenderer);
EXOPLAYER- I can play anything from streamings (video and audio)!
LET ME KNOW IF YOU NEED HELP TO IMPLEMENT IT! :)
This is a wild shot in the dark, but I have seen similar behavior with the flash player where it actually ignores the file name and only relies on the MIME type sent by the server. Any idea what headers are being sent down from example.com? You might want to try wrapping your blah.m4a in a page that can set the headers and then stream the binary data. Give these types a shot and the community would appreciate a confirmation of what works:
audio/mpeg
audio/mp4a
audio/mp4a-latm
audio/aac
audio/x-aac
I found that if you record the audio file on Android with the following properties, you are then able to play it on your server. It also plays well in the HTML Audio Element, however only on Firefox at the moment. This may change in the future.
Android (JAVA):
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);
mediaRecorder.setAudioSamplingRate(44100);
mediaRecorder.setAudioChannels(1);
mediaRecorder.setOutputFile(filePath);
HTML:
<audio id="audioMediaControl" controls src="yourfile.m4a"> Your browser does not support the audio element. </audio>
try --
1) MP.prepareAsync()
2) onPrepared() { mp.start() }