当前位置:操作系统 > 安卓/Android >>

android 实现流媒体播放远程mp3文件代码

[java] 
<SPAN style="COLOR: #cc6600; FONT-SIZE: 12px">package com.shadow.util; 
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 java.util.ArrayList; 
import java.util.List; 
import com.shadow.service.AudioPlayService.LocalBinder; 
import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.os.Binder; 
import android.os.Handler; 
import android.os.IBinder; 
import android.util.Log; 
import android.widget.Button; 
import android.widget.ImageButton; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
import android.widget.Toast; 
/**
 * MediaPlayer does not yet support streaming from external URLs so this class provides a pseudo-streaming function
 * by downloading the content incrementally & playing as soon as we get enough audio in our temporary storage.
 */ 
public class StreamingMediaPlayer extends Service{ 
    private static final int INTIAL_KB_BUFFER =  96*10/8;//assume 96kbps*10secs/8bits per byte  
    private TextView textStreamed; 
     
    private ImageButton playButton; 
     
    private ProgressBar    progressBar; 
     
    //  Track for display by progressBar  
    private long mediaLengthInKb, mediaLengthInSeconds; 
    private int totalKbRead = 0; 
    // Create Handler to call View updates on the main UI thread.  
    private final Handler handler = new Handler(); 
    private MediaPlayer     mediaPlayer; 
    private File downloadingMediaFile;  
    private boolean isInterrupted; 
    private Context context; 
    private int counter = 0; 
    private static Runnable r; 
    private static Thread playerThread; 
    private LocalBinder localBinder = new LocalBinder(); 
    private MediaPlayer player; 
    private boolean isPause = false;     //播放器是否处于暂停状态  
    private boolean isSame = false;      //所点播歌曲是否是当前播放歌曲  
    private Integer position = -1;       //设置播放标记  
    private List<String> music_name;     //歌曲列表  
    private List<String> music_path; 
     
     public StreamingMediaPlayer(Context  context,TextView textStreamed, ImageButton    playButton, Button    streamButton,ProgressBar    progressBar)  
     { 
         this.context = context; 
        this.textStreamed = textStreamed; 
        this.playButton = playButton; 
        this.progressBar = progressBar; 
    } 
     
    /**  
     * Progressivly download the media to a temporary location and update the MediaPlayer as new content becomes available.
     */   
    public void startStreaming(final String mediaUrl, long    mediaLengthInKb, long    mediaLengthInSeconds) throws IOException { 
         
        this.mediaLengthInKb = mediaLengthInKb; 
        this.mediaLengthInSeconds = mediaLengthInSeconds; 
         
        r = new Runnable() {    
            public void run() {    
                try { 
                    Log.i("downloadAudioIncrement", "downloadAudioIncrement"); 
                    downloadAudioIncrement(mediaUrl); 
                } catch (IOException e) { 
                    Log.e(getClass().getName(), "Unable to initialize the MediaPlayer for fileUrl=" + mediaUrl, e); 
                    return; 
                }    
            }    
        };    
        playerThread = new Thread(r); 
        playerThread.start(); 
        //new Thread(r).start();  
    } 
     
    /**  
     * Download the url stream to a temporary location and then call the setDataSource  
     * for that local file
     */   
    public void downloadAudioIncrement(String mediaUrl) throws IOException { 
         
        URLConnection cn = new URL(mediaUrl).openConnection();    
        cn.addRequestProperty("User-Agent","NSPlayer/10.0.0.4072 WMFSDK/10.0"); 
        cn.connect();    
        InputStream stream = cn.getInputStream(); 
        if (stream == null) { 
            Log.e(getClass().getName(), "Unable to create InputStream for mediaUrl:" + mediaUrl); 
     &nb

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,