18

संपादित करें:Android MediaPlayer Android 2.1 करने के लिए कस्टम ऑडियो स्ट्रीमिंग आवेदन अप में ठीक लेकिन उच्च संस्करणों में नहीं काम करता है

Android 2.2 MediaPlayer is working fine with one SHOUTcast URL but not with the other one

मैं ऑडियो फ़ाइलें बाह्य URL से (shoutcast स्ट्रीम) खेलने के लिए की जरूरत है। वर्तमान में ऑडियो फ़ाइलों को डाउनलोड किया जाता है & जैसे ही हम फोन स्थानीय अस्थायी भंडारण में पर्याप्त ऑडियो प्राप्त करते हैं। मैं StreamingMediaPlayer class का उपयोग कर रहा हूं।

चेक कोड के इस टुकड़े:

private MediaPlayer createMediaPlayer(File mediaFile) 
      throws IOException { 
     MediaPlayer mPlayer = new MediaPlayer(); 
     //example of mediaFile =/data/data/package/cache/playingMedia0.dat 
     FileInputStream fis = new FileInputStream(mediaFile); 
     mPlayer.setDataSource(fis.getFD()); 
     mPlayer.prepare(); 
     return mPlayer; 
    } 

वर्तमान स्थिति:

1- यह एंड्रॉयड 1.6 से 2.1 के लिए, लेकिन एंड्रॉयड 2.2 की तरह उच्च संस्करणों में नहीं ठीक काम करता है।

2- "mPlayer.setDataSource (fis.getFD())" वह रेखा है जो त्रुटि को फेंकता है।

3 त्रुटि है "मीडिया प्लेयर बनाने के लिए करने में असमर्थ"

अन्य समाधान की कोशिश की:

मैं वैकल्पिक समाधान नीचे की कोशिश की लेकिन कुछ भी नहीं अब तक काम किया।

Android 2.2 MediaPlayer is working fine with one SHOUTcast URL but not with the other one

क्या मैं देख रहा हूँ?

मेरा लक्ष्य कोड की शांति रखना है जो एंड्रॉइड 2.1 & पर काम कर सकता है।

यह समस्या भी यहाँ चर्चा की है:

1- Inconsistent 2.2 Media Player Behavior

2- android code for streaming shoutcast stream breaks in 2.2

3- यह समस्या भी इस साइट पर ढेर सारे प्रश्न में चर्चा की है, लेकिन मैंने पाया उत्तर कहीं नहीं।

4- markmail.org

LogCat ट्रेस: ​​

Unable to to create media player 
Error copying buffered conent. 
java.lang.NullPointerException 
com.ms.iradio.StreamingMediaPlayer.startMediaPlayer(StreamingMediaPlayer.java:251) 
com.ms.iradio.StreamingMediaPlayer.access$2(StreamingMediaPlayer.java:221) 
com.ms.iradio.StreamingMediaPlayer$2.run(StreamingMediaPlayer.java:204) 
android.os.Handler.handleCallback(Handler.java:587) 
android.os.Handler.dispatchMessage(Handler.java:92) 
android.os.Looper.loop(Looper.java:123) 
android.app.ActivityThread.main(ActivityThread.java:3683) 
java.lang.reflect.Method.invokeNative(Native Method) 
java.lang.reflect.Method.invoke(Method.java:507) 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
dalvik.system.NativeStart.main(Native Method) 
+1

शायद इस लिंक आपके प्रश्न का उत्तर होगा, और यदि नहीं, तो त्रुटि आपको मिल के लिए एक निशान पोस्ट करें: http://stackoverflow.com/questions/3834548/android-code-for-streaming-shoutcast- स्ट्रीम-ब्रेक-इन-2-2 – leanne

उत्तर

3

StreamingMediaPlayer वर्ग एक डबल बफरिंग तकनीक का उपयोग कर रहा है एंड्रॉयड के पूर्व 1.2 विज्ञप्ति में सीमाओं के आसपास पाने के लिए। एंड्रॉइड ओएस के सभी उत्पादन रिलीज में मीडियाप्लेयर शामिल है जो स्ट्रीमिंग मीडिया (1) का समर्थन करता है। मैं इस समस्या को हल करने के लिए इस डबल-बफरिंग तकनीक का उपयोग करने के बजाय ऐसा करने की सलाह दूंगा।

एंड्रॉइड ओएस 2.2 ने पुराने मीडिया प्लेयर कोड को फ्रेटकास्ट प्लेयर के साथ बदल दिया जो शायद इस मामले में अलग-अलग काम कर रहा है।

आपके स्टैक ट्रेस में लाइन नंबर आपके द्वारा लिंक की गई फ़ाइल पर मैप नहीं करते हैं, इसलिए मुझे लगता है कि आप वास्तव में उपयोग कर रहे एक अलग संस्करण हैं।मुझे लगता है कि NullPointerExceptionMediaPlayer द्वारा रिपोर्ट किया जा रहा है लेकिन न तो FileInputStream और न ही FileDescriptor लौटाया null हो सकता है।

(1) संस्करण 2.2 से पहले मीडिया प्लेयर प्रतिक्रिया में "आईसीवाई/1.1" संस्करण शीर्षलेख के साथ ShoutCast धाराओं को पहचान नहीं पाएगा। एक प्रॉक्सी बनाकर जो इसे "HTTP/1.1" से बदल देता है, आप उसे हल कर सकते हैं। उदाहरण के लिए StreamProxy class here देखें।

+0

1- मैं स्ट्रीमिंग मीडिया प्लेयर का उपयोग कर रहा हूं केवल इसमें मामूली बदलाव किए गए हैं। 2- एनआरपी स्ट्रीमप्रॉक्सी क्लास के लिए कई अन्य वर्गों (उदाहरण के लिए एनआरपी समाचार) की आवश्यकता है, मैंने इसे पहले ही इस्तेमाल करने की कोशिश की है लेकिन इसे सही तरीके से करने में सफल नहीं हो सका। –

+0

हम्म ... स्टैक ट्रेस #startMediaPlayer में लाइन 251 दिखाता है लेकिन लिंक किए गए स्रोत में यह पंक्ति #transferBufferToMediaPlayer में है। साथ ही, एनपीआर स्रोत से स्ट्रीमप्रॉक्सी को एनपीआर स्रोत से कुछ भी नहीं चाहिए, लेकिन एंड्रॉइड स्रोत में बहुत सी चीजें शामिल हैं। [यहां देखें] (http://code.google.com/p/npr-android-app/source/browse/Npr/src/org/npr/android/news/PlaybackService.java#347) कार्यान्वयन उदाहरण के लिए या [परीक्षण] (http://code.google.com/p/npr-android-app/source/browse/Npr_Test/src/org/npr/android/news/StreamProxyTest.java) केस। – jwadsack

+0

ग्रेट धन्यवाद। मैं सोमवार को कोशिश करूंगा। कृपया यह प्रश्न भी देखें: http://stackoverflow.com/questions/8681550/android-2-2-mediaplayer-is-working-fine-with-one-shoutcast-url-but-not-with-the –

5

समस्या यह है कि सामग्री प्रकार "ऑडियो/एएसीपी" स्ट्रीमिंग सीधे समर्थित नहीं है।

Freeware Advanced Audio (AAC) Decoder for Android

How to use this library?

Consider legal issues while using it: कुछ डिकोडिंग पुस्तकालयों "AACP" खेलने के लिए इस्तेमाल किया जा सकता, कृपया नीचे समाधान देखते हैं।

[टी] वह http://code.google.com/p/aacplayer-android/ परियोजना GPL के अंतर्गत लाइसेंस प्राप्त है, तो आप इसे के शीर्ष पर वाणिज्यिक क्षुधा बना सकते हैं, लेकिन आप जीपीएल fullfill करने के लिए की जरूरत है - मुख्य रूप से यह अच्छी तरह से के रूप में अपने कोड प्रकाशित करने का मतलब है। यदि आप दूसरी परियोजना http://code.google.com/p/aacdecoder-android/ का उपयोग करते हैं, तो आपको को अपना कोड प्रकाशित करने की आवश्यकता नहीं है (लाइब्रेरी को एलजीपीएल के तहत लाइसेंस प्राप्त है)।

+0

पर मेरी सहायता करें http://stackoverflow.com/questions/16264225/andriod-shoutcast-internet-radio पर मेरी सहायता करें -filenotfoundexception – String

1

मैं इस कोड का उपयोग कर रहा हूं और डाउनलोड स्ट्रीमिंग के लिए 2.2 संस्करण को ऊपरी संस्करण में चलाता हूं।

import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.URL; 
import java.net.URLConnection; 

import android.content.Context; 
import android.media.MediaPlayer; 
import android.os.Environment; 
import android.os.Handler; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageButton; 
import android.widget.ProgressBar; 
import android.widget.TextView; 


public class StreamingMediaPlayer { 
private static final int INTIAL_KB_BUFFER = 96*10;//assume 96kbps*10secs/8bits per byte 

private TextView textStreamed; 

private ImageButton playButton; 

private ProgressBar progressBar; 
ProgressBar pb; 
int audiofiletime=0; 
private long mediaLengthInSeconds; 
private int totalKbRead = 0; 
int totalsize=0; 
int numread; 
int totalBytesRead = 0; 
private final Handler handler = new Handler(); 
private MediaPlayer mediaPlayer; 
private File downloadingMediaFile; 
private boolean isInterrupted; 
private Context context; 
private int counter = 0; 

public StreamingMediaPlayer(Context context,TextView textStreamed, ImageButton playButton, Button streamButton,ProgressBar progressBar,ProgressBar pb) 
{ 
    this.context = context; 
    this.textStreamed = textStreamed; 
    this.playButton = playButton; 
    this.progressBar = progressBar; 
    this.pb=pb; 
} 

/** 
* Progressivly download the media to a temporary location and update the MediaPlayer as new content becomes available. 
*/ 
public void startStreaming(final String mediaUrl) throws IOException { 


    //this.mediaLengthInSeconds = 100; 


    Runnable r = new Runnable() { 
     public void run() { 
      try { 
       downloadAudioIncrement(mediaUrl); 
      } catch (IOException e) { 
       Log.e(getClass().getName(), "Unable to initialize the MediaPlayer for fileUrl=" + mediaUrl, e); 
       return; 
      } 
     } 
    }; 
    new Thread(r).start(); 
} 

/** 
* Download the url stream to a temporary location and then call the setDataSource 
* for that local file 
*/ 
@SuppressWarnings({ "resource", "unused" }) 
public void downloadAudioIncrement(String mediaUrl) throws IOException { 

    URLConnection cn = new URL(mediaUrl).openConnection(); 
    cn.connect(); 
    InputStream stream = cn.getInputStream(); 
    if (stream == null) { 
     Log.e(getClass().getName(), "Unable to create InputStream for mediaUrl:" + mediaUrl); 
    } 

    ///////////////////save sdcard/////////////// 
    File direct = new File(Environment.getExternalStorageDirectory()+"/punya"); 

    if(!direct.exists()) { 
     if(direct.mkdir()); //directory is created; 
    } 

    String[] files=mediaUrl.split("/"); 

    String fileName=files[files.length-1]; 
    fileName = fileName.replace(".m4a", ".rdo"); 


    //create a new file, to save the downloaded file 

    File file = new File(direct,fileName); 

    @SuppressWarnings("resource") 
    FileOutputStream fileOutput = new FileOutputStream(file); 

    ///////////////////end///////////////// 

    totalsize=cn.getContentLength(); 

    //mediaLengthInKb = 10000; 

    downloadingMediaFile = new File(context.getCacheDir(),fileName); 

    if (downloadingMediaFile.exists()) { 
     downloadingMediaFile.delete(); 
    } 

    FileOutputStream out = new FileOutputStream(downloadingMediaFile); 
    byte buf[] = new byte[16384]; 
    int incrementalBytesRead = 0; 
    do { 
     numread = stream.read(buf); 
     if (numread <= 0) 
      break; 
     out.write(buf, 0, numread); 
     fileOutput.write(buf, 0, numread); 
     totalBytesRead += numread; 
     incrementalBytesRead += numread; 
     totalKbRead = totalBytesRead/1000; 
     // pb.setMax(100); 
     // pb.setProgress(totalKbRead); 

     testMediaBuffer(); 
     fireDataLoadUpdate(); 
    } while (validateNotInterrupted()); 
    stream.close(); 
    if (validateNotInterrupted()) { 
     fireDataFullyLoaded(); 
    } 
} 

private boolean validateNotInterrupted() { 
    if (isInterrupted) { 
     if (mediaPlayer != null) { 
      mediaPlayer.pause(); 
      //mediaPlayer.release(); 
     } 
     return false; 
    } else { 
     return true; 
    } 
} 


/** 
* Test whether we need to transfer buffered data to the MediaPlayer. 
* Interacting with MediaPlayer on non-main UI thread can causes crashes to so perform this using a Handler. 
*/ 
private void testMediaBuffer() { 
    Runnable updater = new Runnable() { 
     public void run() { 
      if (mediaPlayer == null) { 
       // Only create the MediaPlayer once we have the minimum buffered data 
       if (totalKbRead >= INTIAL_KB_BUFFER) { 
        try { 
         startMediaPlayer(); 
        } catch (Exception e) { 
         Log.e(getClass().getName(), "Error copying buffered conent.", e);    
        } 
       } 
      } else if (mediaPlayer.getDuration() - mediaPlayer.getCurrentPosition() <= 1000){ 
       // NOTE: The media player has stopped at the end so transfer any existing buffered data 
       // We test for < 1second of data because the media player can stop when there is still 
       // a few milliseconds of data left to play 
       transferBufferToMediaPlayer(); 
      } 
     } 
    }; 
    handler.post(updater); 
} 

private void startMediaPlayer() { 
    try { 
     //File bufferedFile = new File(context.getCacheDir(),"playingMedia" + (counter++) + ".m4a"); 

     //moveFile(downloadingMediaFile,bufferedFile); 

     // Log.e(getClass().getName(),"Buffered File path: " + bufferedFile.getAbsolutePath()); 
     // Log.e(getClass().getName(),"Buffered File length: " + bufferedFile.length()+""); 

     mediaPlayer = createMediaPlayer(downloadingMediaFile); 

     //mediaPlayer.start(); 
     startPlayProgressUpdater();   
     //playButton.setEnabled(true); 
     playButton.setVisibility(View.VISIBLE); 
    } catch (IOException e) { 
     Log.e(getClass().getName(), "Error initializing the MediaPlayer.", e); 
     return; 
    } 
} 

private MediaPlayer createMediaPlayer(File mediaFile) 
     throws IOException { 
    MediaPlayer mPlayer = new MediaPlayer(); 
    mPlayer.setOnErrorListener(
      new MediaPlayer.OnErrorListener() { 
       public boolean onError(MediaPlayer mp, int what, int extra) { 
        Log.e(getClass().getName(), "Error in MediaPlayer: (" + what +") with extra (" +extra +")"); 
        return false; 
       } 
      }); 

    FileInputStream fis = new FileInputStream(mediaFile); 
    mPlayer.setDataSource(fis.getFD()); 
    mPlayer.prepare(); 
    return mPlayer; 
} 

/** 
* Transfer buffered data to the MediaPlayer. 
* NOTE: Interacting with a MediaPlayer on a non-main UI thread can cause thread-lock and crashes so 
* this method should always be called using a Handler. 
*/ 
private void transferBufferToMediaPlayer() { 
    try { 

     boolean wasPlaying = mediaPlayer.isPlaying(); 
     int curPosition = mediaPlayer.getCurrentPosition(); 

     File oldBufferedFile = new File(context.getCacheDir(),"playingMedia" + counter + ".m4a"); 
     File bufferedFile = new File(context.getCacheDir(),"playingMedia" + (counter++) + ".m4a"); 

     bufferedFile.deleteOnExit(); 
     moveFile(downloadingMediaFile,bufferedFile); 

     //mediaPlayer.pause(); 
     mediaPlayer.release(); 

     mediaPlayer = createMediaPlayer(bufferedFile); 
     mediaPlayer.seekTo(curPosition); 

     boolean atEndOfFile = mediaPlayer.getDuration() - mediaPlayer.getCurrentPosition() <= 1000; 
     if (wasPlaying || atEndOfFile){ 
      mediaPlayer.start(); 
     } 

     oldBufferedFile.delete(); 

    }catch (Exception e) { 
     Log.e(getClass().getName(), "Error updating to newly loaded content.", e);     
    } 
} 

private void fireDataLoadUpdate() { 
    Runnable updater = new Runnable() { 
     public void run() { 

      //float loadProgress = ((float)totalBytesRead/(float)mediaLengthInKb); 
      //float per = ((float)numread/mediaLengthInKb) * 100; 
      float per = ((float)totalBytesRead/totalsize) * 100; 
      textStreamed.setText((totalKbRead + " Kb (" + (int)per + "%)")); 
      progressBar.setSecondaryProgress((int)(per)); 
      pb.setSecondaryProgress((int)(per)); 

     } 
    }; 
    handler.post(updater); 
} 

private void fireDataFullyLoaded() { 
    Runnable updater = new Runnable() { 
     public void run() { 
      transferBufferToMediaPlayer(); 

      downloadingMediaFile.delete(); 
      textStreamed.setText(("Download completed")); 

     } 
    }; 
    handler.post(updater); 
} 

public MediaPlayer getMediaPlayer() { 
    return mediaPlayer; 
} 

public void startPlayProgressUpdater() { 
    audiofiletime =mediaPlayer.getDuration(); 
    float progress = (((float)mediaPlayer.getCurrentPosition()/ audiofiletime) * 100); 
    progressBar.setProgress((int)(progress)); 
    //pb.setProgress((int)(progress*100)); 

    if (mediaPlayer.isPlaying()) { 
     Runnable notification = new Runnable() { 
      public void run() { 
       startPlayProgressUpdater(); 
      } 
     }; 
     handler.postDelayed(notification,1000); 
    } 
}  

public void interrupt() { 
    playButton.setEnabled(false); 
    isInterrupted = true; 
    validateNotInterrupted(); 
} 

/** 
* Move the file in oldLocation to newLocation. 
*/ 
public void moveFile(File oldLocation, File newLocation) 
     throws IOException { 

    if (oldLocation.exists()) { 
     BufferedInputStream reader = new BufferedInputStream(new FileInputStream(oldLocation)); 
     BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(newLocation, false)); 
     try { 
      byte[] buff = new byte[5461]; 
      int numChars; 
      while ((numChars = reader.read( buff, 0, buff.length)) != -1) { 
       writer.write(buff, 0, numChars); 
      } 
     } catch(IOException ex) { 
      throw new IOException("IOException when transferring " + oldLocation.getPath() + " to " + newLocation.getPath()); 
     } finally { 
      try { 
       if (reader != null){      
        writer.close(); 
        reader.close(); 
       } 
      } catch(IOException ex){ 
       Log.e(getClass().getName(),"Error closing files when transferring " + oldLocation.getPath() + " to " + newLocation.getPath()); 
      } 
     } 
    } else { 
     throw new IOException("Old location does not exist when transferring " + oldLocation.getPath() + " to " + newLocation.getPath()); 
    } 
} 
} 
संबंधित मुद्दे