2012-03-08 11 views
18

मान लें कि मेरे पास फ़ाइल का पूरा पथ है। MediaPlayer में उस फ़ाइल को लोड करने का बेहतर तरीका कौन सा है?MediaPlayer setDataSource, पथ या फ़ाइलडिस्क्रिप्टर का उपयोग करने के लिए बेहतर है?

String filePath = "somepath/somefile.mp3"; 
mediaPlayer.setDataSource(filePath); 

या

String filePath = "somepath/somefile.mp3"; 
File file = new File(filePath); 
FileInputStream inputStream = new FileInputStream(file); 
mediaPlayer.setDataSource(inputStream.getFD()); 
inputStream.close(); 

इससे कोई फर्क पड़ता है? बस पथ का उपयोग करना आसान लगता है लेकिन क्या फ़ाइल डिस्क्रिप्टर का उपयोग करने के लिए अतिरिक्त काम करने का कोई कारण है?

+0

'ContentDrovider' या सामग्री के लिए संपत्ति फ़ोल्डर से सामग्री से खेलते समय फ़ाइलडिस्क्रिप्टर उपयोग योग्य है। – Jens

उत्तर

29

वास्तव में, यह पता चला है कि कुछ स्थितियों में कोई अंतर है।

mediaPlayer.setDataSource(String path) विफल हो जाएगा जब आप mediaPlayer.prepare() पर कॉल करते हैं, यदि आप फ़ाइल को सहेजने के तरीके के आधार पर getApplicationContext().getFilesDir() से फ़ाइल लोड करने का प्रयास कर रहे हैं। उदाहरण के लिए, यदि मैं new RandomAccessFile(filePath, "rw") का उपयोग कर एक फ़ाइल लिखता हूं, तो यदि आप mediaPlayer.setDataSource(String path) का उपयोग करते हैं तो फ़ाइल वास्तव में mediaplayer द्वारा पठनीय नहीं है। prepare() तुरंत मध्यस्थ से error(1, -2147483648) ट्रिगर करेगा; अनिवार्य रूप से एक फ़ाइल अनुमति त्रुटि। एसडीके 9 ने file.setReadable (boolean readable, boolean ownerOnly) पेश किया जो संभावित रूप से आपको ownerOnly को झूठी पर सेट करके इस समस्या को हल करने की अनुमति देगा ... लेकिन यदि आपको पुराने एसडीके का समर्थन करने की आवश्यकता है तो इससे आपकी सहायता नहीं होती है।

हाउवर, mediaPlayer.setDataSource(FileDescriptor fd) में यह समस्या नहीं है और mediaplayer बिना किसी समस्या की समस्या के ठीक उसी फ़ाइल को सफलतापूर्वक तैयार करेगा।

+1

-2147483648 फ़ाइल अनुमति त्रुटि नहीं है। – braden

+1

http://www.weston-fl.com/blog/?p=2988 भले ही, जो कुछ भी कारण है, स्ट्रिंग का उपयोग कर ऐप की फाइल डिर में फ़ाइल में मीडिया प्लेयर को इंगित करने में विफल रहता है लेकिन फ़ाइल डिस्क्रिप्टर काम करता है। –

+0

संबंधित नोट पर; एन्क्रिप्टेड एपीके विस्तार फ़ाइल (ओबीबी) से वीडियो चलाते समय आपको MediaPlayer में अपने वीडियो को चलाने के लिए फ़ाइलडिस्क्रिप्टर का उपयोग करने की आवश्यकता होती है। MediaPlayer आपके एप्लिकेशन के संदर्भ से बाहर फ़ाइलों तक पहुंचने का प्रयास करता है, जहां आपके ओबीबी-फाइल को पढ़ने के लिए सही अनुमति नहीं है। 'फाइलडिस्क्रिप्टर' का उपयोग इस मुद्दे पर निर्भर करता है। – Reinier

2

MediaPlayer.java ने डेटाटार्स() हस्ताक्षर सेट किए हैं जो स्ट्रिंग (पथ) और एक एफडी दोनों को स्वीकार करते हैं। वे अंततः देशी सी कोड में जाते हैं। हालांकि इनमें से एक थोड़ा अधिक कुशल हो सकता है, यह तब तक नगण्य होगा जब तक कि आप एक बार एक बार से अधिक बार अपने डेटा स्रोत को सेट नहीं कर रहे हों।

/** 
* Sets the data source (file-path or http/rtsp URL) to use. Call this after 
* reset(), or before any other method (including setDataSource()) that might 
* throw IllegalStateException in this class. 
* 
* @param path the path of the file, or the http/rtsp URL of the stream you want to play 
* @throws IllegalStateException if it is called 
* in an order other than the one specified above 
*/ 
public native void setDataSource(String path) throws IOException, IllegalArgumentException, IllegalStateException; 

/** 
* Sets the data source (FileDescriptor) to use. It is the caller's responsibility 
* to close the file descriptor. It is safe to do so as soon as this call returns. 
* Call this after reset(), or before any other method (including setDataSource()) 
* that might throw IllegalStateException in this class. 
* 
* @param fd the FileDescriptor for the file you want to play 
* @throws IllegalStateException if it is called 
* in an order other than the one specified above 
*/ 
public void setDataSource(FileDescriptor fd) 
     throws IOException, IllegalArgumentException, IllegalStateException { 
    // intentionally less than LONG_MAX 
    setDataSource(fd, 0, 0x7ffffffffffffffL); 
} 

/** 
* Sets the data source (FileDescriptor) to use. It is the caller's responsibility 
* to close the file descriptor. It is safe to do so as soon as this call returns. 
* Call this after reset(), or before any other method (including setDataSource()) 
* that might throw IllegalStateException in this class. 
* 
* @param fd the FileDescriptor for the file you want to play 
* @param offset the offset into the file where the data to be played starts, in bytes 
* @param length the length in bytes of the data to be played 
* @throws IllegalStateException if it is called 
* in an order other than the one specified above 
*/ 
public native void setDataSource(FileDescriptor fd, long offset, long length) 
     throws IOException, IllegalArgumentException, IllegalStateException; 
+0

धन्यवाद! मैंने सोचा कि शायद फाइल डिस्क्रिप्टर कुछ अनोखे लाभ पैदा कर सकता है; बफरिंग फायदे या ऐसी कुछ चीज। –

संबंधित मुद्दे

 संबंधित मुद्दे