MediaPlayer can not play mp3 from app data directory - android

I create an App which download a mp3 file and save it in app data directory and play it using android MediaPlayer class , the problem is, this code work on some device and not work on others, for example I noticed the device with SDK 10 and lower can't play it.
the code I use is very simple
mp = MediaPlayer.create(Sounds.this, Uri.fromFile(f));
mp.start();
I would appreciate any help.

Related

Android doesn't see playlist (.m3u) file

I am creating an own application for android that generates playlists. Application creates .m3u files in UTF-8 encoding format. Files exist and visible in file manager, but media players don't see the playlists and this is a problem. Playlists files are correct and work properly in build-in player in file manager.
Here is some part of the code, that gererates playlists:
File file = new File(Environment.getExternalStorageDirectory() + "/Music","Test.m3u");
PrintWriter writer = new PrintWriter(file, "UTF-8");
...
writer.println(PathToSong.toString()+"\r");
....
writer.flush();
writer.close();
I downloaded a program from Play Market that also creates playlists (.m3u) files. Playlists files that are built by the program have the same format as mine, but visible for media players. The most interesting thing is: ater launch the program from Play Market, playlists from my own program becomes visible for media players too.
Also my playlists become visible for the media players after restarting the phone. I develop and test my application using Android Studio 0.3.4.
I found the answer. It is needed to run media scanner so that mediafiles(photo, music, etc.) become visible in applications. Media Scanner runs automatically after phone restart.
To run Media Scanner manually you just need to paste and run this part of code:
UpdateMediaIntent = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()));
sendBroadcast(UpdateMediaIntent);
Hope, it will be helpful for somebody else too.

cocos2d-x how to play background music on android

I know if the music file under the 'Resources', it can work well.
But now, the music file under the '/data/' directory. It cant't play! why?
You can access via absolute path, example: /data/data/your.package.name/files/music.mp3

Video won't play with Android's native MediaPlayer, however it plays with Dropbox's video player

I'm having trouble playing a certain .3gp file (from a Dropbox link) with Android's MediaPlayer. However the file magically plays without problems from the Dropbox Android app.
I'm trying to play it using this:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.dropbox.com/s/zpiqwmxka6llavt/SketchyFile.3gp"));
startActivity(intent);
and my manifest contains
<uses-permission android:name="android.permission.INTERNET" />
which gives a "Can't play this video" error.
Logcat shows:
I/NuCachedSource2(89): new range: offset= 2245017
I/ChromiumHTTPDataSource(89): connect to https://dl.dropbox.com/0/view/a1crokuhpprsaeo/SketchyFile.3gp #2245017
I/NuCachedSource2(89): ERROR_END_OF_STREAM
E/MediaPlayer(3629): error (1, -2147483648)
E/MediaPlayer(3629): Error (1,-2147483648)
D/VideoView(3629): Error: 1,-2147483648
If I try to play it from the Dropbox Android app, Dropbox's video player has no problem playing it.
According to ffmpeg's ffprobe tool I could verify that the media type is not something exotic to Android.
Is Dropbox performing some weird transcoding voodoo and is there a way I can make this work in a similar way?
EDIT: Here's some more insight into DropBox's encoding voodoo.
Since I neither have the drop box app source or access to the actual file here is my best case guess that I will make on basis of a couple of assumptions.
The file is not playable by the native app because it is in a format that cannot be played there. Can you say download the file, put it in a sd card and then play it? If not, then the phone does not support this format.
Why does dropbox player play it? My guess based on same assumption is that drop box player adds its own decoding library (eg. ffmpeg ) along with it below the jni. So it can decode this.
Another possible guess:
You are trying to do a streaming play of 3gp file which is not possible.HTTP pd play may be possible. Again, download it and play it from sd card, if it plays, then probably this is the issue.
This file may not be hinted which it needs to be.
Drop box is playing this because perhaps the dropbox app is somehow accessing the header which is at end of file (your voodoo) through some other api?
Again both of the above are guesses from common problems i see as I neither have the dropbox app source, access to your file or know exactly how you are trying to play the video using native player.Perhaps one of the above tips help. Good luck!
EDIT: Issue seen: Nexus S does not play given file locally or from http link. Turns out remuxing to mp4 made Nexus S play it both ways. This implies 3gp support is not there in local native player in some android devices.
You can Use try-catch like follows :
try
{
VideoView videoView = (VideoView) findViewById(R.id.VideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
// Set Audio/Video
String strfilename = "http://example.com/video.mp4";
Uri video = Uri.parse(strfilename);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
}
catch (Exception e)
{
//enter code here
//Handle Errors
}

GET Access Token for Installed Applications for YouTube API

I want to get YouTube Videos in Android VideoView for my application.
I am going through the Google's YouTube API. I have generated the Client ID,Client Secret,redirect-URL and code by the help of this API Documentation.
https://developers.google.com/youtube/2.0/developers_guide_protocol#OAuth2_Installed_Applications_Flow
Next step is to get Access Token for Installed Applications, I am doing the same as the above Doc is explaining but getting this error.
java.io.FileNotFoundException: https://accounts.google.com/o/oauth2/token?code=4/sfsekiMy-EWE_7der69fy3wsdfdsseclient_id=412600012302.apps.googleusercontent.com&client_secret=0P7H4yqRZY2jmpluiyswqnbz&redirect_uri=http://localhost/oauth2callback&grant_type=authorization_code
Any help is highly appreciated OR you can suggest some way to access YouTube video in my Android App using only MediaPlayer and VideoView.
Thanks in advance.
Try this thread How to play YouTube video in my Android application?
Steps:
a. Create a new Activity, for your player(fullscreen) screen with menu options. Run the mediaplayer and UI in different threads.
b. For playing media - In general to play audio/video there is mediaplayer api in android. FILE_PATH is the path of file - may be url(youtube) stream or local file path
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(FILE_PATH);
mp.prepare();
mp.start();

Android Media player from file stored in memory

i have this problem, i create a app, in this app i record audio ogg file, i store file in memory (i see the file using DDMS) ,
when i wont to play the file audio i have a error
in this link you can see the source
main:
http://pastebin.com/PWE9FsxT
android manifest:
http://pastebin.com/p39J1Y6i
logcat
http://pastebin.com/ktyFWPXq
please help me
Best Regads
Antonio
This error comes because sometimes mediaplayer is not ready , put a condition before playing a
sound so that sound will play when media player is completely released .

Categories

Resources