samedi 26 avril 2014

How to Get Real Path from URL (URL to URi to Real Path)


Vote count:

0




First of all i dont know what am i looking for is real path or not, i think it is about real path so i'm asking.


I have a movie website, I'm showing videos from url and add subtitle to them with external subtitle(.srt). I made an android app that fetching datas from website , Anyway Let me summarize:


I'm fetching datas as a JSON and decoding it, E.g Movie Url :



http://ift.tt/1inwY8S


Subtile Url :



http://ift.tt/1fD9QgV


I could play video from URL in android but i change my all code for subtitle because I want to add subtitle to video. I found the solution but in the solution he is fetching srt and video from raw folder. How can i convert it to URL.


Solution codes :



protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtDisplay = (TextView) findViewById(R.id.txtDisplay);
MediaPlayer player = MediaPlayer.create(this, R.raw.video);
try {
player.addTimedTextSource(getSubtitleFile(R.raw.sub),
MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);
int textTrackIndex = findTrackIndexFor(
TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT, player.getTrackInfo());
if (textTrackIndex >= 0) {
player.selectTrack(textTrackIndex);
} else {
Log.w(TAG, "Cannot find text track!");
}
player.setOnTimedTextListener(this);
player.start();
} catch (Exception e) {
e.printStackTrace();
}
}

private String getSubtitleFile(int resId) {
String fileName = getResources().getResourceEntryName(resId);
File subtitleFile = getFileStreamPath(fileName);
if (subtitleFile.exists()) {
Log.d(TAG, "Subtitle already exists");
return subtitleFile.getAbsolutePath();
}
Log.d(TAG, "Subtitle does not exists, copy it from res/raw");

// Copy the file from the res/raw folder to your app folder on the
// device
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = getResources().openRawResource(resId);
outputStream = new FileOutputStream(subtitleFile, false);
copyFile(inputStream, outputStream);
return subtitleFile.getAbsolutePath();
} catch (Exception e) {
e.printStackTrace();
} finally {
closeStreams(inputStream, outputStream);
}
return "";
}


And my converted codes :



protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtDisplay = (TextView) findViewById(R.id.subtext);
Intent get= getIntent();
Uri uri = Uri.parse( get.getStringExtra("Video_URL"));
Uri uri2 = Uri.parse( get.getStringExtra("Subtitle_URL"));
MediaPlayer player = MediaPlayer.create(VideoActivity.this,uri );
try {
player.addTimedTextSource(getRealPathFromURI(uri2),
MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);
int textTrackIndex = findTrackIndexFor(
TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT, player.getTrackInfo());
if (textTrackIndex >= 0) {
player.selectTrack(textTrackIndex);
} else {
Log.w(TAG, "Cannot find text track!");
}
player.setOnTimedTextListener(VideoActivity.this);
player.start();
} catch (Exception e) {
e.printStackTrace();
}
}

private String getRealPathFromURI(Uri contentURI) {
String result;
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file path
result = contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx);
cursor.close();
}
return result;
}


I'm getting this error :



java.io.exception at android.mediaplayer.addtimedtextsource


Where am i doing wrong, any idea ?



asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire